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
Add a comment  | 

1 Answer 1

Reset to default 1

According 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