I’ve got a custom query on my homepage showing all posts in a certain category. I need this query to respect sticky posts, but it seems from my research that category queries ignore stickyness. My question is two (and a half) fold:
- Can anyone tell me where/how in the database stickyness is applied to a post? I don’t see it in
wp_postmeta
orwp_posts
. This one is most important and will probably be enough to get you thewinaccepted answer. - Is there any simple elegant way to grab sticky posts only from a certain category?
- If not, then how about an ugly way to do it?
Just for the heck of it, here’s my query, though I think it won’t make a difference to the answer.
$getHighlights = array(
'posts_per_page' => 7,
'post_type' => array('post','Event'),
'category_name' => 'Highlights',
);
Sorry for the long title, but I wanted to be clear what I was asking for.
Just add
'post__in' => get_option('sticky_posts')
to your query, to confine your query to only sticky posts. So,should work for you.
Edit: This is how you can merge two arrays to get the sticky posts at the top of your query:
This will show 7 posts, with the stickies at the top (of course, this is assuming you don’t have more than 7 stickies, otherwise it’ll be all messed up…) (edited to use
numberposts
as per OP’s comment below…)Sticky is not saved per post, there is
sticky_posts
option that holds array of such. So you can fetch that with$sticky_posts = get_option('sticky_posts');
See Sticky Post Parameters in Codex for details on querying stickies.
I am not entirely sure what would be the best approach to make your query respect stickies. From quick look at code it’s likely
is_home
conditional gets overridden and that kicks stickies off. Might be able to hook somewhere and fake that conditional, but this might break something further in processing.