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

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

Related posts

Leave a Reply

6 comments

  1. 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;
    
  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 );
    
  3. 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.

  4. 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);
    
  5. 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);