Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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));
share|improve this question
    
what does your code look like that iterates over the results of your query? what does your code look like that registers your post type and taxonomy? –  Milo Feb 15 '13 at 23:57
    
I've been checking everything from WP_Query to hated query_posts, both taxonomy and custom post types are ok, code that iterates the results also, does filtering custom post types using terms work for you? Because I'm afraid it just simply doesn't and that's the problem. All examples in Codex use 'post' post type... –  Wordpressor Feb 16 '13 at 4:09
    
your code above works fine for me, that's why I asked about your post type and tax registration code, and what your loop looked like. it is indeed possible to do what you want, you've just got an issue with your code elsewhere. –  Milo Feb 16 '13 at 4:28
    
Alright, thanks a lot! Question updated. –  Wordpressor Feb 16 '13 at 16:17

1 Answer 1

up vote 2 down vote accepted

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'
        )
    )
);
share|improve this answer
    
Returns empty object for me, are you certain it works? By the way I wanted to display only foo or bar, not both :) And yeah, my taxonomy exists, there are posts filed under it. –  Wordpressor Feb 16 '13 at 3:37
    
Yeah, just checked, looks like tax_query works alright only with post post type, and for custom post types returns nothing for arrays such as: 'terms' => array('foo') or everything for strings: 'terms' => 'foo'. Well, looks like we still can't mix custom post types AND term filtering? :( –  Wordpressor Feb 16 '13 at 4:04
    
You said foo (or bar) so I misinterpreted that mean you had items in both categories. This query is looking for items that are in both, so if you don't have any you would get an empty object. For what it's worth, tax_query will work with any post type. You should probably post your code for registering the post type and the taxonomies. I'll adjust my code, though if you are doing a simple query, what you posted should work which is why I think you need to post more code. –  helgatheviking Feb 16 '13 at 14:23
    
PS- I didn't mean to "shout" and bold everything, I forgot that # is markdown for bold. Doh. –  helgatheviking Feb 16 '13 at 14:24
    
That's alright, just updated my question with custom post type and taxonomy registration codes. –  Wordpressor Feb 16 '13 at 16:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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