Skip to content

Instantly share code, notes, and snippets.

@rickalday
Created March 20, 2015 23:26
Show Gist options
  • Save rickalday/e4b846bd0eab7fcbf48a to your computer and use it in GitHub Desktop.
Save rickalday/e4b846bd0eab7fcbf48a to your computer and use it in GitHub Desktop.
Ge post siblings
function get_post_siblings( $limit = 1, $date = '' ) {
global $wpdb, $post;
if( empty( $date ) )
$date = $post->post_date;
//$date = '2009-06-20 12:00:00'; // test data
$limit = absint( $limit );
if( !$limit )
return;
$p = $wpdb->get_results( "
(
SELECT
p1.post_title,
p1.post_date,
p1.ID
FROM
$wpdb->posts p1
WHERE
p1.post_date < '$date' AND
p1.post_type = 'sell_media_item' AND
p1.post_status = 'publish'
ORDER by
p1.post_date DESC
LIMIT
$limit
)
UNION
(
SELECT
p2.post_title,
p2.post_date,
p2.ID
FROM
$wpdb->posts p2
WHERE
p2.post_date > '$date' AND
p2.post_type = 'sell_media_item' AND
p2.post_status = 'publish'
ORDER by
p2.post_date ASC
LIMIT
$limit
)
ORDER by post_date ASC
" );
$i = 0;
$adjacents = array();
for( $c = count($p); $i < $c; $i++ )
if( $i < $limit )
$adjacents['prev'][] = $p[$i];
else
$adjacents['next'][] = $p[$i];
return $adjacents;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment