Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to display sticky post from a certain category? [Mimbo 3.0] WordPress

  • SOLVED

Hello, I'm running [[LINK href="http://www.darrenhoyt.com/2007/08/05/wordpress-magazine-theme-released/"]]Mimbo Free 3.0[[/LINK]] theme on my site.

By default, the lead area displays sticky OR latest post. I would like to change the code to display the sticky but only have posts from certain category in the loop.

The code for this part in the theme looks like this:


$postCount = 0;
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( 'paged=$page&post_per_page=-1&cat=' . get_query_var('cat') );
while (have_posts()) { the_post();
if( $postcount == 0 ) { /* sticky post stuff goes here */ }
else { /* rest of the loop goes here */ }
$postcount ++;


Naturally, I tried to change the query to:

query_posts('paged=$page&post_per_page=-1&cat=4,' . get_query_var('cat'));

but that doesn't work - it ends up displaying latest, not sticky post in the lead area.

Basically, what I'm facing is having EITHER sticky post from all categories OR latest post from selected category. How to change this query to display sticky from a selected category?

Thanks in advance,

Justine

Answers (3)

2010-06-14

Xavier Faraudo answers:

I'd suggest that you use three "queries": one for sticky posts, other for the category, then merge both in another one.

Let's start with:

$post_ids = array();
$cat_posts = get_posts( 'paged=$page&posts_per_page=-1&cat=' . get_query_var('cat') );
if( $cat_posts ){
foreach( $cat_posts as $cat_post ){
$post_ids[] = $cat_post->ID;
};
};

We got here all the post ids for those in cat in the array. Now we'll add the stickies:

$stickies = get_posts ( array( 'cat' => get_query_var('cat') ,'post__in' => get_option("sticky") , 'posts_per_page' => -1 ) );
if( $stickies ){ foreach( $stickies as $sticky ){
$post_ids[] = $sticky->ID;
}; };

Now, all the post ids are in the array. We can query_posts (and populate $wp_query, and all those things that query_posts does which get_posts doesn't).

$posts = query_posts( array( 'post__in' => $post_ids ) );

Please note that this can be specially useful in combination with rewind_posts(). You can get all the posts (ordered) which comply the criteria, loop them and do something only to, say, stickies, and then rewind_posts() and do something different checking if posts are not stickies, and/or are in a given category, etc.

2010-06-12

Oleg Butuzov answers:

cate array values better to use array keys..
like

query_posts(array('category__in'= > array(4, get_query_var('cat'))));

sticky

query_posts(
array(
'category__in' => array(4, get_query_var('cat')),
'post__in' => get_option("sticky")
)
);


not sure as for true is sticky option calls stliky but you can change it.


Justine comments:

Neither of these worked, it keeps displaying the latest post from category 4 instead of the sticky one.

2010-06-12

Utkarsh Kukreti answers:

What is the code inside

/* sticky post stuff goes here */


Justine comments:

Hi Utkarsh,

This is the whole block of code: [[LINK href="http://pastie.org/1001644"]]http://pastie.org/1001644[[/LINK]]


Utkarsh Kukreti comments:

query_posts(array(
'cat' => 4,
'post__in' => get_option('sticky_posts')
));


Justine comments:

This managed to return the sticky from a category 4. Thanks.

But the problem is, the whole block of code is a loop for all the posts in this category. The first post (which should be the sticky) is displayed in the "lead" div, then goes a couple of next posts, then the list of even more posts, all in one loop.

You can see demo of this theme [[LINK href="http://www.darrenhoyt.com/demo/wordpress/"]]here[[/LINK]]. All the posts on this page (featured, recent posts, older posts) are returned using just one loop.

So if I use your query, I will only get the sticky post in it.