I have 2 loops, movie trailers and gaming trailers but gaming trailers is displaying under both Movie Trailers and Gaming Trailer. Each loop displays the post tagged with either Movie Trailer
or Gaming Trailer
.
How do I get posts tagged with “Movie Trailers” under the movies area and posts tagged with “Gaming Trailers” under the gaming area?
Heres the loops:
<div class="section-header clearfix">
<h2>Movie Trailers</h2>
<div class="section-filter">
<ul>
<li><a class="active" href="http://site.com/videos/">Recent</a></li>
<li><a href="http://site.com/tag/movie-trailers/">More Movie Trailers</a></li>
</ul>
</div>
</div>
<?php if ( have_posts() ) : ?>
<?php ?>
<?php add_action( 'pre_get_posts', 'display_movie_trailers' ); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
get_template_part( 'content-videos', get_post_format() );
?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>
<div class="section-header latest-gaming-trailers clearfix">
<h2>Gaming Trailers</h2>
<div class="section-filter">
<ul>
<li><a class="active" href="http://site.com/videos/">Recent</a></li>
<li><a href="http://site.com/tag/gaming-trailers/">More Gaming Trailers</a></li>
</ul>
</div>
</div>
<?php if ( have_posts() ) : ?>
<?php ?>
<?php add_action( 'pre_get_posts', 'display_gaming_trailers' ); ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
get_template_part( 'content-videos', get_post_format() );
?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>
Functions.php
//Display Movie Trailers
function display_movie_trailers( $query ) {
if ( $query->is_category() && $query->query_vars['category_name'] == 'videos') {
$query->set( 'tag', 'movie-trailers' );
}
}
add_action( 'pre_get_posts', 'display_movie_trailers' );
//Display Gaming Trailers
function display_gaming_trailers( $query ) {
if ( $query->is_category() && $query->query_vars['category_name'] == 'videos') {
$query->set( 'tag', 'gaming-trailers' );
}
}
add_action( 'pre_get_posts', 'display_gaming_trailers' );
You can’t add the pre_get_posts action right before the loop. You HAVE to do it in functions.php. It has to be set before the site actually queries the db to set up the loop which happens before your template file is loaded.
What you are doing is looping through the exact same query twice, because pre_get_posts can’t change a query after happens. In this case, the easiest way to change it is to query_posts to set up a whole new query. Or manually build a query with WP_Query.
The ideal situation might be to use pre_get_posts for one loop and a new query for the other, but a simple solution is something like this:
I think you have misunderstood the use of
pre_get_posts()
. You’d normally use this in your functions.php – which you are, kind of – but you also have it in your template, which is incorrect. Also, the versions in your functions.php both check for the same category (videos).That said, I wouldn’t attempt to use pre_get_posts in this scenario at all, as you have multiple loops on one page. Instead, you should be using WP_Query