$args = array('numberposts' => 10, 'tag' => 'my-tag', 'ID' => 555');
$posts = get_posts($args);
I want to bring only 10 records from an specific tag and that the ID is less than a number. Is there any way to do this with the get_posts arguments? How can I specify Greater Than, Less Than or Not Like in the arguments array?
Thanks…
A nice solution if you want to get the posts with an ID lower than X:
props to girlieworks here: https://wordpress.org/support/topic/wp_query-how-to-get-posts-with-and-id-lower-than?replies=7#post-8203891
You need to get the IDs first, and then add those IDs to wp_query.
You could use the
posts_where
filter to alter the SQL query to restrict the results to posts with ID lower (or greater) than a certain number:You would have to query all of them, and inside the query-loop check if the id is greater or less than the number of your choice.
As for as I know the query itself can’t handle such requests.
This is the fastest, most efficient and most reliable way (at least compared to the craziness of passing thousands of integers into SQL):
I like Web-Entwickler’s idea, but using in the opposite direction (as it is in the question). It’s even futureproof if you set up the latest known ID and using the not_in method. So my solution is if you want 555+:
And if you want 1-555: