Skip to main content
added 157 characters in body
Source Link
tfrommen
  • 9.2k
  • 7
  • 39
  • 59

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,
    )
);
 

// EDIT
If 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.

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]);

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

// EDIT
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]);

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

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

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.

fixed mistake
Source Link
tfrommen
  • 9.2k
  • 7
  • 39
  • 59

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->ID === $GLOBALS['post']->ID)
        // ... and remove
        unset($sticky[$key]);

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

// EDIT
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->ID === $GLOBALS['post']->ID)
        // ... and remove
        unset($sticky[$key]);

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

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

// EDIT: now accessing global post object.

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->ID === $GLOBALS['post']->ID)
        // ... and remove
        unset($sticky[$key]);

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

// EDIT
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->ID === $GLOBALS['post']->ID)
        // ... and remove
        unset($sticky[$key]);

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

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

// EDIT: now accessing global post object.

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]);

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

// EDIT
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]);

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

$query = new WP_Query(
    array(
        'post__in' => $sticky,
        'ignore_sticky_posts' => 1,
    )
);
added 76 characters in body
Source Link
tfrommen
  • 9.2k
  • 7
  • 39
  • 59

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->ID === $post$GLOBALS['post']->ID)
        // ... and remove
        unset($sticky[$key]);

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

// EDIT
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->ID === $post$GLOBALS['post']->ID)
        // ... and remove
        unset($sticky[$key]);

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

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

// EDIT: now accessing global post object.

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->ID === $post->ID)
        // ... and remove
        unset($sticky[$key]);

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

// EDIT
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->ID === $post->ID)
        // ... and remove
        unset($sticky[$key]);

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

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

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->ID === $GLOBALS['post']->ID)
        // ... and remove
        unset($sticky[$key]);

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

// EDIT
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->ID === $GLOBALS['post']->ID)
        // ... and remove
        unset($sticky[$key]);

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

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

// EDIT: now accessing global post object.

added 501 characters in body
Source Link
tfrommen
  • 9.2k
  • 7
  • 39
  • 59
Loading
Source Link
tfrommen
  • 9.2k
  • 7
  • 39
  • 59
Loading