I thought the sticky feature was for front page only?
According to the documentation: http://codex.wordpress.org/Template_Tags/query_posts#Sticky_Post_Parameters
“Posts that are set as Sticky will be displayed before other posts in a query, unless excluded with the caller_get_posts=1 parameter.”
It doesn’t mention anything about being specific about which page it’s been called on, unless the Codex is wrong.
great – hope someone knows more about this
Okay, done a little bit of playing around, stickies work when a category isn’t specified. It appears as if you can’t get stickies for particular categories, how annoying.
Okay, if anyone else comes across this, I’ve written some code that implements what I want:
function get_posts_with_stickies($cat, $num)
{
global $post;
$finalposts = array();
$stickiesID = array();
// append relevant stickies to the front
query_posts(array('post__in'=>get_option('sticky_posts')));
while (have_posts())
{
the_post();
if (in_category($cat) && $post->post_status == 'publish')
{
$finalposts[] = $post;
$stickiesID[] = $post->ID;
}
}
// if any of the stickies are in what we've got, remove them
$args = array(
'category' => $cat,
'numberposts' => $num
);
$posts = get_posts($args);
foreach ($posts as $post)
{
if (!in_array($post->ID, $stickiesID))
{
$finalposts[] = $post;
}
}
// check we've not exceeeded the max length
while (count($finalposts) > $num)
{
array_pop($finalposts);
}
return $finalposts;
}
Basically, pop this in your theme’s functions.php, and then call get_post_with_stickies with the first argument as your category ID, and the second as the number of posts to return, and it will return a list of posts, with anything stickied in that category at the front.
thanks – appreciate you coming back with this
hey chris, thanks for the code, but for some reason it doesn’t work for me!
I stuck in the function in my functions.php, and called out in a specific category page this:
get_posts_with_stickies(9,3)
What happens after that is that my category page (in this instance category-3.php) only has one post – the sticky post, but not the rest.
Am I doing something wrong?
Thanks in advance!
Also I forgot to mention that that category is a child category, just in case it affects it.
Try using this plugin. Its just what I needed and solved my problem.
http://wordpress.org/extend/plugins/astickypostorderer/
Hi morticya33 – sorry, didn’t realise you’d posted in this thread.
Actually, the code I’ve posted above does have this bug. Here’s the fixed version which I used (sorry for not updating it on here)
function get_posts_with_stickies($cat, $num)
{
global $post;
$finalposts = array();
$stickiesID = array();
// append relevant stickies to the front
if (count(get_option('sticky_posts')) > 0)
{
query_posts(array('post__in' => get_option('sticky_posts')));
$iscomma = strpos($cat, ',');
if ($iscomma === false)
{
$maincat = $cat;
}
else
{
$maincat = substr($cat, 0, $iscomma);
}
while (have_posts())
{
the_post();
if (in_category($maincat) && $post->post_status == 'publish')
{
$finalposts[] = $post;
$stickiesID[] = $post->ID;
}
}
}
// if any of the stickies are in what we've got, remove them
$args = array(
'cat' => $cat,
'showposts' => $num,
'caller_get_posts' => 1
);
query_posts($args);
while (have_posts())
{
the_post();
if (!in_array(get_the_ID(), $stickiesID))
{
$finalposts[] = $post;
}
}
// check we've not exceeeded the max length
while (count($finalposts) > $num)
{
array_pop($finalposts);
}
return $finalposts;
}