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

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


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


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 IDsforeach ( $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 loopif( $custom_query->have_posts() ) :    while( $custom_query->have_posts() ) : $custom_query->the_post();        echo get_the_title() . '<br>';    endwhile;endif;


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.