Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have created a custom post type that contains television channel data. Each channel has its own post. I have also created a custom taxonomy of channel groups. What I want to do is query all the data and display it all on a table on a single page like so:

Channel # | Channel Name | Basic Package | Delux Package | Elite Package
1         | NBC          | X             | X             | X
2         | ESPN Classic |               | X             | X

What I need is a query that will return the related taxonomies for each post. This is what I have so far:

$args = array(
    'post_type' => 'my_channel',
    'numberposts' => -1,
    'order_by' => 'menu_order',
    'order' => 'ASC',
    'tax_query' => array(
        array(
            'taxonomy' => 'my_channel_package',
            'field' => 'slug',
            'terms' => array('basic', 'delux', 'elite'),
        ),
    ),
);

$posts_array = get_posts($args);

But the post array does not return the taxonomy data. Is my next option to query the database with custom SQL or am I missing a feature in the WordPress API?

Thanks! Jason

share|improve this question

1 Answer 1

up vote 0 down vote accepted

Welcome to WPSE Jason, get_posts is for querying posts, and the tax_query part of that allows you to select posts which belong to taxonomies of a certain criteria.

To get the taxonomy terms of a particular post, use get_the_terms, to which you pass the post's ID and a taxonomy. It returns an array of taxonomy term objects associated to that post, and belonging to that specified taxonomy.

Example usage:

<?php
$terms = get_the_terms( $post->ID, 'on-draught' );
//Echo a list of 'on-draught' terms for this post   
echo '<ul>';                    
if ( $terms && ! is_wp_error( $terms ) ) : 
    foreach ( $terms as $term ) {
        echo '<li>'.$term->name.'</li>';
    }
echo '</ul>';
?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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