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.

Can I use WP_Query to only query posts less than (or greater than) a given post ID?

$filtered_query_args = array(
    'post_type' => 'projects',
    'order' => $prev_next=='next' ? 'DESC' : 'ASC',
    'orderby' => 'ID',
    // ID <= $post->ID
);

I've been digging around in the codex with no luck.

I can of course do this conditionally within the loop, but it would be much cleaner if I could use WP_Query directly.

share|improve this question
1  
Make use of the posts_clauses or posts_where filter. Dozens of examples on the page. –  kaiser Jun 19 '12 at 1:46

1 Answer 1

up vote 0 down vote accepted

I ended up using something like this:

function filter_where( $where = '' ) {
  global $post, $prev_next;
  $the_ID = $post->ID;
  $where .= " AND wp_posts.ID ";
  $where .= $prev_next=='next' ? "< $the_ID" : "> $the_ID";
  return $where;
}

add_filter( 'posts_where', 'filter_where' );
$nextProjQuery = new WP_Query( $filtered_query_args );
remove_filter( 'posts_where', 'filter_where' );  
share|improve this answer

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.