I have a custom post type myposttype
and it’s taxonomy is called myposttype_categories
.
myposttype_categories
have multiple terms inside, such as foo
and bar
.
The tricky question is – how do I list all myposttype posts that belong to foo
(or bar
)?
I thought this should work, but it does not:
$args = array(
'post_type' => 'myposttype',
'myposttype_categories'=> 'foo');
$loop = new WP_Query( $args );
And the loop is always empty.
I’ve tried possibly every Taxonomy paramter for WP_Query() also checked Category parameters.
There are very old posts addressing the issue issue, but it seems it still doesn’t work after 3 years…? Or am I missing something?
http://wordpress.org/support/topic/wp_query-and-custom-taxonomies
http://core.trac.wordpress.org/ticket/13582
[edit]
That’s how I register my post type:
add_action('init', 'myposttype_register');
function myposttype_register() {
$labels = array(
'name' => _x('Myposttype', 'post type general name'),
'singular_name' => _x('Myposttype item', 'myposttype item'),
'add_new' => _x('Add Myposttype', 'myposttype item'),
'add_new_item' => __('Add New Item'),
'edit_item' => __('Edit Item'),
'new_item' => __('New Item'),
'view_item' => __('View Item'),
'search_items' => __('Search Items'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'menu_icon' => get_stylesheet_directory_uri() . '/article16.png',
'rewrite' => true,
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','editor','thumbnail','page-attributes','comments','trackbacks'),
'show_in_nav_menus' => true,
);
register_post_type( 'myposttype' , $args );
}
And taxonomy:
register_taxonomy("myposttype_categories", array("myposttype"), array("hierarchical" => true, "label" => "Categories", "singular_label" => "Type", "rewrite" => true));
What about doing a
tax_query
?Facepalm question, you are sure that these taxonomies/post types exist and that there are posts filed under them?
Update
The query seems to work fine for me, and I am able to show a list of the posts I have added with that term/category. I moved your
register_taxonomy
call into the function that fires oninit
. Per the codex it is inadvisable to callregister_taxonomy
outside of an action and could be the cause of your troubles.And the query:
For what its worth, both of the following also work as arguments, though since you are only querying a single taxonomy you probably don’t need to use the
tax_query
. I used that initially because I thought you needed to find posts in both terms.and