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?
2 s
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);
}