How to Sort/Show Sticky Posts on Top of Post List with get_posts() in WordPress

It is unfortunately that WordPress doesn’t have parameter to sort based on sticky status. To show them on top of a list, we need to query them separately then merge with other normal post list.

$sticky_args = array( 
		'post_type' 	   => 'post', 
		'post_status'      => 'publish',
		'posts_per_page'   => 10,
		'offset'           => 0,
		'include_sticky_posts' => true,
		'include' => implode(',', get_option('sticky_posts')),
	);

$args = array( 
		'post_type' 	   => 'post', 
		'post_status'      => 'publish',
		'posts_per_page'   => 10,
		'offset'           => 0,
		'include_sticky_posts' => false,			
	);

$sticky_posts = get_posts($sticky_args);
$posts = get_posts($args);

$all_posts = array_merge($sticky_posts, $posts);

foreach ( $all_posts as $p ){
	//show data
}

include_sticky_posts parameter determines whether the query includes sticky posts in result or not. So, for the sticky post list, it must be set to true, and for the normal post list, it should be false to avoid query the same posts on 2 lists.

include parameter determines a list of posts to be included in result. We get those sticky posts’ IDs from option table with get_option('sticky_posts').

Leave a Comment

Your email address will not be published. Required fields are marked *

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close