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 | Show 1 more comment3 Answers
Reset to default 15try 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
版权声明:本文标题:categories - WP_Query by a category id and a custom post_type 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736665980a1946664.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$cat_id
? you canvar_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$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