So I have a custom post type called Video and a taxonomy called category that I use to group videos togheter, so I want to list them like this:

Taxonomy NAME 1

Taxonomy NAME 2
Video 1
Video 2

So I tried something but it doesn't work, the problem is that for each video I also display the taxonomy name like this:

Taxonomy Name 1
Video 1
Taxonomy Name 1
Video 2

So this is not what I want, mabe you guys can figure it out, what I do wrong:

<?php get_header();?>
    $args = array( 'post_type' => 'Video');
    $loop = new WP_Query( $args );
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
        <?php $fg = get_the_terms(get_the_ID(),'category'); ?>
        <?php foreach($fg as $term): ?>
                <h2><?php echo $term->name; ?></h2>
                <?php wp_reset_postdata(); ?>
                        $posts = new WP_Query(array('post_type' => 'Video', 'category' => $term->name) );
                        while ( $posts->have_posts() ) : $posts->the_post();
                            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
        <?php endforeach; ?>
<?php endwhile; ?>

<?php get_footer(); ?>

If someone can help please do it :( I can't get this


Okay, so first off, I would rename your video category to something like video_category so it doens't conflict with the built in category.

That said, the easiest way I could think to demonstrate this for you was with a shortcode.

Some initial prep, set up post types (for demonstration, delete the callas to register_post_type and register_taxonomy if you're pasting this into your functions.php file) and add the shortcode.

add_action('init', 'wpse28770_add_types' );
function wpse28770_add_types()
    register_post_type( 'video', array( 'public' => true, 'label' => 'Videos' ) );
    register_taxonomy( 'video_category', 'video', array( 'label' => 'Video Category' ) );
    add_shortcode( 'wpse28770videos', 'wpse28770_shortcode_cb' );

And they we have the shortcode callback function which does all the work. First you need to get all the terms in your video_category taxonomy.

Next, loop through each term, fetching the posts in each one via a tax_query in get_posts. Then just loop through the posts and add them to a list.

function wpse28770_shortcode_cb()
    // get the terms
    $terms = get_terms( 'video_category' );

    // no terms?  bail.
    if( ! $terms ) return '';

    $out = '';

    // loop through the terms
    foreach( $terms as $term )
        // get videos in each term
        $videos = get_posts(array(
            'post_type' => 'video',
            'tax_query' => array(
                    'taxonomy'  => 'video_category',
                    'field'     => 'id',
                    'terms'     => $term->term_id,
                    'operator'  => 'IN'

        // no videos? continue!
        if( ! $videos ) continue;
        $out .= '<h2>' . esc_html( $term->name ) . '</h2>';
        $out .= '<ul>';
        // loop through the video posts
        foreach( $videos as $v )
            $link = sprintf( 
                '<a href="%s">%s</a>', 
                esc_url( get_permalink( $v ) ),
                esc_html( $v->post_title )
            $out .= '<li>' . $link . '</li>';
        $out .= '</ul>';
    return $out;

You can just drop the above 'wpse28770_shortcode_cb' function in your functions.php file, and call it in whatever you template you like like this...

<?php echo wpse28770_shortcode_cb(); ?>

Here's that whole mess as a plugin. Hopefully that at least gets you started!

  • This Worked, OMG GOD bless you, you are the best, omg I really needed this, I don't know how to thank you! AGAIN: THANKS!
    – Uffo
    Jan 13 '12 at 0:35
  • If I understand your problem correct, you could also look into get_objects_in_term() as it is the proper way to achieve this using the WP API. If you get your terms and then loop through them, then you can get all the relevant posts for each term using this function. Jun 12 '12 at 13:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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