logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$10
Custom Taxonomy Question(s)

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:

Question 1. 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() ?>"><?php the_date( 'n/d'); ?><?php the_title(); ?></a></li>
<?php endwhile; ?>


Question 2. 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?

This question has been answered.

Jesse Sutherland | 07/08/10 at 2:19pm Edit


(5) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    07/08/10
    2:34pm
    Milan Petrovic says:

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

  • avatar
    Last edited:
    07/09/10
    10:07am
    Xavier Faraudo says:

    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.

    Previous versions of this answer: 07/08/10 at 2:40pm

  • avatar
    Last edited:
    07/09/10
    10:07am
    Lew Ayotte says:

    Did you register your post type and taxonomy?

    http://codex.wordpress.org/Function_Reference/register_post_type
    http://codex.wordpress.org/Function_Reference/register_taxonomy

    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.

  • avatar
    Last edited:
    07/08/10
    3:00pm
    Rashad Aliyev says:

    That's very useful!

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

  • avatar
    Last edited:
    07/08/10
    3:45pm
    Nile Flores says:

    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.

This question has expired.





Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.