分页显示基于日期 archive.php 循环的空页

Pagination shows empty pages for date based archive.php loop

我正在使用 wordpress,并且有一个自定义 post 类型的自定义存档页面。 自定义循环获取登录用户的注册日期,并且仅显示在他们注册时或之后发布的 posts。效果很好。

但是,分页仍在使用主查询,因此如果总共有 4 post 设置为每页 2 post 分页总是显示有两个页面,即使只有由于登录用户的注册日期,显示一个post。

任何人都可以帮我修改我的内容,以便分页只显示超过 2 post 的用户查询的结果吗?我已经使用我在网上找到的各种更改尝试了几个小时...

<?php if ( have_posts() ): ?>
        <?php
        # Get the current user's info
        $user_info = get_userdata(get_current_user_id());
        # Use date_parse to cast your date to an array 
        $regdate = date_parse($user_info->user_registered);
        # Set your arguments for WP Query    

        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

        $args = array(
            'post_type'  => 'inner',
            'posts_per_page'         => '2',
            'posts_per_archive_page' => '2',
            'paged'          => $paged,
            'date_query' => array(
                array(
                    'after'    => array(
                        # Setting date to array above allows to call specific values within that date    
                        'year'  => $regdate['year'],
                        'month' => $regdate['month'],
                        'day'   => $regdate['day'],
                    ),
                    # Include posts from the day the user registered  
                    'inclusive' => true,
                ),
            ),
            # Display all posts on a single page.
        );          
        $my_query = new WP_Query( $args );
        while ($my_query->have_posts()) : $my_query->the_post(); 

        get_template_part( 'template-parts/content', get_post_format() );

        endwhile; ?>

        <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
        <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>

        <?php else: ?>
            Nada
        <?php endif; ?>

使用自定义存档和分页

@Scott Eldo 您创建自定义查询的方法不会更改自定义存档上的主查询。仅供参考,您是正确的,分页仅适用于主查询。

在你的情况下,推荐的方法,我会使用过滤器 pre_get_posts to work with custom archive and pagination. Please take a look my answer here how to modify main query on post type archive page,你可以用你的查询参数来计算。

但是如果您打算直接在您的模板中创建查询(即使它没有更改主查询),您需要将您的自定义查询与分页中使用的 $GLOBALS['wp_query'] 匹配并且不要忘记使用wp_reset_query()(必须)。看看我在这里与您的代码相关的方法:

<?php if ( have_posts() ): ?>
    <?php
    # Get the current user's info
    $user_info = get_userdata(get_current_user_id());
    # Use date_parse to cast your date to an array 
    $regdate = date_parse($user_info->user_registered);
    # Set your arguments for WP Query    

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    $args = array(
        'post_type'  => 'inner',
        'posts_per_page'         => '2',
        'posts_per_archive_page' => '2',
        'paged'          => $paged,
        'date_query' => array(
            array(
                'after'    => array(
                    # Setting date to array above allows to call specific values within that date    
                    'year'  => $regdate['year'],
                    'month' => $regdate['month'],
                    'day'   => $regdate['day'],
                ),
                # Include posts from the day the user registered  
                'inclusive' => true,
            ),
        ),
        # Display all posts on a single page.
    );          
    $my_query = new WP_Query( $args );
    while ($my_query->have_posts()) : $my_query->the_post(); 

        get_template_part( 'template-parts/content', get_post_format() );

    endwhile; ?>
    <?php
        /**
         * Fix Pagination in custom page/archive
         * Set global wp_query the same as our custom query
         * 
         * Use function the_posts_pagination( $args ); for pagination will work too
         * see https://developer.wordpress.org/reference/functions/get_the_posts_pagination/#source-code
        */
        $GLOBALS['wp_query'] = $my_query;
    ?>
    <div class="nav-previous alignleft"><?php next_posts_link( 'Older posts' ); ?></div>
    <div class="nav-next alignright"><?php previous_posts_link( 'Newer posts' ); ?></div>
    
<?php else: ?>
    Nada
<?php endif; ?>
<?php wp_reset_query(); ?> // MUST use to reset global query and post data

另一种仍然不推荐的方法是使用 query_post for your custom query. More reading about it you can learn more in here