Compare post-IDs within WP_Query? (Less than / Greater than)

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.

Read More

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

Related posts

Leave a Reply

1 comment

  1. 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' );