forming WP_Query for posts of all post types but from specific categories

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

?>

Related posts

Leave a Reply

1 comment

  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.