Search code examples
wordpresscustom-post-typetaxonomy

WordPress 3.0.1 Query Custom Post Type with Custom Taxonomy


I have a custom post type with multiple taxonomy types. The issue is focused around just one of them. I need to display all custom posts that have a taxonomy from featured-vendors checked. Right now, there is just one "featured" , but there may be more in the future, such as "highlight" or "sponsor" or something alone those lines. But, for now, I just need to go through all the "vendors" custom post type, find ones with "featured" checked inside of the "featured-vendors" taxonomy.

I have some some posts out there that state it's not possible, but they were either from 2.8 or from the very first of this year and I know WordPress has released at least one update since then.

Thanks in advance!!


Solution

  • Query Custom Post Types by Taxonomy Term


    This sample will assume:

    • the custom post type was registered with the taxonomy and the taxonomy was registerd with 'query_var' =>true and 'hierarchial' => true

    • The "checked" term will be the parent and any new ones can be added later on as children.

    The Code:

     <?php query_posts( array( 'featured-vendors' => 'checked' ) ); ?>
        <?php if( is_tax() ) {
            global $wp_query;
            $term = $wp_query->get_queried_object();
            $title = $term->name;
        }  ?>
        
        <div class="my-custom-post">
        <h3><?php echo($title); ?></h3> //this will show the name of the post type
        <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        
         <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 
    
        <div class="entry"> <h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
    
            <?php the_excerpt(); ?> //shows the excerpt 
    
         </div><!--/end entry-->
    </div><!--/end post-->
        
        <?php endwhile; else: ?>
        <p><?php _e('No Post Found','your_theme_text_domain'); ?></p> 
    
        <?php endif; ?>