• Resolved Cedric

    (@cooperanet)


    Hi,

    I meet a bug with my query_posts loop. The same loop is used for index.php, category.php and tag.php

    On my index, when I click on a tag link or a category link in order to display the list of posts related to this tag or category, I’ve got the same results than the index and not the list of the posts of the category or the tag.

    <?php query_posts( array( 'ignore_sticky_posts' => 1, 'paged' => get_query_var('paged') ) ); ?>
    	<?php if ( have_posts() ) : ?>
    	<?php while ( have_posts() ) : the_post(); ?>
    
    <!-- do stuff here -->
    
    	<?php endwhile; ?>
    	<?php else : ?>
    	<!-- message -->
    	<?php endif; ?>
    	<?php wp_reset_query(); ?>

    When I remove the query_posts, it works fine.

    Do you have any idea of what could be the problem ?

    Any help would be greatly appreciated !

Viewing 15 replies - 1 through 15 (of 15 total)
  • When I remove the query_posts, it works fine.

    What do you mean?
    Also what theme are you using?
    Regards

    Do you have any idea of what could be the problem ?

    Yes: query_posts() is the problem! Don’t use it. πŸ™‚

    What are you trying to accomplish? We’ll help you implement a better method.

    Thread Starter Cedric

    (@cooperanet)

    Hello jnhghy,

    Thanks for your reply.

    It is a customized theme.

    I mean that when I only use the classic loop ( if (have_posts() … ) without the query_posts($args), it works fine.

    But when using the query_posts($args), it always return the same results…

    Thread Starter Cedric

    (@cooperanet)

    Hi Chip,

    Thanks for your reply.

    I will try with WP_Query and see if it returns the same “error”.

    Assuming that you simply want to ignore sticky posts always:

    function child_theme_ignore_sticky_posts( $query ) {
        $query->query_vars['ignore_sticky_posts'] => 1;
    }
    add_action( 'pre_get_posts', 'child_theme_ignore_sticky_posts' );

    If you want to conditionally ignore sticky posts, let me know what those conditions are.

    @chip: Why is it batter not to use query_posts and implement anything else?

    Thread Starter Cedric

    (@cooperanet)

    Ok, it seems that i get the same results with WP_Query().

    I just want to avoid sticky posts to get on top.

    Thread Starter Cedric

    (@cooperanet)

    @chip : using pre_get_posts returns me an internal server error 500. Weird…

    @chip : using pre_get_posts returns me an internal server error 500. Weird…

    Sorry; that was an error on my part. I’ve fixed the callback:

    function child_theme_ignore_sticky_posts( $query ) {
        $query->query_vars['ignore_sticky_posts'] => 1;
        // Forgot to return!
        return;
    }
    add_action( 'pre_get_posts', 'child_theme_ignore_sticky_posts' );

    @chip: Why is it batter not to use query_posts and implement anything else?

    Because using query_posts() is the worst possible method for modifying the main query. For several reasons. Nacin gave a great presentation about it recently; I’ll see if I can find his presentation slides, and link them here.

    Found Nacin’s slides from his WC Portland 2011 presentation:

    And one additional question: where did you put the code I posted? Did you put it in your template file, or did you put it in functions.php. (Note: it belongs in functions.php, not in a template file.)

    Thread Starter Cedric

    (@cooperanet)

    Hi Chip,

    I put it in my functions.php file but it now returns an internal server error.

    So I guess that there is a conflict problem somewhere else.

    Okay, let’s take one more stab at that callback:

    function child_theme_ignore_sticky_posts( $query ) {
        // Only modify the main query
        if ( $query->is_main_query() ) {
            // Use Nacin's syntax
            $query->set( 'ignore_sticky_posts', 1 );
        }
    }
    add_action( 'pre_get_posts', 'child_theme_ignore_sticky_posts' );

    Also: can you enable WP_DEBUG, and let me know if you get any specific Fatal Error messages?

    Thread Starter Cedric

    (@cooperanet)

    Hi Chip,

    Sorry for my late response!

    Thank you so much for your piece of code, it worked like a charm!

    A simple but efficient way to resolve my issue as I only need to avoid sticky posts to show on top of my main loop.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘query_posts() troubleshooting’ is closed to new replies.