WordPress.org

Support

Support » How-To and Troubleshooting » How to show posts by one specific tag, using new WP_query

How to show posts by one specific tag, using new WP_query

  • I wanna show the last posts (2-3 posts with title, content) that have a specific tag = ‘mytag’
    so I tried to put this code in a php widget:

    <?php
                $query = new WP_Query();
                $query->query('tag=mytag');
            ?>
     <?php echo get_the_title(); ?>
    <?php the_content('Read more...'); ?>

    But it gets the most recent post that also doesn’t have any tag…Anyone know why?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Michael

    @alchymyth

    Forum Moderator

    I put the “usage” code and changed the $arg with mytag. The title of the right post is showing up but suddenly the background changed color!?

    <?php
    
    // The Query
    $the_query = new WP_Query( 'tag=casa' );
    
    // The Loop
    while ( $the_query->have_posts() ) : $the_query->the_post();
    	echo '<li>';
    	the_title();
    	echo '</li>';
    endwhile;
    
    // Reset Post Data
    wp_reset_postdata();
    
    ?>

    Ok it was a HTML/CSS issue, I replaced the
    <li> with <p> and it’s fine. Now I want to add some content beyond the title so I started with the excerpt.

    <?php
    
    // The Query
    $the_query = new WP_Query( 'tag=casa' );
    
    // The Loop
    while ( $the_query->have_posts() ) : $the_query->the_post();
    	echo '<p>';
    	the_title();
            the_excerpt();
    	echo '</p>';
    endwhile;
    
    // Reset Post Data
    wp_reset_postdata();
    
    ?>

    I need thumbnail but not sure how to get it. I know how to add everything if I have them outside the php function.
    Like I was trying to do in my firt post above. Is it possible? it will make things much easier as I’m not expert in php
    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to show posts by one specific tag, using new WP_query’ is closed to new replies.