I am creating a WordPress theme for a record label. One aspect is the video gallery.

I created the gallery using a custom post type called 'videos'. The code below is what I placed in my functions.php file to set up the post type:

add_action( 'init', 'create_my_post_types' );
        function create_my_post_types() {

register_post_type( 'videos',
            'labels' => array(
                'name' => __( 'Videos' ),
                'singular_name' => __( 'Video' ),
                'add_new' => 'Add New Video',
                'edit' => 'Edit Video'
            'public' => true,
            'exclude_from_search' => false,
            'supports' => array( 'title', 'editor', 'thumbnail','page-attributes','excerpt' ),
            'rewrite' => array( 'slug' => 'videos', 'with_front' => false ),


I also created a custom taxonomy called 'artists' so I can assign the artist name to each video I upload.

 add_action( 'init', 'create_videos_taxonomies' );

    function create_videos_taxonomies() {

                    'hierarchical' => false,
                    'label' => 'Artist',
                    'query_var' => true,
                    'show_tagcloud' => true,
                    'show_ui' => true,
                    'rewrite'=>array('slug' => 'artists', 'with_front' => false)

At this point, everything works fine on the backend and the video page returns all of the videos successfully.

On another area of my website, I have pages for every single artist. On any one of these artist pages, i would like to be able to loop through all of the videos in the custom post type I created, and only return results in a given taxonomy. Below is my code to loop through a custom post type:

<?php $loop = new WP_Query( array( 'post_type' => 'videos', 'post_child' => 0, 'posts_per_page' => 5 ) ); ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_post_thumbnail( 'video' ); ?>
<?php the_content(); ?> 
<?php endwhile; ?>

This successfully loops through the videos, and returns the 5 most recent. I need this to loop through all the videos with a specific taxonomy slug or ID. For instance, I created a taxonomy called 'Smash Palace' and it has a slug of 'smash-palace' and an id of '17'. Any idea how I can loop though this custom post type and only return results in a given taxonomy?


Check the documentation here:


You'll want to add this to your wp_query:

'smash-palace' => 'smash-palace'

In other words, your new query would look something like:

$loop = new WP_Query( array( 'post_type' => 'videos', 'artists' => 'smash-palace', 'post_child' => 0, 'posts_per_page' => 5 ) );

EDIT: I answered this question under the assumption that "Smash Palace" isn't actually a separate taxonomy, but a term within your existing "Artists" taxonomy. If "Smash Palace" is actually a separate taxonomy, then the following should work:

$args = array (
   'tax_query' => array (
      array (
         'post_type' => 'videos',
         'taxonomy' => 'smash-palace',
         'field' => 'slug',
         'terms' => 'slug-of-your-term'

$query = new WP_Query( $args );

if( $query->have_posts() ) {
   while ( $query->have_posts() ) : $query->the_post();
      // output your stuff

One thing I noticed: if you do not define "terms" in the tax_query array, you may get unexpected results (or none at all).

You can query multiple terms at once like this:

$args = array (
   'tax_query' => array (
      'terms' => array ( 'slug-of-term-one', 'slug-of-term-two' )

I hope that helps -- let me know if I understood your question correctly.

  • Just to confirm your comment, taxonomy queries do need to include a term slug or ID, you can't query just on taxonomy alone, a term must be provided, whether it's one to match or not match, eg.. = or != .. – t31os Apr 2 '11 at 13:36
  • @Jesse, glad I could help! – Ryan Apr 4 '11 at 1:10
  • @t31os, thanks for your comment. I did notice that leaving the slug out gave strange results. – Ryan Apr 4 '11 at 1:11

Your Answer

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