• Hi,

    On this closed topic Chip Bennett advises strongly to use pre_get_posts, but I can’t find out how to combine the function he describes with my Loop.

    What do I need to change in my Loop? It looks like this now, and Chip will be mad at me, but I want to ignore sticky posts, and all other solutions I found online break pagination:

    <?php global $query_string; query_posts( $query_string . '&ignore_sticky_posts=1'); ?>
    
     <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    	<?php get_template_part( 'content', get_post_format() ); ?>
    
    <?php endwhile; else: ?>
    <?php endif; ?>

    I already put the function Chip describes in my functions.php:

    function breatheout_filter_pre_get_posts( $query ) {
        if ( $query->is_main_query ) {
            $query->set( 'ignore_sticky_posts', '1' );
        }
    }
    add_action( 'pre_get_posts', 'breatheout_filter_pre_get_posts' );

    Thanks for your time!

Viewing 5 replies - 1 through 5 (of 5 total)
  • So, what happens? Nothing? Something?

    A couple of debugging things:

    1) ignore_sticky_posts is Boolean, so try this instead:

    $query->set( 'ignore_sticky_posts', true );

    2) Be sure to target only the front end, in the correct context:

    All contexts on the front end:

    if ( ! is_admin() && $query->is_main_query() ) {
    
    }

    Just the blog posts index:

    if ( $query->is_home() && $query->is_main_query() ) {
    
    }

    3) Make sure you remove all references to query_posts() in the template, and output only the default loop instantiation.

    p.s. Mad? Nah. Ain’t nobody got time for that!

    Thread Starter vandenb

    (@vandenb)

    Ah, what happened: I put the code you described in the other post in my functions.php and made my Loop ‘normal’, and I saw sticky posts on top of my latest posts.

    This is my blog: https://vandenb.com/
    And where it says at the bottom ‘de goeie stukjes’, i’m showing my stickies.
    I’m using the dangerous code now. But I don’t want to.

    The problem I have: I’m just beginning with WordPress, have no php-experience, and a lot of things that should help me on this forum or in the Codex are pretty hard to grasp, because people say: here, use this code. But very often they don’t give any context. So I don’t know where to go with that code.

    I think it’s really really great all these people out here are helping us n00bs, but ouch, I feel stupid now, I have to ask:

    Where do I use the code examples you are giving me? How should my Loop be? And my functions.php?

    I was looking for the same thing as you, Vandenb. In my case, I’m using Sticky Posts only to select which posts are to go in a slider on the front page. But otherwise I do not want them to appear at the top any time I call The Loop. They should be in chronological order with the rest of the posts.

    So using Chip’s recommendation, I added this to my theme’s functions.php:

    //Makes sure sticky posts don't get placed at the top of The Loop
    function nostickies_filter_pre_get_posts( $query ) {
        if ( ! is_admin() && $query->is_main_query() ) {
            $query->set( 'ignore_sticky_posts', true );
        }
    }
    add_action( 'pre_get_posts', 'nostickies_filter_pre_get_posts' );

    That way I don’t have to change anything in The Loop, which looks something like this:

    <?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
    
    			<div class="post">
    				<p class="date"><?php the_time('d M Y'); ?></p>
    				<h2 class="posttitle"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
    				<div class="postcontent"><?php the_content(); ?></div>
    			</div>
    
    			<?php endwhile; ?>
    			<?php endif; ?>

    And it works!

    I don’t have pagination currently but I’ll implement it to make sure nothing’s broken there and let you know the results.

    I installed the WP-PageNavi plugin and there’s no issue with its pagination that I can see. Thanks for the help, Chip!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Using pre_get_posts’ is closed to new replies.