Excluding sticky posts from main query on index.php
-
Hello. I have a design where the sticky post is in a different area of the page from the main posts.
Creating a new query for the sticky posts was easy enough. However I have been trying to remove the sticky posts from the general posts query using pre_get_posts and nothing is working. I have found several different examples of this online and none of them seem to be doing anything:
/** * Remove sticky posts from main loop * @author Bill Erickson * @link https://www.billerickson.net/code/remove-sticky-posts-main-loop/ * * @param object $query */ function ea_remove_sticky_from_main_loop( $query ) { if( $query->is_main_query() && ! is_admin() ) { $query->set( 'ignore_sticky_posts', true ); } } add_action( 'pre_get_posts', 'ea_remove_sticky_from_main_loop' );
/** * Remove sticky from main posts loop */ add_action( 'pre_get_posts', 'custom_post_archive_changes' ); function custom_post_archive_changes( $query ) { if ( is_home() && $query->is_main_query() ) { // exclude sticky posts from main news page $stickies = get_option("sticky_posts"); $query->set( 'post__not_in', $stickies ); } }
/** * Remove sticky from main posts loop - resources */ function r55_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', 'r55_ignore_sticky_posts');
Has something in WordPress changed? This is on the page that is assigned as the posts page, show current template says im in index.php, and if I echo out some text conditionally using is_home() it works. What could I be doing wrong?
Viewing 4 replies - 1 through 4 (of 4 total)
Viewing 4 replies - 1 through 4 (of 4 total)
- The topic ‘Excluding sticky posts from main query on index.php’ is closed to new replies.