Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the following query, trying to fetch the latest sticky posts but exclude the current one:

$sticky = get_option('sticky_posts');
rsort( $sticky );
$sticky = array_slice($sticky, 0, 3);

query_posts(
    array(
        'post__not_in' => array($post->ID),
        'post__in' => $sticky,
        'caller_get_posts' => 1,
    )
);

Getting the posts works fine, but post__not_in doesn't. Is there an effective way to do this?

share|improve this question
3  
Read this. Note: you cannot combine post__in and post__not_in in the same query. –  t f Apr 18 '13 at 23:16
    
And this. –  Johannes Pille Apr 18 '13 at 23:17
add comment

1 Answer

up vote 2 down vote accepted

This should do:

// Get sticky posts
$sticky = get_option('sticky_posts');
rsort($sticky);
$sticky = array_slice($sticky, 0, 3);

// Check if current post is inside
foreach ($sticky as $key => $value)
    if ($value === $GLOBALS['post']->ID) {
        // ... and remove
        unset($sticky[$key]);
        break;
    }

$query = new WP_Query(
    array(
        'post__in' => $sticky,
        'ignore_sticky_posts' => 1,
    )
);

If you want to have 3 posts (no matter what), you have to do it like so (minimally changed):

// Get sticky posts
$sticky = get_option('sticky_posts');
rsort($sticky);

// Check if current post is inside
foreach ($sticky as $key => $value)
    if ($value === $GLOBALS['post']->ID) {
        // ... and remove
        unset($sticky[$key]);
        break;
    }

$sticky = array_slice($sticky, 0, 3);

$query = new WP_Query(
    array(
        'post__in' => $sticky,
        'ignore_sticky_posts' => 1,
    )
);

// EDIT: added break after current post ID has been found and removed, in case you have a great number of sticky posts.

share|improve this answer
1  
Works like charm :D Thanks alot for the effort and patience –  Lucian Davidescu Apr 19 '13 at 12:10
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.