admin管理员组文章数量:1404567
I need to get post from blog, custom post type 1, custom post type 2. custom post type 3 and either from category 1 or category 2 or category 3 or category 4 or category 5 or category 6 or category 7 or category 8. I have this but isn't working. I get no results, no errors either.
<?php
function filter_where_categs() {
$where .= " AND (category_name => array('black-star-crescent-moon', 'audio', 'blackward', 'ghost', 'protect', 'remember', 'return', 'video'))";
return $where;
}
$args=array(
'post_type'=> array( 'test-bscm', 'post', 'test-btum', 'test-other' ),
'posts_per_page' => 19
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
add_filter('posts_where', 'filter_where_categs');
$wp_query->query($args);
?>
I need to get post from blog, custom post type 1, custom post type 2. custom post type 3 and either from category 1 or category 2 or category 3 or category 4 or category 5 or category 6 or category 7 or category 8. I have this but isn't working. I get no results, no errors either.
<?php
function filter_where_categs() {
$where .= " AND (category_name => array('black-star-crescent-moon', 'audio', 'blackward', 'ghost', 'protect', 'remember', 'return', 'video'))";
return $where;
}
$args=array(
'post_type'=> array( 'test-bscm', 'post', 'test-btum', 'test-other' ),
'posts_per_page' => 19
);
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
add_filter('posts_where', 'filter_where_categs');
$wp_query->query($args);
?>
Share
Improve this question
edited Aug 4, 2012 at 10:52
Amit Kosti
3,6101 gold badge22 silver badges34 bronze badges
asked Aug 4, 2012 at 10:33
user1569898user1569898
111 silver badge3 bronze badges
1
- are you using the default category taxonomy for all your custom post types? – Jan Beck Commented Aug 4, 2012 at 10:51
1 Answer
Reset to default 1According to the codex category_name only takes one string value. category_in
accepts an array of category IDs:
<?php
$args = array(
'category__in' = array(1,4,6,8), // use IDs
'post_type'=> array( $ptype, 'post', 'test-btum', 'test-other' )
);
$wp_query = new WP_Query($args); ?>
It might be possible that the default category taxonomy is not registered to your custom post type. In that case try adding
register_taxonomy_for_object_type( 'category', 'test-btum' )
to your themes functions.php
. Repeat for every post type you want to register. Make sure your custom post types are actually assigned to a category otherwise they won't show up in your query.
本文标签: forming WPQuery for posts of all post types but from specific categories
版权声明:本文标题:forming WP_Query for posts of all post types but from specific categories 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744838357a2627769.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论