How to get Posts Greater Than X (ID) using get_posts
Asked Answered
B

6

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

Boyce answered 31/5, 2012 at 4:59 Comment(0)
K
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

Kirit answered 28/3, 2016 at 8:7 Comment(1)
This is a terrible idea. Don't do this! This will create an array with 555 elements which will then be concatenated, escaped and formatted into one extremely long SQL query (SELECT * FROM wp_posts WHERE ID IN(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 ..., 554, 555);), absolutely tanking the performance!Abjuration
R
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;
Reducer answered 26/8, 2015 at 16:49 Comment(0)
T
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 );
Tapes answered 27/11, 2018 at 23:25 Comment(0)
H
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.

Hardecanute answered 31/5, 2012 at 14:10 Comment(0)
A
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);
Abjuration answered 23/8, 2022 at 9:11 Comment(0)
D
-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);
Disposable answered 3/7, 2021 at 8:4 Comment(1)
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);...Abjuration

© 2022 - 2024 — McMap. All rights reserved.