I just want to grab all posts that have a greater value than a supplied post ID
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'rid',
'value' => $rid,
'compare' => '=',
'type' => 'numeric',
),
array(
'key' => ID,
'value' => $last_id,
'compare' => '>',
'type' => 'numeric'
),
),
So if $last_id is "350", I want to grab all posts with a higher ID than 350
I just want to grab all posts that have a greater value than a supplied post ID
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'rid',
'value' => $rid,
'compare' => '=',
'type' => 'numeric',
),
array(
'key' => ID,
'value' => $last_id,
'compare' => '>',
'type' => 'numeric'
),
),
So if $last_id is "350", I want to grab all posts with a higher ID than 350
Share Improve this question asked Sep 23, 2019 at 9:27 LeopoldLeopold 76 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 1You can use prepare:
global $wpdb;
$post_ids = [];
$last_id = 350;
$query = $wpdb->prepare(
"
SELECT ID
FROM $wpdb->posts
WHERE ID > %d
", $last_id );
$results = $wpdb->get_results( $query );
// it will convert the IDs from row objects to an array of IDs
foreach ( $results as $row ) {
array_push( $post_ids, $row->ID );
}
//now you can set up your query
$custom_args = array(
'posts_per_page' => 100,
'post__in' => $post_ids
);
$custom_query = new WP_Query( $custom_args );
if( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post();
echo get_the_title() . '<br>';
endwhile;
endif;
Reference
Note: You can change condition as per your requirement >, >=, <, <=
etc.
ID
? And is that'key' => ID
(i.e. unquoted "ID" / meta key) just a typo in the question? – Sally CJ Commented Sep 23, 2019 at 10:12