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

$args = array('numberposts' => 10, 'tag' => 'my-tag', 'ID' => 555'); $posts = get_posts($args); 

I want to list only 10 entries from a specific tag and that id is less than a number. Is there a way to do this using get_posts arguments? How can I specify Greater Than, Less Than or Not Like in an array of arguments?

Thanks...

+4
source share
3 answers

A good solution if you want to receive messages with an identifier below X:

 $post_ids = range(1, 555); $args = array('numberposts' => 10, 'tag' => 'my-tag', 'post__in' => $post_ids'); $posts = get_posts($args); 

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

+3
source

First you need to get the identifiers, and then add these 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
source

You will need to query them all, and inside the query loop, check if the identifier is more or less than your choice.

As you know, the request itself cannot process such requests.

0
source

Source: https://habr.com/ru/post/1415286/


All Articles