Loading posts only from last month with posts
-
Hi there,
The topic may seem to be a little odd, but this is my problem.
I have quite an unusual menu composed of three columns :
1st level : a typical menu (created thanks towp_nav_menu
, first item is ‘NEWS’.
2nd level :wp_get_archives
(corresponding to the item ‘NEWS) displaying a list of the 12 last months.
3rd level : the posts corresponding to each month (wp_query
usingdate_query
).When you get to the homepage, you are directly getting to the NEWS section.
There, the second column displays the list of months, no problem. Then, third column displays the list of posts.
This works as soon as you are in the current month. As soon as you are for example in february, but your last posts were posted in january, no list of posts is created…To be clearer, I’ll add my code :
if ( is_home() || is_page() ) { ?> <div class="navigation col-lg-4 col-md-6 col-sm-9 col-xs-12"> <div class="menu1 auto-current col-lg-4 col-md-4 col-sm-4 col-xs-12"> <?php $args = array( 'theme_location' => 'header-menu' ); wp_nav_menu($args); ?> </div> <div class="menu-mask"> <div class="menu2 auto-current col-lg-4 col-md-4 col-sm-4 col-xs-6"> <?php $args = array( 'type' => 'monthly', 'post_type' => 'video', 'limit' => '12', 'format' => 'html', 'before' => '', 'after' => '', 'show_post_count' => false, 'echo' => 1, 'order' => 'DESC' ); wp_get_archives_cpt($args); ?> </div> <div class="menu3 auto-current col-lg-4 col-md-4 col-sm-4 col-xs-6"> <?php /* var_dump($GLOBALS['wp_query']); */ $month = get_the_time('m'); $year = get_the_time('Y'); $args = array( 'post_type' => array('video'), 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => -1, 'date_query' => array( array( 'month' => $month, 'year' => $year ), ) ); $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> </div> <div class="video-container col-lg-8 col-md-6 col-xs-12"> <?php $args = array( 'post_type' => 'video', 'numberposts' => 1, 'orderby' => 'post_date', 'order' => 'DESC' ); $recent_posts = get_posts( $args ); if ($recent_posts) { foreach ($recent_posts as $post) : $post_ID = get_the_ID(); /*echo $post_ID.' '; */ $next_vid = get_permalink(get_adjacent_post( true, '', true )); ?> <video id="my-video-js" class="video-js vjs-default-skin vjs-big-play-centered" controls preload="auto" autoplay data-setup='{}' width="auto" height="auto"> <source src="<?php echo get_field('video_mp4'); ?>" type="video/mp4"> <source src="<?php echo get_field('video_webm'); ?>" type="video/webm"> </video> <?php endforeach; } ?> </div> <?php } get_template_part('partials/video-grid-date'); get_footer(); ?>
See what my problem is ???
I need to display the last posts from the last month with post…
In other words, I need something like : ‘hey WP, please look at the last month which has posts and get all of them, but only them, not the ones from the previous months.”Any idea??
- The topic ‘Loading posts only from last month with posts’ is closed to new replies.