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.

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.

Index.php:

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

    <?php
        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

1 Answer 1

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() ):
    $query->the_post();
    // loop stuff
endwhile;
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
1  
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

 
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.