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.
//Display All Reviews
function display_all_reviews( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'tag', 'reviews' );
    }
}
add_action( 'pre_get_posts', 'display_all_reviews' );

//Movie All Reviews
function display_movie_reviews( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'tag', 'movie-reviews' );
    }
}
add_action( 'pre_get_posts', 'display_movie_reviews' );

Display All Reviews is the only thing that is suppose to be displayed on the homepage and display_movie_reviews is suppose to be displayed in the sidebar for its category is_category('movies'), as well as posts in that category. I see they have $query->is_home() but I have tried is_category('movies') without any luck..

Here is the display_movies_reviews markup for the category:

<div id="sidebar-reviews">
    <span>Movie Reviews</span>
    <ul>
        <?php if ( have_posts() ) : ?>
        <?php /* Start the Loop */ ?>
        <?php add_action( 'pre_get_posts', 'display_movie_reviews' ); ?>
        <?php while ( have_posts() ) : the_post(); ?>
        <?php get_template_part( 'sidebar-reviews', get_post_format() ); ?>
        <?php endwhile; ?>
        <?php else : ?>
        <?php get_template_part( 'no-results', 'index' ); ?>
        <?php endif; ?>
    </ul>

</div>

And here is the display_movies_reviews markup for the posts within the movies category:

<!-- ###Movies Single Sidebar### -->
<?php elseif ( ! empty ( $GLOBALS['post'] ) && is_single() && in_category( 'movies', $GLOBALS['post'] ) ) : ?>
<div class="sidebar-ad">
</div>
<div id="sidebar-reviews">
    <span>Movie Reviews</span>
    <ul>
        <?php if ( have_posts() ) : ?>
        <?php /* Start the Loop */ ?>
        <?php add_action( 'pre_get_posts', 'display_movie_reviews' ); ?>
        <?php while ( have_posts() ) : the_post(); ?>
        <?php get_template_part( 'sidebar-reviews', get_post_format() ); ?>
        <?php endwhile; ?>
        <?php else : ?>
        <?php get_template_part( 'no-results', 'index' ); ?>
        <?php endif; ?>
    </ul>

</div>

I have tried fixing it like this:

//Movie All Reviews
function display_movie_reviews( $query ) {
    if ( $query->is_category('movies') ) {
        $query->set( 'tag', 'movie-reviews' );
    }
}
add_action( 'pre_get_posts', 'display_movie_reviews' );

but havent had any luck. Any ideas how I can get the homepage and sidebar working?

edit: sidebar

<?php if ( $query->is_category('movies') ): ?>
<?php $movies = new Wp_Query('tag=movie-reviews'); ?>

<?php while ( $movies->have_posts() ) : ?>
<?php $movies->the_post(); ?>
<?php get_template_part( 'sidebar-reviews', get_post_format() ); ?>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>
share|improve this question
    
Wow. Shouldn't you be using new WP-Queries for everything? It would be a trillion times easier. –  Dan Gayle Dec 9 '12 at 1:00
    
@DanGayle what do you mean? –  Joe Bobby Dec 9 '12 at 1:13
    
For the sidebar, just use a wp-query block. See answer below –  Dan Gayle Dec 9 '12 at 1:19
add comment

1 Answer

up vote 1 down vote accepted

For the sidebar bit, you should be using a new Wp_Query:

if ( $query->is_category('movies') ):
    $movies = new Wp_Query('tag=movie-reviews');

    while ( $movies->have_posts() ) :
        $movies->the_post();
        get_template_part( 'sidebar-reviews', get_post_format() );
    endwhile;

    else:
        get_template_part( 'no-results', 'index' );
endif;

You don't need query filters for anything except the main loop on a page.

share|improve this answer
    
I edited my main post and added the code after seeing this post but I'm getting an error... did I do it incorrectly? –  Joe Bobby Dec 9 '12 at 2:08
    
the error is Fatal error: Call to a member function is_category() on a non-object in /home/dev/public_html/wp-content/themes/wgtc/sidebar.php on line 52 –  Joe Bobby Dec 9 '12 at 3:15
    
Sorry, I didn't test any of that code. You'll need to update the if statement accordingly. –  Dan Gayle Dec 10 '12 at 5:39
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.