0

I've been banging my hand against the wall for a little while now trying to figure this out.

Can anyone explain to me why this custom wp_query prduces two different sql statements, one for the index page (which is correct) and one for the category page (which is wrong)

function myFunction( $category )
{
    if(!$category) return;

    $query = array(
        'post_type' => 'download',
    );

    $query['tax_query'][] = array(
        'taxonomy' => 'download_category',
        'terms' => explode( ',', $category ),
        'field' => 'slug'
    );

    $downloads = new WP_Query( $query );

    return intval($downloads->found_posts);
}

Index sql (correct):

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (20) ) AND wp_posts.post_type = 'download' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'refunded' OR wp_posts.post_status = 'failed' OR wp_posts.post_status = 'revoked' OR wp_posts.post_status = 'abandoned' OR wp_posts.post_status = 'active' OR wp_posts.post_status = 'inactive' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10

Category sql (wrong):

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (15) ) AND wp_posts.post_type IN ('nav_menu_item', 'post', 'eddset') AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'refunded' OR wp_posts.post_status = 'failed' OR wp_posts.post_status = 'revoked' OR wp_posts.post_status = 'abandoned' OR wp_posts.post_status = 'active' OR wp_posts.post_status = 'inactive' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 10

It is ignoring the post_type but can't figure out why?

Any help much appreciated.

1 Answer 1

1

I can only assume that there is filter running on the category page that is overwriting your post type.

  1. Try adding 'suppress_filters'=>true to your query arguments, but that only works for some filters not all. I suspect it will not work in this case.
  2. Or find the filter that is altering the query and modify it so that it does not overwrite existing post_type arguments, which is what I assume the filter does.

I expect that option #2 is going to be the solution.

1
  • Thanks s_ha_dum, you are spot on. This is why you shouldn't code when tired :)
    – Fyrebase
    Commented Nov 28, 2013 at 21:07

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.