Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This question already has an answer here:

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.

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?

share|improve this question
add comment

marked as duplicate by Chip Bennett, s_ha_dum, kaiser, toscho Oct 31 '13 at 14:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

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.

share|improve this answer
    
+1. Never EVER EVER EVER use query_posts(). It's amazing how easily pagination issues disappear when replacing query_posts() with a proper pre_get_posts filter. –  Chip Bennett Oct 1 '13 at 14:03
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.