• 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 to wp_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 using date_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??

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Hello AM, back for more, eh? ??

    I think what you need to do is add an else : condition to if ( $the_query->have_posts() ) :. Here you would do a new query where you get the posts from only the previous month by using $month-1 in the date query.

    You probably should also output something like “Nothing for this month yet, here’s last month’s posts” as an explanation of why the user sees we are in February but is seeing January posts.

    This assumes there are enough posts each month that a previous month query will always return something. If there are likely months without any posts, organizing archives by month may not be the best strategy.

Viewing 1 replies (of 1 total)
  • The topic ‘Loading posts only from last month with posts’ is closed to new replies.