What about doing a tax_query
?
$args = array(
'post_type' => 'myposttype',
'tax_query'=> array(
'taxonomy' => 'myposttype_categories',
'terms' => array('foo'),
'field' => 'slug',
)
);
$loop = new WP_Query( $args );
var_dump($loop);
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 on init
. Per the codex it is inadvisable to call register_taxonomy
outside of an action and could be the cause of your troubles.
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 );
register_taxonomy("myposttype_categories", array("myposttype"), array("hierarchical" => true, "label" => "Categories", "singular_label" => "Type", "rewrite" => true));
}
And the query:
$args = array(
'post_type' => 'myposttype',
'myposttype_categories'=> 'foo');
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="https://wordpress.stackexchange.com/questions/87111/<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p><?php
endwhile;
}
wp_reset_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.
$args = array( 'myposttype_categories'=> 'foo' );
and
$args = array(
'post_type' => 'myposttype',
'tax_query' => array(
array(
'taxonomy' => 'myposttype_categories',
'terms' => array('foo'),
'field' => 'slug'
)
)
);