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.

2 s
2

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.

Leave a Reply

Your email address will not be published. Required fields are marked *