Pagination for Custom Taxonomy Page

I’m having an issue getting my custom taxonomy page to paginate correctly. I looked through other examples posted on this forum, but I could not get my second page to not return a 404 error page. Here are my details

  • Custom Tax: Study_Tags
  • Example Terms: Community, Economy, Crime…
  • Custom Taxonomy Page: taxonomy-study_tags.php
  • Posts Per Page: 5 (set in Reading Settings, global for all pages/posts on site)

I am using a Custom Post Type “Study” with the custom taxonomy “Study Tags”, which is why i feel I need to use the “query_posts()” function.

Read More

I only want to show parent pages in the results, as each “Study” CPT can have children page with study details, presentations… etc.

My information to setup the query

<?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args = array_merge( $wp_query->query_vars, 
    array(
            'post_type'=> 'study',
            'post_parent' => '0',
            'paged' => $paged,
            'order' => 'ASC',
            ));
            query_posts($args);

?>

$wp_query->query_vars passes for example “[study_tags] => community” when var_dump() is used, so I know it is transmitting the correct term to the custom tax page.

Any ideas how to get a second page of results?

Related posts

1 comment

  1. First of all: never use query_posts: when you need to change the main query, is a lot better use the pre_get_posts hook: your site users will thank you, once performance will increase.

    If the custom taxonomy study_tags is used only for study CPT the only reason to change default query is to set post parent to 0.

    add_action('pre_get_posts','set_study_parent');
    
    function set_study_parent( $query ) {
      if ( ! is_admin() && is_main_query() && is_tax('study_tags') ) {
        $query->set( 'post_parent', 0 );
      }
    }
    

    Then in your taxonomy-study_tags.php template, you can just use the loop. Pagination will work without doing anything else.

Comments are closed.