• Hi!

    I’m using

    $sticky=get_option('sticky_posts') ;
    	query_posts('p=' . $sticky[0]);

    to get the last sticky home

    but i want to show on the same page, another block of posts without the last sticky post that it’s already printed…

    any ideas?

    thanks in advance!

Viewing 14 replies - 1 through 14 (of 14 total)
  • You can add a query_posts before the next query loop and use $sticky[0] in an exclude= parameter

    Thread Starter mzuker

    (@mzuker)

    Something like this?

    //The Query
    $args=array(
    ‘excude’=> $sticky[0],
    ‘showposts’=>4 ,
    ‘caller_get_posts’=> 1,
    );
    query_posts($args);

    Using that i’m still getting the sticky post

    thanks!

    Thread Starter mzuker

    (@mzuker)

    I mean, the last sticky post

    you spelled exclude wrong

    Thread Starter mzuker

    (@mzuker)

    Yes stvslf i know

    but it isn’t working, even well spelled ??

    //The Query
    	$args=array(
       'caller_get_posts'=>1,
       'showposts'=>4,
       'exclude'=> $sticky[0],
       );
    	query_posts($args);

    is that ok?

    Hi

    I had not done the particular thing you are looking for before. I got it working using this code. Explanation below

    <?php $sticky = get_option('sticky_posts') ;
    $post_to_exclude[] = $sticky[0];
    
    $args=array(
       'caller_get_posts'=>1,
       'showposts'=>4,
       'post__not_in'=> $post_to_exclude,
    );
    query_posts($args); ?>

    caller_get_posts=1 means do not display sticky posts at the top of the post list. They will still be included in the natural order they would appear in the post listing if they were not sticky.

    post__not_in requires an array as argument that is a list of the posts to be excluded. $sticky[0] is not an array, so I created a new array $post_to_exclude with one element, the value of $sticky[0]. That worked when I ran the code.

    There doesn’t seem to be a way to exclude one sticky post and have the rest of the sticky posts appear at the top of the page – you can have them appear in chronological order within the posts, not as stickies.

    If it turns out you have other stickies you want excluded you can add their post ID’s to the $post_to_exclude array.

    Thread Starter mzuker

    (@mzuker)

    I think that using your code but having the array ordered by date should be enought…

    is that possible?

    because the ID of the array used in post_to_exclude it isn’t ordered by the date of the posts

    thank for your great support here!

    I doubt that ordering the array by date will make any difference. Its simply a list of posts not to include.

    Another approach is to have one loop that is only sticky posts, and a second loop that excludes all sticky posts

    $sticky=get_option('sticky_posts');
    $args=array(
       'cat'=>3,
       'caller_get_posts'=>1,
       'post__not_in' => $sticky,
       'paged'=>$paged,
       );

    this code excludes all sticky posts from a loop. Then you could display the sticky posts in a loop before that one, arranged in whatever order you want.

    Thread Starter mzuker

    (@mzuker)

    i’m using this code `<?php $sticky = get_option(‘sticky_posts’) ;
    $post_to_exclude[] = $sticky[0];

    $args=array(
    ‘caller_get_posts’=>1,
    ‘showposts’=>4,
    ‘post__not_in’=> $post_to_exclude,
    );
    query_posts($args); ?>`

    but sometimes if there are more than one sticky post selected, it isn’t displayed the last one. i dunno why…

    that’s why i told you if arranging by date could fix that

    Hi

    When you refer to “last sticky post” do you mean by that the most recent one?

    I’ve been going over the WP sticky_posts code and I don’t see yet what determines the order in which they appear.

    What is it you are trying to do? Do you have more than one sticky post? Are you trying to display only one of them and then exclude that from the loop of the rest of the posts? Is it always the same sticky post that you want to display? Do you always know the post ID # of that sticky post?

    Do you always want to display the same post at the top, or does the post you want displayed at the top change from time to time?

    Thread Starter mzuker

    (@mzuker)

    Yes!, I want to show the most recent post marked as sticky.

    And then, display the rest of the posts excluding the most recent post marked as sticky.

    Sometimes i’ve got more than one post marked as sticky, thats because i want to display only the most recent one.

    It’s not the same post…i can’t hardcode it.

    thanks!

    If you only want to display the most recent sticky post and not include any sticky posts at all in the display of all the other posts, I’d suggest having two loops.

    The first would display the most recent sticky

    <?php
     $sticky=get_option('sticky_posts') ;
     $recentSticky = new WP_Query();
     $recentSticky->query("p=$sticky[0]");
     while ($recentSticky->have_posts()) : $recentSticky->the_post(); ?>
       <h2><a href="<?php the_permalink(); ?>" <?php the_title(); ?></a></h2>
       <?php the_content();
    endwhile;
    ?>

    That code goes before the WP loop

    The second loop to display all the other posts excluding all stickies

    <?php $sticky=get_option('sticky_posts');
    $args=array(
       'cat'=>3,
       'caller_get_posts'=>1,
       'post__not_in' => $sticky,
       'paged'=>$paged,
       );
    query_posts($args); ?>

    That would go after the sticky code and before the main WordPress loop.

    Thread Starter mzuker

    (@mzuker)

    Yes, the first code it’s right, but on the second loop i want to include all the post excluding the one that it’s printed using the code on the first loop.

    Filtering all the stickies it’s not the solucion, because i only want to filter the most recent one ??

    well then we are back where we started, with the original solution.

    You said the problem with that one was that sticky[0] was not always the top post.

    If these stickies are not displaying anywhere except on this one page, you might consider an approach that does not involve stickies. If you had a category for “featured post”, and used two loops as I detailed above, you could assign the post you want featured to that category, and have the first loop display that category. There would only be one post in that category. When you want to switch to a new featured post, take the old one out of that category and assign the new post to that category.

    The second loop query would exclude that category from displaying. This would be much easier to program than all this sticky code.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Exclude last sticky post’ is closed to new replies.