WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
Make use of the posts_clauses or posts_where filter. Dozens of examples on the page. – kaiser Jun 19 '12 at 1:46
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


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.