In this article, we will show you how to retrieve wordpress posts within specific time frame and display all months wordpress post. code snippet with detail.
how to display all months wordpress posts on homepage
WordPress is providing as full proof wordpress api so we can achieve the this using wordpress api only.
You can use the query_posts() function for fetching the wordpress posts. Open your index.php from your wordpress template folder. Before loop just use the following code for in index.php file.
$current_date = getdate(); query_posts(monthnum=' .$current_date["mon"] );
You can pass the multiple parameters to query_posts function as per your requirement.
- hour= – hour (from 0 to 23)
- minute= – minute (from 0 to 60)
- second= – second (0 to 60)
- day= – day of the month (from 1 to 31)
- monthnum= – month number (from 1 to 12)
- year= – 4 digit year (e.g. 2009)
- w= – week of the year (from 0 to 53)
Now I will show the another example. which will show only current date post on home page.
$current_date = getdate(); query_posts('year=' .$current_date["year"] .'&monthnum=' .$current_date["mon"] .'&day=' .$current_date["mday"] );
Another good example for fetching the 30 days latest post from wordpress
<!--?php //based on Austin Matzko's code from wp-hackers email list function filter_where($where = '') { //posts in the last 30 days $where .= " AND post_date --> '" . date('Y-m-d', strtotime('-30 days')) . "'"; return $where; } add_filter('posts_where', 'filter_where'); query_posts($query_string); ?>
Using above code you can display all months wordpress posts.
Hi,
Firstly, many thanks for this little snippet, it has been a great help.
However, I am running into a problem when using the following:
The above works, except it displays the first, most recent post twice. Am I doing anything wrong?
Many thanks!