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.

I'm trying to only display posts with the tags Movie Trailers and Gaming Trailers but my loops are still displaying everything under the category /videos/

heres what I have

category-videos.php:

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

and in the functions.php i have:

//Display Movie Trailers
function display_movie_trailers( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'tag', 'movie trailers' );
    }
}
add_action( 'pre_get_posts', 'display_movie-trailers' );

//Display Gaming Trailers
function display_gaming_trailers( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'tag', 'gaming trailers' );
    }
}
add_action( 'pre_get_posts', 'display_gaming-trailers' );

I made some mistakes in the functions.php code above, and corrected them with the code you suggested but nothing is appearing in the loop. The category is videos so I now have:

//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' );

Edit: i tried gaming trailers and the sluggaming-trailers` without any luck.. anything that could be stopping this from working?

share|improve this question
    
I'd check to make sure whether or not your tag slugs have spaces in them or not because they shouldn't. game trailers probably should be game-trailers –  Manny Fleurmond Mar 30 '13 at 15:00

1 Answer 1

up vote 3 down vote accepted

In your template, the add_action you have there is way too late because by the time WordPress gets to your template, the query has already been performed.

If this is for a category page, I'd do the following in your functions.php:

//Display Gaming Trailers
function display_gaming_trailers( $query ) {
    if ( $query->is_category() && $query->query_vars['category_name'] == 'video') {
        $query->set( 'tag', 'gaming-trailers' );
    }
}
add_action( 'pre_get_posts', 'display_gaming_trailers' );
share|improve this answer
    
thanks for the help! I made some edits to the main post since my category is videos and my add_action was display_gaming-trailers instead of display_gaming_trailers so i corrected that also. For some reason the loop is returning "Nothing Found It seems we can’t find what you’re looking for. Perhaps searching can help." –  Joe Bobby Mar 30 '13 at 15:11
    
Do you know the tag slug? –  Manny Fleurmond Mar 30 '13 at 15:13
    
somehow the tags got removed from all the posts but after adding them its working fine! thanks! –  Joe Bobby Mar 30 '13 at 15:14

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.