Skip to content

Instantly share code, notes, and snippets.

@rambuvn
Created May 31, 2012 15:02
Show Gist options
  • Save rambuvn/2843990 to your computer and use it in GitHub Desktop.
Save rambuvn/2843990 to your computer and use it in GitHub Desktop.
Wordpress get next, preivous attachment
function rb_get_next_attachment( $post_id, $previous = false ){
global $wpdb;
$post = get_post($post_id);
$current_post_date = $post->post_date;
if( $previous ){
$order = 'DESC';
$where = "p.post_date < '$current_post_date'";
}else{
$order = 'ASC';
$where = "p.post_date > '$current_post_date'";
}
$query = "SELECT p.* FROM wp_posts AS p
INNER JOIN wp_term_relationships AS tr
ON p.ID = tr.object_id
INNER JOIN wp_term_taxonomy tt
ON tr.term_taxonomy_id = tt.term_taxonomy_id
AND tt.taxonomy = 'category'
WHERE $where
AND p.post_type = 'attachment'
AND p.post_status = 'inherit'
AND tt.taxonomy = 'category'
ORDER BY p.post_date $order LIMIT 1";
$result = $wpdb->get_row($query);
if ( null === $result )
$result = '';
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment