Pagination shows empty pages for date based archiv

2019-09-06 12:56发布

问题:

I'm using wordpress and have a custom archive page for a custom post type. The custom loop gets the logged in users registration date and only shows posts that were published on or after they registered. Works great.

However, the pagination is still using the main query so if there are 4 posts in total set to 2 posts per page the pagination always shows there are two pages even if only one post is displayed due to the logged in users registered date.

Can anyone help me modify what I have so the pagination only shows for results in more than 2 posts for that users query? I've been trying for hours now using various changes I've found on the web...

<?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; ?>

回答1:

Working with Custom Archive and Pagination

@Scott Eldo Your approach with create custom query will not change main query on your custom archive. FYI and you are correct, pagination only work for main query.

In your case, the recommended approach, I will use filter 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, you can figure it out with your query parameters.

BUT if you intent to create query direct into your template ( even it is not change main query ), you need to match your custom query with $GLOBALS['wp_query'] that use in pagination and don't forget to use wp_reset_query() ( MUST ). Take a look my approach here related with your code:

<?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

Another approach and still NOT recommended is use query_post for your custom query. More reading about it you can learn more in here.