WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Confused on how to achieve this, but what I'm trying to do is load posts with the tag "reviews" in the loop on the homepage.


<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>

        get_template_part( 'content-reviews', get_post_format() );

<?php endwhile; ?>

<?php else : ?>

<?php get_template_part( 'no-results', 'index' ); ?>

<?php endif; ?>
share|improve this question
up vote 1 down vote accepted

Use the pre_get_posts action to modify the main query. Place this in your functions.php file:

function wpa71787_home_tag_query( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'tag', 'reviews' );
add_action( 'pre_get_posts', 'wpa71787_home_tag_query' );

You can set any valid parameters of WP_Query with this method.

Edit, secondary query excluding tagged posts, use tag ID:

$query = new WP_Query( array( 'tag__not_in' => array( 42 ) ) );
while( $query->have_posts() ):
    // loop stuff
share|improve this answer
i added <?php add_action( 'pre_get_posts', 'tag=reviews' ); ?> above have_posts but when I add the code ot the functions php it doesnt display anything. I have 2 loops in the index file is that ok? – Joe Bobby Nov 7 '12 at 2:41
add_action does not work that way, you need to reference a function, see the example I provided. It has to go in functions.php because pre_get_posts happens before the template is loaded. If this is not your main loop, you should query directly for the posts you want by tag via WP_Query. – Milo Nov 7 '12 at 2:50
I have it working now, but I have 2 loops on the page that seem to be affected. how do i display only the "reviews" tag in 1 loop while excluding it from the other? – Joe Bobby Nov 7 '12 at 2:51
Create a secondary query for other loops on the page, see edit above. – Milo Nov 7 '12 at 2:55
would that go in the functions.php? – Joe Bobby Nov 7 '12 at 3:06

Your Answer


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.