List all custom post type posts from a given category?

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.

Read More

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

Related posts

Leave a Reply

1 comment

  1. 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="<?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'
            )
        )
    );