Home > Back-end >  Wordpress Custom Post Type: Pagination reloads same content on every page
Wordpress Custom Post Type: Pagination reloads same content on every page

Time:04-15

So I have a custom post type called broadcast and it is currently spitting out 10 posts a page onto an archive page. However, when you switch pages via pagination, the URL changes to page/2/ and the current button at the bottom changes, but the same 10 posts remain the same. So the pagination side of things seems to be working, it just isn't loading in different posts. Here's the code (this might be too much, but better to have more context than less):

<?php
// WP_Query arguments
$args = array(
    'post_type' => array('broadcast'),
    'post_status' => array('publish'),
    'nopaging' => false,
    'offset' => 1,
    'order' => 'DESC',
    'orderby' => 'date',
    'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
    'tax_query' => array(
        array(
            'taxonomy' => 'format',
            'terms' => 'videos',
            'field' => 'slug'
        )
    )
);

// The Query
$broadcasts = new WP_Query($args);

// The Loop
if ($broadcasts->have_posts()) {
    while ($broadcasts->have_posts()) {
        $broadcasts->the_post(); ?>

        // SHOW THE POSTS HERE

    <?php } ?>

    <!-- Start Pagination-->
    <div >
        <?php

        echo paginate_links(array(
            'base' => str_replace(999999999, '%#%', esc_url(get_pagenum_link(999999999))),
            'total' => $broadcasts->max_num_pages,
            'current' => max(1, get_query_var('paged')),
            'format' => '?paged=%#%',
            'show_all' => false,
            'type' => 'list',
            'end_size' => 2,
            'mid_size' => 1,
            'prev_next' => true,
            'prev_text' => sprintf('<i></i> %1$s', __('Newer Videos', 'text-domain')),
            'next_text' => sprintf('%1$s <i></i>', __('Older Videos', 'text-domain')),
            'add_args' => false,
            'add_fragment' => '',
        ));
        ?>
    </div>

    <?php
} else {
    echo 'there are no posts.'; // no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>

Any help is appreciated, I've gotten nowhere reading docs, as this * should * work and it isn't throwing errors for me to debug. Thanks!

CodePudding user response:

The reason of your issue is offset parameter.

When offset parameter is set, paged parameter will be ignored.

Check WP_Query parameters.

https://developer.wordpress.org/reference/classes/wp_query/#post-page-parameters

offset (int) – number of post to displace or pass over. Warning: Setting the offset parameter overrides/ignores the paged parameter and breaks pagination. The 'offset' parameter is ignored when 'posts_per_page'=>-1 (show all posts) is used.

I think you are trying to skip the latest post.

In this case you can use the following code.

$latest_post_args = array(
    'post_type'     => 'broadcast',
    'numberposts'   => 1,
    'fields'        => 'ids',
    'tax_query' => array(
        array(
            'taxonomy' => 'format',
            'terms' => 'videos',
            'field' => 'slug'
        )
    )
);

$latest_post_ids = get_posts( $latest_post_args );

$args = array(
    'post_type' => array('broadcast'),
    'post_status' => array('publish'),
    'nopaging' => false,
    'post__not_in' => $latest_post_ids,
    'order' => 'DESC',
    'orderby' => 'date',
    'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1,
    'tax_query' => array(
        array(
            'taxonomy' => 'format',
            'terms' => 'videos',
            'field' => 'slug'
        )
    )
);
  • Related