Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Help using query_posts with custom post types and taxonomies WordPress

  • SOLVED

Hey everyone,

I've been using custom post types with my theme's feature slider for a while, but I'm now trying to expand upon it a bit by implementing custom taxonomies as well. The eventual goal is to build out a meta options panel where the user can call the feature slides from a particular category on a per page basis.

Right now I'm having trouble simply getting query_posts to work with the new taxonomy system. Here's a bit of code to show you how I'm going about everything:

From functions.php which registers both the custom post type and the taxonomy:

// Create custom post type for iFeature Slider

add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'if_custom_slides',
array(
'labels' => array(
'name' => __( 'Custom Slides' ),
'singular_name' => __( 'Slides' )
),
'public' => true,
'show_ui' => true,
'supports' => array('title', 'editor','custom-fields'),
'taxonomies' => array( 'slide_categories'),
'has_archive' => true,
'rewrite' => array('slug' => 'slides')
)
);
}

// Register custom category taxonomy for iFeature Slider

function if_custom_taxonomies() {
register_taxonomy(
'slide_categories',
'if_custom_slides',
array(
'hierarchical' => true,
'label' => 'Categories',
'query_var' => true,
'rewrite' => array( 'slug' => 'slide_categories' ),
)
);
}

add_action('init', 'if_custom_taxonomies', 0);


And here is the line in my slider.php file that I originally just used to query any of the posts from within this particular custom post type:

query_posts( array ('post_type' => 'if_custom_slides', 'showposts' => 20 ) );

I have tried adding 'category_name' => 'about', for example to call one of the test categories I have made and assigned a custom slide to, but something tells me that it is instead trying to call 'about' from the categories for standard post types.

Is there some step I'm missing to try and query the slider custom posts under the category 'about' (id 8 if that matters)? Also I have verified that I have a custom post under the category in question.

Thank you.

Answers (4)

2011-06-16

John Cotton answers:

You need to do a few things.

First merge the two functions about to one and have the register_taxonomy call first (I'm not absolutely sure it matterss, but just in case).

Then amend your query_posts to this:
query_posts( array ('post_type' => 'if_custom_slides', 'showposts' => 20, 'slide_categories' => 'about' ) );

I have assumed that "About" is a slide category....if not then you need to adjust your register_post_type line to this:

'taxonomies' => array( 'slide_categories', 'category'),

to include standard categories as a usable taxonomy.


Tyler Cunningham comments:

John,

Thank you, I did not realize that I had to simply use the custom taxonomy name in that way, but that makes perfect sense.

Good call on merging the two functions, I didn't do that before I tried your query_posts suggestion but I might as well to simplify things.

2011-06-16

Lew Ayotte answers:

Try this:

query_posts( array ('post_type' => 'if_custom_slides', 'showposts' => 20, 'if_custom_slides' => 'some_slug' ) );

It might be easier if you don't have a taxonomy that has the same name as the post type.

2011-06-16

derekshirk answers:

When I am trying to use a custom post type query, I usually use:


<?php query_posts( 'post_type=CPT_name_here'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>


You could probably add an array and query for the cpt category id instead of the name. Hope that helps.

2011-06-16

Kailey Lampert answers:

I think what you're looking for this this:

query_posts(
array (
'post_type' => 'if_custom_slides',
'showposts' => 20,
'tax_query' => array(
'taxonomy' => 'slide_categories',
'field' => 'slug',
'terms' => 'about' //category slug
)
)
);


Though, the {tax_slug} => 'term_slug' method still works, it has been deprecated as of WP3.1 in favor of tax_query (http://codex.wordpress.org/Function_Reference/WP_Query#Taxonomy_Parameters)