0

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

1 Answer 1

0

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>';
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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