logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

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

Hello, I'm running Mimbo Free 3.0 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

This question has been answered.

Justine | 06/12/10 at 4:28am Edit


(3) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    06/12/10
    4:35am
    Oleg Butuzov says:

    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.

    Previous versions of this answer: 06/12/10 at 4:35am

    • 06/12/10 4:44am

      Justine says:

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

  • avatar
    Last edited:
    06/12/10
    4:34am
    Utkarsh Kukreti says:

    What is the code inside

     /* sticky post stuff goes here */

    • 06/12/10 4:37am

      Justine says:

      Hi Utkarsh,

      This is the whole block of code: http://pastie.org/1001644

    • 06/12/10 5:09am

      Utkarsh Kukreti says:

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

    • 06/12/10 5:16am

      Justine says:

      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 here. 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.

  • avatar
    Last edited:
    06/16/10
    3:46pm
    Xavier Faraudo says:

    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.

This question has expired.





Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.