Search code examples
wordpressarchiveposts

wordpress query_posts in archive issue


I want to make all my posts in each category descend based on its score(using GD star rating plugin). I have found out in the plugin site, that I need to add query_posts("gdsr_sort=rating"); in my archive.php file.

So this is how my archive.php looks with query_posts added:

    <?php query_posts("gdsr_sort=rating"); ?>
<?php while (have_posts()) : the_post(); ?>
    <div <?php post_class() ?>><li>
        <h1><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h1>

        <?php if ( has_post_thumbnail() ) : ?>
            <a href="<?php the_permalink() ?>"><?php the_post_thumbnail(); ?></a>
        <?php endif; ?>

        <?php the_content('Read more...'); ?>
    </li>
    </div>
    <?php endwhile; ?>
        <div class="clear"></div>
    <?php wp_reset_query(); ?>

And it messed things up, so I tried using query_posts('posts_per_page=5'); The good thing is that it is showing 5 articles per page. The bad thing is that it's showing the wrong articles. To be precise, no matter which sub-category I go.. the same articles are being displayed which aren't supposed to be in that sub-category. When you go to next page.. the same articles are being displayed. These articles that are shown everywhere only belong to 1 sub-category. I should also mention that I have like 10 cateogires and each of them has 2-3 sub-categories..

I'm at a loss here, hope you understood my explanation.


Solution

  • Instead of making a new query you can modify the wordpress query. I don't know about the plugin you use but I think this should work:

    function change_archive_query( $query ) {
        if ( $query->is_main_query() && $query->is_archive()) {
             $query->set( 'gdsr_sort', 'rating' );
        }
    }
    add_action( 'pre_get_posts', 'change_archive_query' );
    

    You can put that in your functions.php. In archive.php you can remove query_posts