Jerry Lee
Jerry Lee

Reputation: 33

WP_Query arguments setting for ID

I would like to do the WP_Query with the arguments setting as below, plus "WHERE ID > $post_id" (specific post id). Pls advise how to add this WHERE clause into the $arg setting? Thanks a lot.

$args = array (
            'cat'              => $category_id,
            'nopaging'               => false,
            'posts_per_page'         => '5',
            'order'                  => 'DESC',
            'orderby'                => 'ID'
            );

$query = new WP_Query( $args );

Upvotes: 2

Views: 2607

Answers (2)

Mtxz
Mtxz

Reputation: 3869

If you want to query "all post with an ID superior to X", I think you need to do like this :

$post_ids = range(25, 50); //we need an array of post IDs

$args = [
'posts_per_page' => 25, // if you want to limit/paginate the query
'post__in' => $post_ids
];

$posts = get_posts($args);

This will query all post from IDs 25 to 50 included. Pagination may be useful if you have a very large range of IDs to query. source.

Note that this will not generate a WHERE SQL clause, but a WHERE IN. To get a real MySQL WHERE, I think you'll to go with custom SQL query, as WP_Query doesn't provide a lot of operators on the post IDs.

Upvotes: 2

Ash0ur
Ash0ur

Reputation: 1545

oops, didn't see the > sign you can use post__in with posts ids as @Mtxz answered above

'post__in' => array( )

Upvotes: 1

Related Questions