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

Custom Taxonomy Question(s) WordPress

  • SOLVED

I'm trying to use the new custom taxonomy feature in WP 3.0, but there isn't a ton of tutorials or documentation yet on how to use it once its set up. I have two quick questions:

<strong>Question 1. </strong>I'd just like to list out some posts using a wp_query and filter it by a custom post type (which is working) along with a custom taxonomy (not working). The custom taxonomy is 'event-type' and the kind I want is 'the-journey'.

What am I doing wrong?

<?php $my_query = new WP_Query( array( 'post_type' => 'event', 'event-type' => 'the-journey', 'posts_per_page' => 6, 'order' => 'ASC' ) );while ($my_query->have_posts()) : $my_query->the_post();$do_not_duplicate = $post->ID; ?>
<li><a href="<?php the_permalink() ?>"><strong><?php the_date( 'n/d'); ?></strong><?php the_title(); ?></a></li>
<?php endwhile; ?>


<strong>Question 2. </strong>I also need to be able to test and see if a certain "single" page is a part of that taxonomy, outside the loop. How would I go about doing that?

Answers (5)

2010-07-08

Xavier Faraudo answers:

Question 1:
You should use either the query_var arg of the custom taxonomy, or the parameters
'taxonomy' =>'event-type' , 'term' => 'the-journey' to the WP_Query, or post_type, but not both.

BTW, taxonomies are not new to 3.0, and are much more limited than regular categories and post tags (forget about category__and , for instance). You'd better go with regular cats and tags, and maybe some option row in the database to group them.
Or use my ZYX Term Meta plugin, which allows to create term groups (which can be children of other term groups) with lots of features. This plugin is under development (actually, it's the docs that are missing and some UI), if you feel interested, mail me.

Question 2:
It depends a lot on what do you have inside that single page.
But all you need is to know, I suppose, if some post->ID has any term of that taxonomy attached to it. Just need to do:
wp_get_object_terms( $post->ID , 'event-type' );

It will return an empty array if it has no terms for event-type, so

count( wp_get_object_terms( $post->ID , 'event-type' ) )

will return "true" (1 or bigger) if that "single" has anything related to event-type.

2010-07-08

Lew Ayotte answers:

Did you register your post type and taxonomy?

[[LINK href="http://codex.wordpress.org/Function_Reference/register_post_type"]]http://codex.wordpress.org/Function_Reference/register_post_type[[/LINK]]
[[LINK href="http://codex.wordpress.org/Function_Reference/register_taxonomy"]]http://codex.wordpress.org/Function_Reference/register_taxonomy[[/LINK]]

Example:

(1) register the new post type

register_post_type( 'history',
array(
'label' => 'History',
'public' => true,
'show_ui' => true,
'publicly_queryable' => false,
'exclude_from_search' => true,
'hierarchical' => false,
)
);


(2) register the taxonomy

register_taxonomy('featured',
array('page','post','history'),
array(
'hierarchical' => TRUE,
'label' => 'Featured Pages',
'singular_label' => 'Featured Page',
'public' => TRUE,
'show_ui' => TRUE,
'show_tagcloud' => FALSE,
'rewrite' => FALSE
));

(3) do the query

$args = array(
'post_type' => 'history',
'post_status' => 'publish',
'featured' => 'test',
);

$wp_query = new WP_Query($args);


I could probably get it working for you if I had access to your source.

2010-07-08

Milan Petrovic answers:

Well, you can't do it, because WP_Query doesn't support that (as far as I know). I am author of GD Custom Posts And Taxonomies Tools and I use taxonomies on many websites, and because of problems with using them I have written number of functions that are part of this plugin (Pro version only, not free). One of the functions allow filtering by any number of taxonomies and terms, and in the same time allow using all parameters you would use in WP_Query.

As for the second question, there is function: get_post_taxonomies() it will get taxonomies for a post (use post_id as a parameter for the function).

2010-07-08

Rashad Aliyev answers:

That's very useful!

Look at this : http://codex.wordpress.org/Custom_Taxonomies

2010-07-08

Nile Flores answers:

Why not list it as a query to the category.

http://wpaddict.net/display-posts-from-a-category-in-wordpress/

----------
However, if it is the URL you want to change while using custom taxonomies, I have found Net Ts to have a great tutorial. -
http://net.tutsplus.com/tutorials/wordpress/introducing-wordpress-3-custom-taxonomies/

It is listed on the WP Codex page for the custom taxonomies.