Title: `get_posts` by months?
Last modified: August 22, 2016

---

# `get_posts` by months?

 *  [Agence Myso](https://wordpress.org/support/users/agence-myso/)
 * (@agence-myso)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/get_posts-by-months/)
 * Hi everyone,
 * I’m getting mad… I am using wp_get_archives_cpt to display a list of custom post
   types by month. I would like the corresponding posts to show on the same archive
   page.
    But I can’t figure out how to display the posts corresponding to each 
   month… I don’t see any way to query posts by month ??! Please help me ! Thank
   you all.

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

 *  Thread Starter [Agence Myso](https://wordpress.org/support/users/agence-myso/)
 * (@agence-myso)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/get_posts-by-months/#post-5664180)
 * Just to let you know,
 * I am trying to retrieve the posts date by using get_query_var, then get_posts
   by month.
    But when I echo `wp_reset_postdata();`, all I get is a 0. Anyone knows
   why the hell?
 *  Thread Starter [Agence Myso](https://wordpress.org/support/users/agence-myso/)
 * (@agence-myso)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/get_posts-by-months/#post-5664181)
 * Sorry…
    When I wanted to write : “when I echo `$month=get_query_var('monthnum');`”
 *  [csloisel](https://wordpress.org/support/users/csloisel/)
 * (@csloisel)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/get_posts-by-months/#post-5664182)
 * It would help to see the full code you are using to do the query.
 *  Thread Starter [Agence Myso](https://wordpress.org/support/users/agence-myso/)
 * (@agence-myso)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/get_posts-by-months/#post-5664184)
 * Hi,
    Thank you the quick answer! Here is my code :
 *     ```
       <?php $month=get_query_var('monthnum');
                           $args= array(
                               'monthnum' => $month
                           );
                           echo $month;
                           $my_posts = query_posts($args);
                           foreach ($my_posts as $posts) : setup_postdata( $post ); ?>
   
                       <li><a class="post-link" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                           <?php endforeach; wp_reset_postdata();?>             
   
                   </div>
               <?php } ?>
       ```
   
 *  Thread Starter [Agence Myso](https://wordpress.org/support/users/agence-myso/)
 * (@agence-myso)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/get_posts-by-months/#post-5664197)
 * Well, I’m sorry, I’m a bit confused, it is late here… 🙂
    So, here is the full
   code for my menu :
 *     ```
       if ( is_date() ){ ?>
                   <div class="menu2 col-lg-4 col-md-4 col-xs-4">
   
                   <?php
                   $args = array(
                           'type'            => 'monthly',
                           'post_type'       => 'video',
                           'limit'           => '',
                           'format'          => 'html',
                           'before'          => '',
                           'after'           => '',
                           'show_post_count' => false,
                           'echo'            => 1,
                           'order'           => 'DESC'
                       );
                   wp_get_archives_cpt($args); ?>
   
                   </div>
   
                   <div class="menu3 col-lg-4 col-md-4 col-xs-4">
   
                   <?php $month=get_query_var('monthnum');
                           $args= array(
                               'monthnum' => $month
                           );
                           $my_posts = query_posts($args);
                           foreach ($my_posts as $posts) : setup_postdata( $posts ); ?>
   
                       <li><a class="post-link" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                           <?php endforeach; wp_reset_postdata();?>             
   
                   </div>
               <?php } ?>
       ```
   
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [11 years, 4 months ago](https://wordpress.org/support/topic/get_posts-by-months/#post-5664311)
 * I think your only issue is that `monthnum` is not necessarily defined for any
   particular date archive query. `is_date()` returns true for any date query, whether`
   monthnum` is defined or not. For example the request `example.com/2014/` will
   cause your code to execute but `monthnum` is undefined.
 * If you feel monthnum should be defined for the request you are using, please 
   provide an example request URL. This is assuming your code will do what you want
   if `monthnum` were defined, of which I’m not convinced.
 * I’m not too clear what you are really trying to do here. I assume `wp_get_archives_cpt()`
   outputs a list of archive links for months where the video post type is published,
   correct? Then you say you want corresponding posts. Corresponding to what?
 * Also, where does this code occur? The surrounding context is important, though
   publishing everything is probably not necessary.
 *  Thread Starter [Agence Myso](https://wordpress.org/support/users/agence-myso/)
 * (@agence-myso)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/get_posts-by-months/#post-5664469)
 * Hello,
 * I’m sorry I’ve taken time to thank you and be more precise in my explanations…
 * What I want to do is to display posts (video custom posts in this particular 
   case) by two ways :
    -Category -date
 * When you arrive on the website, you have three menus : NEWS / VIDEOS / PRINT.
   
   VIDEOS and PRINT will lead you to category menus, no problem.
 * NEWS will lead you to a custom menu, where posts of the other categories are 
   displayed by month.
    So, if you click on NEWS, you will get into `archive.php`,
   where you will have a list of months. Ok for this. Once you click on a month,
   a list of posts will be displayed. This is ok too.
 * Problem : when I click on a post listed by month, I get the post in its category.
   I mean, for example, you click on video2 : that brings you to VIDEOS/CATEGORY/
   VIDEO2, whereas I want it to be displayed in NEWS/DECEMBER 2014/VIDEO2.
 * Is it possible to do that?
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/get_posts-by-months/#post-5664470)
 * Don’t worry about response time on your part – it’s your project not getting 
   fixed 😉
 * Thanks for the explanation. If I understand your “problem” declaration correctly,
   your menu script is outputting the wrong permalink for the listed posts. Is that
   right, or are you saying the permalinks are correct but WP redirects such that
   after clicking the correct link, the user is taken to the post page with a different
   URL displayed that you do not want?
 * If it is the former condition, the problem would be where you are using `the_permalink()`
   for the href attribute in generating the anchor links. You should instead use
   the current `monthnum` and post slug combined with other data from the post object
   to build the appropriate URL. This example has several errors in variable names,
   it is just to illustrate the concept, you can’t plug this example into your code
   directly:
 *     ```
       $href = get_site_url("/news/{$posts->year}/{$month}/{$posts->name}");
       echo "<a href='$href'>$title</a>";
       ```
   
 * `$href` in this case would look something like `http://example.com/news/2014/
   12/video-title` which doesn’t quite match your desired format, but it does sort
   of follow one of the standard permalink formats. This is more likely to return
   the correct post than the format you suggested unless you’ve taken measures to
   define a custom permalink. Even just the ‘news’ term in my example could confuse
   WP unless it’s been defined as a permalink tag.
 * I suspect you have more work to do, but I hope this gets you one step closer 
   to your desired user experience.
 *  Thread Starter [Agence Myso](https://wordpress.org/support/users/agence-myso/)
 * (@agence-myso)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/get_posts-by-months/#post-5664471)
 * Hehe, I’m sorry I have to tell you your second hypothesis was the good one…
    
   Let me be more precise. The same posts have to be called by two different ways:
   as category items or as dated items. Why is that? Because I have built a three-
   level-menu which is always visible. If you go to NEWS / OCTOBER 2014, you will
   have three columns, the last one displaying a list of posts which you will also
   be able to find in their own categories in VIDEOS / CATEGORIES /POSTS or PRINT/
   CATEGORIES / POSTS. So, when I call `the_permalink();` in my NEWS / MONTH / list
   of posts, I have my list with good permalinks. The problem is that `the_permalink();`
   brings the user to the corresponding category, whereas I would like to stay in
   NEWS / OCTOBER / POST.
 * As a good code example is always better than tons of words, let me present you
   the content of my `date.php` :
 *     ```
       <?php get_header(); ?>
   
       <div class="navigation col-lg-4 col-md-4 col-xs-12">
   
                       <div class="menu1 col-lg-4 col-md-4 col-xs-4">
   
                           <?php
                                $args = array(
                                   'theme_location' => 'header-menu'
                               );
                               wp_nav_menu($args);
                           ?>
                       </div>
   
                       <div class="menu2 col-lg-4 col-md-4 col-xs-4">
   
                       <?php
                       $args = array(
                               'type'            => 'monthly',
                               'post_type'       => 'video',
                               'limit'           => '',
                               'format'          => 'html',
                               'before'          => '',
                               'after'           => '',
                               'show_post_count' => false,
                               'echo'            => 1,
                               'order'           => 'DESC'
                           );
                       wp_get_archives_cpt($args); ?>
   
                       </div>
   
                       <div class="menu3 col-lg-4 col-md-4 col-xs-4">
   
                       <?php 
   
                       /* var_dump($GLOBALS['wp_query']); */
   
                       $month = get_the_time('m');
   
                               $args = array(
                                   'post_type'  => array('video'),
                                   'order'      => 'ASC',
                                   'date_query' => array(
                                       array(
                                           'month' => $month
                                       ),
                                   )
                               );
   
                               $the_query = new WP_Query($args);
                               if ( $the_query->have_posts() ) :
                               echo '<ul>';
                                   while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                                           <li><a class="post-link" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                                       <?php
                                   endwhile;
                               echo '</ul>';
                               endif; ?>             
   
                       </div>          
   
        </div>
       ```
   
 *  Thread Starter [Agence Myso](https://wordpress.org/support/users/agence-myso/)
 * (@agence-myso)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/get_posts-by-months/#post-5664472)
 * Just to let you know.
 * As I am trying different approches, I am also trying to display the videos with
   Ajax (which won’t give me the full URL, but will allow me to display the video
   without getting out of the NEWS section).
 * I am calling the videos (posts) this way :
 *     ```
       <?php 
   
       if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { ?>
   
       <?php while (have_posts()) : the_post(); ?>
   
                       <?php $next_vid = get_permalink(get_adjacent_post( true, '', true )); ?>
   
                       <?php get_the_date(); ?>
   
       	<video id="my-video-js" class="video-js vjs-default-skin vjs-big-play-centered" controls preload="auto" autoplay
       		data-setup='{}' width="100%" height="auto">
       			<source src="<?php echo get_field('source_mp4')['url']; ?>" type="video/mp4">
       			<source src="<?php echo get_field('video_webm')['url']; ?>" type="video/webm">
       	</video>
   
       	<?php endwhile;?>
       	<?php die();
       }
       ```
   
 * But it doesn’t work… the `video-container`div loads, the `my-video-js` also. 
   But it doesn’t load the video. I can’t get it to load the link to the videos…
 *  Thread Starter [Agence Myso](https://wordpress.org/support/users/agence-myso/)
 * (@agence-myso)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/get_posts-by-months/#post-5664473)
 * Ok, now it works…
    I just used an underscore in place of a dash in `get_field('
   video-webm') etc.` After hours of searching a solution, it’s hard to focus on
   details…
 * But if anyone has an idea about how to display posts by two ways (without using
   Ajax) I am still very interested…
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/get_posts-by-months/#post-5664479)
 * I can’t tell you how much time I’ve wasted on dash/underscore confusion, so writing
   a few paragraphs answering the wrong question is nothing 🙂
 * WP seems to insist on rewriting address bar URLs under all conditions. If there’s
   a way to stop it, I’ve not found it. A few times I’ve resorted to using .htaccess
   rewrites. Relative URLs are not rewritten that way. I don’t know if .htaccess
   will work for you, but it’s about all I can offer.
 * I need to present an example without code because I suck at .htaccess syntax.
   I would hate for something I gave you to bork your site. The RewriteCond will
   identify requests that contain, say, “/NEWS/”. The corresponding RewriteRule 
   would extract the post slug from the original request and rewrite it in the form`/
   index.php?name=post-slug-here`
 * That’s enough to get the right post, but the original request remains in the 
   address bar.
 *  Thread Starter [Agence Myso](https://wordpress.org/support/users/agence-myso/)
 * (@agence-myso)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/get_posts-by-months/#post-5664480)
 * That’s a smart proposal !
    Let’s try this and I’ll tell you. Thanks a lot !

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

The topic ‘`get_posts` by months?’ is closed to new replies.

## Tags

 * [date](https://wordpress.org/support/topic-tag/date/)
 * [get_posts](https://wordpress.org/support/topic-tag/get_posts/)
 * [month](https://wordpress.org/support/topic-tag/month/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 13 replies
 * 3 participants
 * Last reply from: [Agence Myso](https://wordpress.org/support/users/agence-myso/)
 * Last activity: [11 years, 3 months ago](https://wordpress.org/support/topic/get_posts-by-months/#post-5664480)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
