• 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)
  • Where did you put the code? It should work in functions.php, but not in index.php since that loaded is too late.

    But perhaps you should not modify the main query. Instead, you can modify that one loop and check is_sticky() just continuing (no output) if true.

    $query->set( 'ignore_sticky_posts', true ); – should work to remove the sticky post(s) from the front position, but they would still be shown in their original position in the main index loop.

    $query->set( 'post__not_in', get_option('sticky_posts') ); – should totally remove the sticky post(s) from the main index loop.

    does your theme’s index.php use a custom query for the posts on the blog page?
    can you post the full code of index.php of your theme?
    have you asked the developer of your theme?

    Thread Starter pierrehooker

    (@pierrehooker)

    @joyously I am setting it in my functions, not in index. Im saying the file this function should be acting on is index.php.

    @michael, I am (one of)the developers of our theme. Our starter theme is based on _s but with Bootstrap 4 and with our tooling integrated.

    Here is a fiddle of the blog index file:

    https://jsfiddle.net/znc73tah/

    your code looks ok.
    the only thing might be with your sticky query loop, which also includes all other stickies:

    try:

    $args = array(
    			'post__in' => $sticky,
    			'posts_per_page' => 1,
    			'ignore_sticky_posts' => true
    			);
    
    		$sticky_query = new WP_Query( $args );

    and this combination of all your attempts is what worked for me in functions.php:

    function remove_sticky_from_main_loop( $query ) {
    	if( is_home() && $query->is_main_query() && ! is_admin() ) {
    		$query->set( 'ignore_sticky_posts', true );
    		$query->set( 'post__not_in', get_option('sticky_posts') );
    	}
    }
    add_action( 'pre_get_posts', 'remove_sticky_from_main_loop' );
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.