Get a free trial // Grow your CSS skills // Land your dream job

WP – Display posts with certain tag only

  • # August 13, 2014 at 1:04 am


    I’ve been Googling around but can’t seem to find a snippet for my problem.
    I have two custom post types – Foods, Promos. Now I have a section on my homepage where I want to show 3 ‘featured’ items either from the Promos or Foods with a tag of ‘featured’.

    So basically I just want a snippet to show 3 posts with a tag = ‘featured’ from those two custom post type. I am able to show post with a tag of ‘featured’ but only from Foods or only from Promos.


    # August 13, 2014 at 2:58 am

    Ok, I’ve managed to do it but really not sure if it’s even the best way to do it.

    <ul class=”featured-list”>
    $type = array(‘promo’, ‘food’);
    $args = array(
    ‘tag’ => ‘featured’,
    ‘post_type’ => $type,
    ‘post_status’ => ‘publish’,
    ‘numberposts’ => 3

          $my_query = null;
          $my_query = new WP_Query($args);
          if($my_query->have_posts()) {
            while ($my_query->have_posts()) : $my_query->the_post(); ?>
              <div class="promo-thumb">
                  if ( has_post_thumbnail() ) {
                  } else { ?>
                    <img src="<?php echo get_template_directory_uri(); ?>/images/default-featured-thumb.png" alt="">
                  <?php } ?>
              <h3 class="promo-name"><?php the_title(); ?></h3>
              <div class="promo-desc"><?php the_content(); ?></div>
              <a href="#" class="button">Read more</a>


    # August 13, 2014 at 4:40 am

    Seems about right. There might be more efficient ways, but that’s hard to tell without seeing the structure of your code. There’s nothing wrong about this approach though. (I think, I read through it fairly quickly).

    # August 16, 2014 at 2:59 pm

    Did you check Here?

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed