Coplan Consulting
Coplan Consulting

Reputation: 19

WP_Query Pagination Returns Extra pages

I have a wp_query of a custom taxonomy along with a meta value (ACF), the pagination seems to be adding extra pages which end up as a 404 error. When I do a print_r I can see the query returns the correct value, [found_posts] => 44 [max_num_pages] => 4, there is a total of 96 items in the taxonomy, but when filtered by the meta value it is 44, why would the pagination not take the 44

here is the query code:

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

$args = array(
    'posts_per_page'    =>  12,
    'post_type'         => 'genera',
    'meta_key'          => 'species_or_hybrid',
    'meta_value'        => $cat_type,
    'tax_query'         => array(
        array(
            'taxonomy' => 'genus',
            'field'    => 'slug',
            'terms'    => wp_get_post_terms($post->ID, 'genus', array("fields" => "names"))
        ),
    ),
    'paged'         =>  $paged,
);

// get results
$the_query = new WP_Query( $args );

Upvotes: 0

Views: 1680

Answers (2)

thephper
thephper

Reputation: 2610

As @stellarcowboy points out, this is a problem with wordpress query and can be fixed by adjusting the total posts per page from wp-admin -> settings -> reading.

But I found that the found_posts and max_num_pages values were actually correct, but the WP_Query wasn't able of showing content of the last pages with posts until the number in wp-admin was corrected.

Upvotes: 0

stellarcowboy
stellarcowboy

Reputation: 111

In the WordPress admin, Settings > Reading, set your default number of posts shown to less than the amount of posts in your query. This is caused by the global $query variable returning the wrong number of total posts.

https://codex.wordpress.org/Pagination

It's also possible you are running pagination against the primary loop and not your custom, secondary loop. The answer to that issue depends on which tool you are using to output your pagination links. You could try putting your navigation links inside the secondary loop vs. the primary loop.

Upvotes: 1

Related Questions