What I want

When a user opens a category link, there should be automatically displayed the first queried post.

My query looks like this:

    <?php global $query_string;
            $posts = query_posts($query_string.'&posts_per_page=1&order=ASC'); ?>

        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <?php get_template_part( 'content', get_post_format() ); ?>
        <?php endwhile; endif; ?>

        <?php wp_reset_query(); ?>

The problem

The category URL displays the first queried content, but the wrong featured image, which is taken from the last queried post, meaning order=DESC is used.

Any idea how to solve this?

Maybe important

The featured images are set as full screen background images via backstretch.js by using

wp_enqueue_script( 'backstretch', get_bloginfo( 'stylesheet_directory' ) . '/js/jquery.backstretch.min.js', array( 'jquery' ), '1.0.0' );
wp_enqueue_script( 'backstretch-set', get_bloginfo('stylesheet_directory').'/js/backstretch-set.js' , array( 'jquery', 'backstretch' ), '1.0.0' );

wp_localize_script( 'backstretch-set', 'BackStretchImg', array( 'src' => wp_get_attachment_url( get_post_thumbnail_id() ) ) );
  • Try using WP_Query to get the posts, query_posts shouldn't be used, never. Also, I don't think you can call get_post_thumbnail_id, and at the point it's called there's no ID, so the get_the_ID() method should be failing, try turning debugging on.
    – Tomás Cot
    Sep 5, 2014 at 12:33

1 Answer 1


Do not use query_posts, and also do not replace the main query with a custom query. This causes more issues than solving the one issue you had before with the main query. I've done a complete post on these issues recently that you can check out here

According to your query, the only two things you need to change is posts_per_page and order to do this, you can simply use pre_get_posts to alter the query variables before the main query is executed. You can specifically target only category pages with the conditional tag is_category()

To solve this issue, return to the default loop on your category page. You'll just need the following on category.php, nothing more.

if (have_posts()) {
    while (have_posts()) {
        get_template_part( 'content', get_post_format() ); 

In your functions.php, use this code with pre_get_posts to alter the amount of posts per page and how they are ordered on category pages

function wpse_change_category( $query ) {
    if ( !is_admin() && $query->is_category() && $query->is_main_query() ) {
        $query->set( 'posts_per_page', '1' );
        $query->set( 'order','ASC' );
add_action( 'pre_get_posts', 'wpse_change_category' );
  • 1
    awesome! thanks for the hint with pre_get_posts. I am going to read about that. Thanks!
    – kabr
    Sep 5, 2014 at 14:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.