Move sticky posts down in main loop

Currently I am working for a site which runs on WordPress, and using twenty thirteen theme which already has a sticky post function built in and working reasonably well.

But I do not like how a sticky post is always displayed on absolute top. I would rather be able to display the latest one or two or three posts, then the featured/sticky posts. How can I do this?

Related posts

1 comment

  1. You have to change the order in $wp_query->posts. Compare it to the values from get_option( 'sticky_posts' ).

    Example:

    function move_stickies_down( $num = 1 )
    {
        global $wp_query;
    
        if ( empty ( $wp_query->posts ) )
            return;
    
        $stickies = get_option( 'sticky_posts' );
    
        if ( empty ( $stickies ) )
            return;
    
        $sticky_posts = $top = $after = array();
    
        foreach ( $wp_query->posts as $p )
        {
            if ( in_array( $p->ID, $stickies ) )
            {
                $sticky_posts[] = $p;
            }
            elseif ( $num > 0 )
            {
                $top[] = $p;
                $num -= 1;
            }
            else
            {
                $after[] = $p;
            }
    
        }
    
        $wp_query->posts = array_merge( $top, $sticky_posts, $after );
    }
    

    Add this function to your functions.php and call it before the loop like this:

    move_stickies_down( 2 );
    
    if ( have_posts() )
    {
        while ( have_posts() )
        {
            the_post();
            // show post
        }
    }
    

Comments are closed.