I have the same problem with sticky posts in 2.9.2 – but using get_posts() for the custom loop.
Sticky Post Parameters
Sticky posts first became available with WordPress Version 2.7. Posts that are set as Sticky will be displayed before other posts in a query, unless excluded with the caller_get_posts=1 parameter.
* array(‘post__in’=>get_option(‘sticky_posts’)) – returns array of all sticky posts
* caller_get_posts=1 – To exclude sticky posts being included at the beginning of posts returned, but the sticky post will still be returned in the natural order of that list of posts returned.
To return just the first sticky post:
$sticky=get_option('sticky_posts') ;
query_posts('p=' . $sticky[0]);
or
$args = array(
'posts_per_page' => 1,
'post__in' => get_option('sticky_posts'),
'caller_get_posts' => 1
);
query_posts($args);
Note: the second method returns only the more recent sticky post; if there are not sticky posts, it returns the last post published.
To return just the first sticky post or nothing:
$sticky = get_option('sticky_posts');
$args = array(
'posts_per_page' => 1,
'post__in' => $sticky,
'caller_get_posts' => 1
);
query_posts($args);
if($sticky[0]) {
// insert here your stuff...
}
OK – here is why sticky posts are not shown when you query a category.
When you use a category query, is_archive() returns true and is_home() returns false. Sticky posts are only honored when is_home() is true.
There may be a better way, but here is a kludgy work-around. Put this ahead of the call to query_posts(), or get_posts().
WARNING: Only minimally tested.
function fake_is_home($args) {
// Really doesn't filter posts_results.
// Set is_home = 1 to allow sticky posts to be honored.
global $wp_query;
$wp_query->is_home = 1;
return $args;
}
add_filter('posts_results','fake_is_home');
vtxyzzy, thanks for taking the time to respond to this thread.
When you use a category query, is_archive() returns true and is_home() returns false. Sticky posts are only honored when is_home() is true.
I have a posts page where the stickies work correctly out of a standard wordpress loop. However, the template sets is_archive and is_home as follows:
`$wp_query->is_archive = true;
$wp_query->is_home = false;`
For my original sticky post/custom loop problem with get_posts, in line with vtxyzzy’s original suggestion, I have resorted to using two custom loops – one for stickies and one for the remaining category posts – and use posts__in and posts__not_in to include or filter out the stickies respectively.
Though it still bugs me that I can’t get the stickies to work as documented!
The other concern is that the twin custom loops approach to stickies doesn’t seem to respond to the posts_per_page parameter, eg.
‘$news_posts = get_posts(
array(‘posts_per_page’ => 1,
‘post__in’ => get_option(‘sticky_posts’),
‘order’ => ‘ASC’,
‘cat’ => 6)
);’
displays 2 stickies. Using the deprecated showposts works as expected.
Yes, I have found that showposts works with get_posts while posts_per_page does not. Probably a bug.
You might try the plugin A Sticky Post Orderer to see if it will do what you want.