• Resolved teatall

    (@teatall)


    Hi guys!

    Hate to bother but I really need some help.

    I used query_posts to pull sticky posts under a certain category. Here’s what I want, but it seems to be beyond me.

    Let’s say there are 10 sticky posts and 20 normal posts under this category.
    Is it possible to have the 5 latest sticky posts at the top of the list while the other 5 will be listed together with the 20 normal posts in the order of time?

    Any help will be appreciated!

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must use two loops to do this. In the first loop, set posts_per_page to 5 and ignore_sticky_posts to 0. In that loop, add all IDs found to a ‘$do_not_duplicate’ array’.

    In the second loop, set ignore_sticky_posts to 1, and post__not_in to the $do_not_duplicate array.

    Thread Starter teatall

    (@teatall)

    thanks buddy!
    that’s quite illuminating.

    Thread Starter teatall

    (@teatall)

    I tried your method a couple of times, but still failed. could you plz help me look at the following code and tell me where should i put the ‘$do_not_duplicate’ array?

    i now would like to have three latest sticky posts at the top of the list.

    $sticky=array(
                            'cat'=>2,/*category to be displayed*/
                            'showposts'=>3,/*number of posts to be displayed*/
                            'post__in'=>get_option('sticky_posts'));
    $others=array(
                            'cat'=>2,/*category to be displayed*/
                            'showposts'=>7,/*number of posts to be displayed*/);
    /*the query*/
    query_posts( $sticky );
    /*the 1st loop*/
    while ( have_posts() ) : the_post();
        echo '<li><a href="';
        the_permalink();
        echo '" title="';
        the_title();
        echo '">';
        get_short_title();
        echo '</a><span>';
        the_time('Y-m-d');
        echo '</span>';
    endwhile;
    /*reset query*/
    wp_reset_query();
    /*the 2nd query*/
    query_posts( $others );
    /*the loop*/
    while ( have_posts() ) : the_post();
        echo '<li><a href="';
        the_permalink();
        echo '" title="';
        the_title();
        echo '">';
        get_short_title();
        echo '</a><span>';
        the_time('Y-m-d');
        echo '</span>';
    endwhile;
    /*reset query*/
    wp_reset_query();

    thanks again!

    Thread Starter teatall

    (@teatall)

    I have already solved the problem. thanks a lot!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘query_posts and sticky posts’ is closed to new replies.