admin管理员组

文章数量:1125590

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.

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.

Share Improve this question edited Nov 20, 2014 at 0:50 ᴍᴇʜᴏᴠ asked Nov 20, 2014 at 0:17 ᴍᴇʜᴏᴠᴍᴇʜᴏᴠ 4021 gold badge4 silver badges15 bronze badges 6
  • similar but not helpful: wordpress.stackexchange.com/questions/166564/… – ᴍᴇʜᴏᴠ Commented Nov 20, 2014 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 Commented Nov 20, 2014 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 – ᴍᴇʜᴏᴠ Commented Nov 20, 2014 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 Commented Nov 20, 2014 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 – ᴍᴇʜᴏᴠ Commented Nov 20, 2014 at 13:50
 |  Show 1 more comment

3 Answers 3

Reset to default 15

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.

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 );

Get posts by category id

$categories = get_categories();
$loop = new WP_Query([
    'posts_per_page' => 6,
    'post_type' => 'post',
    'tax_query' => [
        [
            'taxonomy' => 'category',
            'field' => 'id',
            'terms' => $categories[0]->term_id
        ],
    ],
]);

本文标签: categoriesWPQuery by a category id and a custom posttype