0

I am trying to list only the posts from a single category of my custom taxonomy.

I have created a custom post type called "Inventory" and a custom taxonomy called "Inventory Category".

I then created a number of categories using the "Inventory Category" taxonomy.

I would now like to display the posts from only a single category. fyi, this is not for display on the taxonomy.php page, that is all set up and working fine. This loop will be called on other pages.

This is the code I have so far, it returns ALL posts from the "Inventory Category" taxonomy rather than just those in the category "bulk racks".

query_posts( array( 'post_type' => 'inventory', 'inventory-category' => 'bulk-racks' ) ); 

Any help would be greatly appreciated!

Here is how i created the custom post type & taxonomy:

/* --- Create Inventory Custom Post Type --- */
add_action('init', 'inventory_register');

function inventory_register() {
$args = array(
    'label' => __('Inventory'),
    'singular_label' => __('Inventory'),
    'public' => true,
    'show_ui' => true,
    'capability_type' => 'post',
    'hierarchical' => true,
    'rewrite' => true,
    'supports' => array('title', 'thumbnail', 'revisions','custom-fields')
);

register_post_type( 'inventory' , $args );
}

/* --- Register Custom Taxonomy for Inventory Lists --- */ 
add_action( 'init', 'register_my_taxonomies', 0 );

function register_my_taxonomies() {

register_taxonomy(
    'division',
    array( 'inventory' ),
    array(
        'hierarchical' => true,
        'public' => true,
        'labels' => array(
            'name' => __( 'Inventory Category' ),
            'singular_name' => __( 'Inventory Categories' ),
            'show_ui' => true,
            'show_in_nav_menus' => true,
        ),
    )
);
}

1 Answer 1

3

I believe your looking for something like this

<?php
    $args = array(
        'posts_per_page' => 1,
        'post_type' => 'inventory',
        'tax_query' => array(
            array(
                'taxonomy' => 'inventory-category',
                'field' => 'slug',
                'terms' => array( 
                    'bulk-racks' 
                )
            )
        )       
    );
    query_posts( $args ); while ( have_posts() ): the_post();

    // do stuff here
?>

<?php endwhile; ?>

Heres how to properly register a post type according to the WordPress Codex page on post types and how to register taxonomies

8
  • Thanks for the reply VicePrez, but that is not returning anything at all for me. Could the problem be with how I created the custom posts & taxonomies?
    – maikunari
    Commented May 12, 2011 at 0:42
  • maybe, maybe not. check that your using their slugs and not actual names. Post up a link of your query, and how you registered your taxonomy and post type on Pastebin, so others and myself can see whats going on.
    – VicePrez
    Commented May 12, 2011 at 0:57
  • you've called your taxonomy "division" not "inventory-category".
    – VicePrez
    Commented May 12, 2011 at 1:15
  • @maikunari please visit the links above so you can edit your code according to the WordPress Codex.
    – VicePrez
    Commented May 12, 2011 at 1:23
  • Aha!! Thanks very much!! It sure helps to have an extra set of eyes. Very much appreciated, it works fine now.
    – maikunari
    Commented May 12, 2011 at 1:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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