9

I need to query all posts that belong to a given category (default, not custom) and a custom post type. As simple as that. The fact that it doesn't work, to me, is ridiculous. Unless I'm missing something?

Here's what I've tried:

$args=array(
    'posts_per_page' => 50, 
    //'taxonomy' => 'category',      
    'post_type' => 'my_custom_type'
    'category__in' => array($cat_id),
);
$wp_query = new WP_Query( $args );

then

$args=array(
    'posts_per_page' => 50,    
    'post_type' => 'my_custom_type'
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field'    => 'id',
            'terms'    => $cat_id,
        ),
    ),
 );
$wp_query = new WP_Query( $args );

and of course

$args=array(
    'posts_per_page' => 50, 
    'post_type' => 'my_custom_type'
    'category' => $cat_id,
);
$wp_query = new WP_Query( $args );

also, some combinations of adding/renaming/removing the $args keys.

Getting all posts by a post type and then looping through them and filtering by a category is not an effective option, I believe.

Please help.

6
  • similar but not helpful: wordpress.stackexchange.com/questions/166564/… – aexl Nov 20 '14 at 0:23
  • well your first one has a typo. I assume you have debugging enabled and aren't seeing any errors? where do you set $cat_id? you can var_dump your query after and look at the generated SQL, it should reveal the issue. also, you probably shouldn't be overwriting $wp_query. – Milo Nov 20 '14 at 0:39
  • @Milo thanks ok the $cat_id is actually an array key (this is where the single quote came from), I've just edited it to be a simple variable so this question looks less messy. Thanks for pointing out though. As for not overwriting the $wp_query - I'll keep that in mind, thanks – aexl Nov 20 '14 at 0:52
  • 1
    Still appear to be syntax errors there Jamaica. Missing comma in your array should throw a PHP parse error. Can you post the actual code you're using w/o edits? – jdm2112 Nov 20 '14 at 2:32
  • @jdm2112 Shame on me, that was the comma. This explains why I was getting nothing when trying to uncomment the category-related keys. I would ask you to post the suggestion as an answer so I can accept and upvote, but I am not sure if this will be useful for the community. I've upvoted your other answers instead. I will delete this question a bit later. Thank you – aexl Nov 20 '14 at 13:50
10

try this, it's work for me.

    $args=array(
    'posts_per_page' => 50, 
    'post_type' => 'my_custom_type'
    'cat' => $cat_id,
);
$wp_query = new WP_Query( $args );

Category Parameters

cat (int): use category id.
category_name (string): use category slug (NOT name).
category__and (array): use category id.
category__in (array): use category id.
category__not_in (array): use category id.
1
  • Hi @kunal-gauswami, welcome to the site and thank you for your reply. Unfortunately, I don't have the code anymore so I can't try your suggestion, but if someone confirms that it worked, I will happily accept the answer. Here's an upvote from me anyway. – aexl Sep 16 '16 at 8:23
5

this worked for me.

$args=array(
'posts_per_page' => 50,    
'post_type' => 'my_custom_type'
'tax_query' => array(
    array(
        'taxonomy' => 'category', //double check your taxonomy name in you dd 
        'field'    => 'id',
        'terms'    => $cat_id,
    ),
   ),
 );
$wp_query = new WP_Query( $args );
1
  • This answer is correct if you are doing a query for a custom taxonomy linked to the custom post type – Low Jan 22 '20 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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