1

Can't get my WP_Query loop to work, cannot figure out why category__in won't work. Just want to pull certain categories FROM my custom post type

$args = array(
  'post_type' => 'bbt',
  'category__in' => array(90,89)
);

$loop = new WP_Query($args);

when I use echo $loop->found_posts; it does returns 0, BUT I have 2 posts in total from those categories

EDIT: Here is how the category was assigned:

    $labels = array(
    'name' => _x( 'Big Boys Toys Categories', 'taxonomy general name' ),
    'singular_name' => _x( 'Tag', 'taxonomy singular name' ),
    'search_items' =>  __( 'Search Types' ),
    'all_items' => __( 'All Tags' ),
    'parent_item' => __( 'Parent Tag' ),
    'parent_item_colon' => __( 'Parent Tag:' ),
    'edit_item' => __( 'Edit Tags' ),
    'update_item' => __( 'Update Tag' ),
    'add_new_item' => __( 'Add New Tag' ),
    'new_item_name' => __( 'New Tag Name' ),
);

// Register Custom Taxonomy
register_taxonomy('tagbbt',array('bbt'), array(
    'hierarchical' => true, // define whether to use a system like tags or categories
    'labels' => $labels,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'tag-bbt' ),
));
  • Is your bbt post type assigned to category taxonomy? – Nilambar Oct 27 '14 at 6:05
  • Please file an edit with the code used to assign the category taxonomy to your post type – Pieter Goosen Oct 27 '14 at 6:18
  • @PieterGoosen cheers, I just added the code for that into my answer – rand_user91 Oct 27 '14 at 6:21
4

category__in will never work as you aren't making use of the build in taxonomy category. You are actually making use of a custom taxonomy called tagbbt. Take a look at this post, I have explained what the differences are

For custom taxonomies, you need to make use of a tax_query

Here is an example

$args = array(
    'post_type' => 'bbt',
    'tax_query' => array(
        array(
            'taxonomy' => 'tagbbt',
            'field'    => 'term_id',
            'terms'    => array(90,89),
        ),
    ),
);
$query = new WP_Query( $args );
  • Excellent, this is exactly what is required. I will definitely read up on your post to learn more of the differences in Taxonomies/Custom Taxonomies, thank you so much – rand_user91 Oct 27 '14 at 6:37
  • My pleasure. Enjoy :-) – Pieter Goosen Oct 27 '14 at 6:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.