1

I have a custom post type called "results" and on my single results page I want to perform a second query after the main loop. The second query is as follows:

$myquery = new WP_Query(array(
 'post_type' => 'endorsements',
   'tax_query' => array(
     array(
     'taxonomy' => 'results_categories',
     'field'    => 'slug',
     'terms' => array( 'facilitative-leaders', 'organization' )
     )
  ),
));

I made a second loop with the above query and what I get back are posts that are in the "results" custom post type, NOT in the 'endorsements' post type as I specified.

If I take out all the tax_query info and only ask for the 'endorsements' it works, but I need the tax info.

Far as I can see my query is correct.. been stuck on this one for a while!!

Any help is appreciated!

4
  • is your post type registered with exclude_from_search set to true? if you set posts_per_page to -1, do you see any endorsements in the returned set? what happens when you add post type instead as an array: 'post_type' => array('endorsements')?
    – Milo
    Commented Nov 2, 2014 at 21:44
  • @Milo exclude_from_search is set to false, your other recommendations still do not return posts from 'endorsements' unfortunately. Is there any other info I can provide that may help?
    – mToce
    Commented Nov 2, 2014 at 22:21
  • 1
    var_dump($myquery) after you run it and see what the query vars look like. I'll guess you have some code somewhere modifying the query, maybe pre_get_posts.
    – Milo
    Commented Nov 2, 2014 at 22:24
  • @Milo Yes, you were right, I was using pre_get_post to exclude some things, but I forgot to add && $query->is_main_query(). Thanks!!
    – mToce
    Commented Nov 3, 2014 at 12:56

1 Answer 1

2

I found out that I was using pre_get_post on these pages to exclude some things on the taxonomy archive pages.

add_action( 'pre_get_posts', 'exclude_cpt' );
function exclude_cpt( $query ) {
    if ( $query->is_tax('results_categories') ) {
         $query->set( 'post_type', array('results') );
    }
    return $query;
}

BUT I forgot to make sure this was only happening in the main query, the final function is below.

add_action( 'pre_get_posts', 'exclude_cpt' );
function exclude_cpt( $query ) {
    if ( $query->is_tax('results_categories') && $query->is_main_query() ) {
         $query->set( 'post_type', array('results') );
    }
    return $query;
}
3
  • Just a note here, always do a check for admin pages as well. pre_get_posts also alters the back end queries, so be carefull with this. Simply just add !is_admin() like this if ( !is_admin() && query->is_tax('results_categories') && $query->is_main_query() ) { Commented Nov 3, 2014 at 15:01
  • @PieterGoosen Thanks for noting that very helpful to all that will read this post!
    – mToce
    Commented Nov 4, 2014 at 15:42
  • Will upvote this answer if you make that change :-) Commented Nov 4, 2014 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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