Skip to main content
2 of 2
added 80 characters in body
Milo
  • 78.8k
  • 4
  • 127
  • 168

This may get a bit complicated, I would recommend not adding the sticky feature to other post types if you want to query them specifically, and instead using post meta to designate custom types as "sticky". If you use the sticky feature on multiple post types, you'll get unexpected results with the following code, as sticky posts are stored as an array of post IDs independent of the type of post they refer to. If you're not using the sticky feature on other post types, then the following code should work as expected.

First, sticky posts are saved in the option 'sticky_posts', to get a limited number of these IDs, we need to slice the array to grab only the amount we want, and then we pass that as the post__in argument of our query.

Second, sticky posts are added to a query on top of what you specify in the query, so one of the parameters we need to set is to ignore sticky posts, which seems counter intuitive, but we want to explicitly query for these posts ourselves, not have them added onto the query.

Lastly, since this is an additional query, we don't want to use query_posts, we use a new WP_Query instead.

// limit the number to our mini features number
$sticky = array_slice( get_option('sticky_posts'), 0, $mini_features_number );

$mini_features = new WP_Query(array(
    'post_type' => 'infobox',
    'post__in' => $sticky,
    'ignore_sticky_posts' => 1,
    'posts_per_page' => $mini_features_number // possibly redundant, but in case it's larger than default posts_per_page
));

while( $mini_features->have_posts() ):
    $mini_features->the_post();
    // your loop stuff
endwhile;
Milo
  • 78.8k
  • 4
  • 127
  • 168