I am trying to display all the content/posts assigned to a certain category.

I have a custom post type set up called 'Videos' and within this I have created categories called music, commercial, promo & short film.

Now everytime I post under the videos post type and assign it to a category I want to display all the content assigned to that category.

My attempt so far:

if ( have_posts() ) while ( have_posts() ) : the_post();
    //$args = array( 'post_type' => 'videos', 'posts_per_page' => 20, 'orderby' => 'date', 'order' => 'ASC' );
    $ytubeID = get_post_meta($post->ID, '_youtubeID', true);
    $vimID = get_post_meta($post->ID, '_vimeoID', true);

    if ($ytubeID || $vimID){
        if ($ytubeID && in_array($videos_cat_to_check,$videos_categories)){ // CHECK IF CURRENT POST HAS CATEGORY MUSIC 

            echo '<div class="video">';

            echo '<iframe title="YouTube video player" class="youtube-player" type="text/html" src="http://www.youtube.com/embed/'.$ytubeID.'"  allowfullscreen="true" frameborder="0" width="640" height="390">';

            echo '</iframe>';

            } elseif ($vimID){
            echo '<br />';
            echo '<div class="video">';
            echo '<iframe src="http://player.vimeo.com/video/'.$vimID.'" width="640" height="390" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
            }//end if yutbeID or vimIDthe_excerpt(); //excerpt added for information
            echo '</div>';

    echo the_content();

    $taxonomy = 'category';

    // get the term IDs assigned to post.
    $post_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
    // separator between links
    $separator = ', ';

    if ( !empty( $post_terms ) && !is_wp_error( $post_terms ) ) {

        $term_ids = implode( ',' , $post_terms );
        $terms = wp_list_categories( 'title_li=&style=none&echo=0&taxonomy=' . $taxonomy . '&include=' . $term_ids );
        $terms = rtrim( trim( str_replace( '<br />',  $separator, $terms ) ), $separator );

        // display post categories
        echo  $terms;



I am displaying a vimeo/youtube video with a title and description. The user enters the video ID into a custom meta box which then displays the video. I want to display all the videos/posts for the current category. Each category has different videos assigned to them.

  • 1
    In what context do you want to display all posts? In the category archive index, or in the single CPT post? – Chip Bennett Oct 4 '13 at 11:54
  • I would like to display all posts on the single-custom-post-type. – Neelam Khan Oct 4 '13 at 12:00

Based on this comment:

I would like to display all posts on the single-custom-post-type

You're saying that, in the single CPT post view, you want to display both the single CPT post, and also a list of all CPT posts in the same category.

To do that:

  1. Create single-{post-type}.php
  2. The queried single CPT post is output via the default loop:

    if ( have_posts() ) : while ( have_posts() ) : the_post();
        // Loop markup here
    endwhile; endif
  3. You will then need to retrieve the appropriate category:

    $cpt_cats = get_the_category();
    $cpt_cat = $cpt_cats[0]->term_id;
  4. You will then need to add a secondary query to query all CPT posts in the given category

    $cpt_query = new WP_Query( array( 
        'post_type' => $cpt_slug, 'cat' => $cpt_cat, 'posts_per_page' => -1
    ) );
  5. You will then need to create a custom loop to output your custom query:

    if ( $cpt_query->have_posts() ) : while ( $cpt_query->have_posts() ) : $cpt_query->the_post();
        // Secondary loop markup here
    endwhile; endif;
    // Don't forget this

(You will need to change the custom query parameters to suit your needs, and fill in the HTML markup where appropriate.)

| improve this answer | |
  • Sorry to be a pain but could you show me how this would fit into my code? I'm not sure where I need to add the code you suggested. Many thanks. – Neelam Khan Oct 4 '13 at 12:54
  • The code goes in the single-{post-type}.php file. – Chip Bennett Oct 4 '13 at 12:59

Try this:

$query = WP_Query('post_type=videos');
while(have_posts()): the_post();?>
      <?php the_title();?>
       <?php the_post_thumbnail(); ?>
        <?php the_content(); ?>
        <?php endwhile;?>

You need to tell WP_Query what post type we need to call here post_type=post-type-slug

| improve this answer | |

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.