treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Display posts from post_type with a certain tag only

  • Ok I'm struggling with this.

    I have a 'Portfolio' post type on my website and I have various tags for different items(web design, graphics, logos, etc).
    On a separate page I want to display posts from that post type, but only show posts that have the 'testimonial' tag.

    I've tried several methods and really can't figure this out so any help would be much appreciated!

    Thanks in advance!
  • I'm a tad confused, by tags do you mean categories?
  • No tags. I have a special post type, so in the WP Admin area, rather than adding a new 'post' I add a new 'portfolio item'. The individual portfolio items are then filtered by tags(http://en.support.wordpress.com/posts/tags/). I want to display everything from the 'testimonial' tag within the 'portfolio' type.
  • Have you tried a wp_query loop like this?


    <p>Tag: <?php single_tag_title(); ?></p>
    <?php
    $args = array( 'post_type' => 'portfolio-item', 'posts_per_page' => 10, 'tag' => 'testimonial' );
    $loop = new WP_Query( $args );
    if ( ! $loop->have_posts() ) :
    echo '<h2>Move along people there is nothing to see here...</h2>';
    else:
    while ( $loop->have_posts() ) : $loop->the_post();
    if( is_tag() || is_category() || is_archive() ) {
    // blah blah blahhhh
    } else {
    // blah blah blahhhh so on and so forth
    }
    endwhile;
    endif;
    ?>


    had that in my snippets archive, don't remember where it came from.. modified it to fit your stuff...assuming your post type is 'portfolio-item' and your tag slug is 'testimonial'


  • Thanks for your help kgscott285, but I didn't use it in the end :-/

    I carried on using query_posts, but since the testimonials are a 'quote' I was able to use this:

    'tax_query' => array(
    'relation' => 'OR',
    array(
    'taxonomy' => 'category',
    'field' => 'slug',
    'terms' => array( 'quotes' )
    ),
    array(
    'taxonomy' => 'post_format',
    'field' => 'slug',
    'terms' => array( 'post-format-quote' )
    )
    )


    Thanks again!