1

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 );
Jerry Lee
  • 23
  • 1
  • 6

2 Answers2

1

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

'post__in' => array( )
Ash0ur
  • 1,505
  • 2
  • 8
  • 11
  • OP wrote `WHERE ID > $post_id` so I think that he doesn't want to retrieve only 1 post, but all post with that have an ID superior to a given number. – Mtxz Sep 04 '18 at 16:00
  • Didn't say It, Thanks @Mtxz – Ash0ur Sep 04 '18 at 16:06
1

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.

Mtxz
  • 3,749
  • 15
  • 29
  • Thank you very much, Mtxz & Ash0ur, I will check it out. – Jerry Lee Sep 04 '18 at 22:14
  • Ok! Please report here when your tests are done to confirm that it works for you. – Mtxz Sep 04 '18 at 23:07
  • set $post_ids in a range and use 'post__in' to filter, it's working!! I do the query for a category, its post_id is not consecutive, so i set the range of ($post_id-100, $post_id). Thanks a lot for the great help, Mtxz. – Jerry Lee Sep 05 '18 at 01:35
  • Cool! Mark the issue as resolved by accepting an answer. This way other users will quickly see how you managed to fix this. Thanks! – Mtxz Sep 05 '18 at 09:16