6
$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...

Mike
  • 173
  • 2
  • 5

6 Answers6

5

A nice solution if you want to get the posts with an ID lower than X:

$post_ids = range(1, 555); 

$args = array('numberposts' => 10, 
'tag' => 'my-tag', 
'post__in' => $post_ids');

$posts = get_posts($args);

props to girlieworks here: https://wordpress.org/support/topic/wp_query-how-to-get-posts-with-and-id-lower-than?replies=7#post-8203891

2

You need to get the IDs first, and then add those IDs to wp_query.

global $wpdb;

$post_ids = [];

// Get all the IDs you want to choose from
$sql = $wpdb->prepare(
    "
        SELECT ID
        FROM $wpdb->posts
        WHERE ID > %d
    ", 555 );

$results = $wpdb->get_results( $sql );

// Convert the IDs from row objects to an array of IDs
foreach ( $results as $row ) {
    array_push( $post_ids, $row->ID );
}

// Set up your query
$custom_args = array(
    'posts_per_page' => 10,
    'tag' => 'my-tag',
    'post__in' => $post_ids
    );

// Do the query
$custom_query = new WP_Query( $custom_args );

// the loop
if( $custom_query->have_posts() ) :
    while( $custom_query->have_posts() ) : $custom_query->the_post();
        echo get_the_title() . '<br>';
    endwhile;
endif;
stillatmylinux
  • 1,399
  • 13
  • 25
2

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:

$args   = [
    'tag'              => 'my-tag',
    'posts_per_page'   => 10,
    // Required: posts_where is not triggered without setting suppress_filters to false.
    'suppress_filters' => false,
];
$max_id = 155;

$filter_handler = function( $where ) use ( $max_id ) {
    global $wpdb;

    return $where . $wpdb->prepare( " AND {$wpdb->posts}.ID < %d", $max_id );
};

add_filter( 'posts_where', $filter_handler );

$posts = get_posts( $args );

remove_filter( 'posts_where', $filter_handler );
Willington Vega
  • 4,732
  • 3
  • 21
  • 19
0

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.

ninja
  • 2,233
  • 1
  • 15
  • 15
0

This is the fastest, most efficient and most reliable way (at least compared to the craziness of passing thousands of integers into SQL):

$compId = 555;
$filterWhere = function(string $where, \WP_Query $query) use (&$compId): string {
    $where .= " AND `ID` > $compId";
    return $where;
};

add_filter('posts_where', $filterWhere, 10, 2);
$posts = get_posts([
    'posts_per_page' => 20,
    'suppress_filters' => false,
]);
remove_filter('posts_where', $filterWhere);
ericek111
  • 575
  • 7
  • 15
-1

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+:

$args['post__not_in'] = range(1, 555);

And if you want 1-555:

$args['post__in'] = range(1, 555);
err
  • 109
  • 2
  • 7
  • This is a terrible idea and should not be used in production! This will create an array with 555 elements which will then be concatenated, escaped and formatted into one extremely long SQL query! Something like: `SELECT * FROM wp_posts WHERE ID IN(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13);`... – ericek111 Aug 23 '22 at 08:52