Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Like the title says, what code would I use on a wordpress page so that it will only show posts that have a specific tag?

share|improve this question
add comment

2 Answers

I'm pretty sure I always read on here that new WP_Query() is recommended over query_posts. Additionally you can use the Transient API to improve performance on additional queries. Place this in whatever template where you'd like the list to display:

// Get any existing copy of our transient data
if ( false === ( $my_special_tag = get_transient( 'my_special_tag' ) ) ) {
    // It wasn't there, so regenerate the data and save the transient

   // params for our query
    $args = array(
        'tag' => 'foo'
       'posts_per_page'  => 5,
    );

    // The Query
    $my_special_tag = new WP_Query( $args );

    // store the transient
    set_transient( 'my_special_tag', $my_special_tag, 12 * HOUR_IN_SECONDS );

}

// Use the data like you would have normally...

// The Loop
if ( $my_special_tag ) :

    echo '<ul class="my-special-tag">';

    while ( $my_special_tag->have_posts() ) :
        $my_special_tag->the_post();
        echo '<li>' . get_the_title() . '</li>';
    endwhile;

    echo '</ul>';

else :

echo 'No posts found.';

endif;

/* Restore original Post Data
 * NB: Because we are using new WP_Query we aren't stomping on the
 * original $wp_query and it does not need to be reset.
*/
wp_reset_postdata();

And in your function.php you will need to clear out the transient when things are updated:

// Create a function to delete our transient when a post is saved or term is edited
function delete_my_special_tag_transient( $post_id ) {
    delete_transient( 'my_special_tag' );
}
add_action( 'save_post', 'delete_my_special_tag_transient' );
add_action( 'edit_term', 'delete_my_special_tag_transient' );
share|improve this answer
add comment

Before the loop, use query_posts function

query_posts( 'tag=foo' );

This will returns all posts with the assigned tag.

<?php
// retrieve post with the tag of foo
query_posts( 'tag=foo' );
// the Loop
while (have_posts()) : the_post();
    the_content( 'Read the full post »' );
endwhile;
?>

You can also use it to return posts with multiple tags

query_posts( 'tag=foo,bike' );

For further parameters and reference, see http://codex.wordpress.org/Class_Reference/WP_Query#Parameters http://codex.wordpress.org/Function_Reference/query_posts

share|improve this answer
1  
Thanks! Also found this plugin as well: wordpress.org/extend/plugins/list-category-posts/other_notes –  user1114968 Jan 14 '12 at 11:40
2  
Whatever works, it's best to avoid using plugins if it can be done within the theme folder. Cuts down on load time and server space. –  Tribbey Jan 14 '12 at 12:37
1  
That’s wrong, it doesn’t matter where the code comes from. If you store plugin code in a theme file you just cannot turn it off separately. Do as little as possible in a theme to make your life easier. –  toscho Jul 23 '12 at 8:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.