Title: Display only  posts in latest post&#8217;s year and month
Last modified: August 19, 2016

---

# Display only posts in latest post’s year and month

 *  Resolved [1251-1](https://wordpress.org/support/users/1251-1/)
 * (@1251-1)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/monthly-posts/)
 * I would like to display my posts by the most recent post’s month, on my home 
   page.
 * For example, if I published 20 posts this current month, they would be displayed.
   But as soon as I published a new post in a new month, I would start a fresh page.
 * I hope this makes sense.
 * Here is my loop at the moment:
 *     ```
       <?
       	if(have_posts())
       	{
       		while(have_posts())
       		{
       			the_post();
       ?>
       			<div>
       				<h1><a href="<?the_permalink();?>" title="<?the_title_attribute();?>"><?the_title();?></a></h1>
       				<?the_content();?>
       			</div>
       <?
       		}
       	}
       	else
       	{
       ?>
       		<div>
       			<h1>Not found</h1>
       			<p>Sorry, but there is nothing here.</p>
       		</div>
       <?
       		get_search_form();
       	}
       ?>
       ```
   

Viewing 5 replies - 1 through 5 (of 5 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/monthly-posts/#post-1111357)
 * Just before your `if (have_posts())`
 *     ```
       $today = getdate();
       query_posts($query_string . '&year=' . $today["year"] . '&monthnum='.$today["mon"]);
       ```
   
 * Related:
    [Time Parameters of query_posts](http://codex.wordpress.org/Template_Tags/query_posts#Time_Parameters)
 *  Thread Starter [1251-1](https://wordpress.org/support/users/1251-1/)
 * (@1251-1)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/monthly-posts/#post-1111428)
 * I don’t think that code quite works for what I want.
 * I don’t want the current month (June), I want the most recent month from when
   my last published post.
 * If my last post was from March, I want all of the March posts on the home page.
   Now it’s June, if I make a new post all posts from March disappear and the page
   is made fresh for more June posts.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/monthly-posts/#post-1111437)
 *     ```
       <?php
       //get latest post, then get all posts in that one post's year and month
       $args=array(
        'showposts'=>1,
        'caller_get_posts'=>1
         );
       $lastpost = get_posts($args);
       if ( $lastpost ) {
       foreach($lastpost as $single) {
       $fpyear = mysql2date("Y", $single->post_date);
       $fpmonth = mysql2date("m", $single->post_date);
       }
       }
       query_posts($query_string . '&year=' . $fpyear . '&monthnum='.$fpmonth);
       ?>
       ```
   
 *  Thread Starter [1251-1](https://wordpress.org/support/users/1251-1/)
 * (@1251-1)
 * [16 years, 11 months ago](https://wordpress.org/support/topic/monthly-posts/#post-1111448)
 * Thank you!
 *  [veinkling](https://wordpress.org/support/users/veinkling/)
 * (@veinkling)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/monthly-posts/#post-1111507)
 * Hello Michael H, can you help? I have just tried your code above to show ONLY
   current month’s posts on front page, and it worked successfully. However, when
   I go to the previous months in the archive section on the right, everything has
   disappeared! How do I keep only this month’s post on the front page, but keep
   all the other months’ posts archived in their relevant month category?

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Display only posts in latest post’s year and month’ is closed to new 
replies.

## Tags

 * [meh_code](https://wordpress.org/support/topic-tag/meh_code/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 3 participants
 * Last reply from: [veinkling](https://wordpress.org/support/users/veinkling/)
 * Last activity: [16 years, 10 months ago](https://wordpress.org/support/topic/monthly-posts/#post-1111507)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
