5

I am having alot of trouble with this. I was using <?php query_posts( array( 'post__not_in' => get_option( 'sticky_posts' ), 'paged' => get_query_var( 'paged' ) ) ); ?> to exclude the posts from the main query on the my homepage because stickies are being used in the slider. I am making this theme for wordpress.org and I was told that was not recommended. I then tried adding this to my functions.php but to no avail:

/**
 * Excluding sticky posts from home page. Sticky posts are in a slider.
 *
 * @since 0.1
 */
function essential_exclude_sticky( $query ) {

    /* Exclude if is home and is main query. */
    if ( is_home() && $query->is_main_query() )
        $query->set( 'ignore_sticky_posts', true );

}

`

Any idea on what I am doing wrong?

5

query_posts isn't recommended because its breaks things.

You're really close, but just declaring the function itself will not work. You need to hook the function into something. In your case, this would be pre_get_posts.

Example (with "namespaced" function):

<?php
// this is key!
add_action('pre_get_posts', 'wpse74620_ignore_sticky');
// the function that does the work
function wpse74620_ignore_sticky($query)
{
    // sure we're were we want to be.
    if (is_home() && $query->is_main_query())
        $query->set('ignore_sticky_posts', true);
}
  • Works like a charm. Thank you for explaining it, I need to learn these things! ;p – Steven Dec 2 '12 at 3:51
  • 1
    @Steven Click the check mark to mark this answer as The Solution. – fuxia Dec 2 '12 at 9:02
3

For me, using ignore_sticky_posts keeps sticky posts from showing at the top, but they still show in chronological order with the other posts.

I'm using post__not_in with get_option('sticky_posts') to exclude sticky posts from the main query.

<?php
/**
 * Exclude sticky posts from home page.
 */
function theme_name_ignore_sticky_posts($query){
  if (is_home() && $query->is_main_query())
    $query->set('post__not_in', get_option('sticky_posts'));
}
add_action('pre_get_posts', 'theme_name_ignore_sticky_posts');
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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