Exclude messages from get_posts ()

Good morning, I found many similar questions, but none of the answers matched my problem. The point is very simple: I have my own loop with get_posts (), and I want to exclude the display of the current message.

The code:

$args = array( 'posts_per_page' => 3, 'orderby' => 'meta_value', 'order' => 'ASC', 'post_type' => 'fasthomepress_pt', 'post__not_in' => array(get_the_id()), 'meta_query' => array( array( 'key' => 'custom_richiesta', 'value' => array($custom_boxes['custom_richiesta'][0] - 10000, $custom_boxes['custom_richiesta'][0] + 10000 ), 'type' => 'numeric', 'compare' => 'BETWEEN' ) ) ); 

I tried:

 'post__not_in' => array(get_the_ID), 'post__not_in' => array($post->ID), 'exclude' => $post->ID, 'exclude' => get_the_ID, 

and with many other combinations with or without an array. Damn, the current post-id is correctly reflected before this loop, and if I try to echo ($ post-> ID) and echo (get_the_ID ()), I have the same, correct result.

I really don’t know what is happening, thank you very much for your help,

Marco

+5
source share
2 answers

here is a function that does just that:

  function get_lastest_post_of_category($cat){ $args = array( 'posts_per_page' => 1, 'order'=> 'DESC', 'orderby' => 'date', 'category__in' => (array)$cat); $post_is = get_posts( $args ); return $post_is[0]->ID; } 

Usage: let's say my category id is 22:

 $last_post_ID = get_lastest_post_of_category(22); 

You can also pass an array of categories to this function.

  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; $args = array( 'posts_per_page' => 18, 'paged' => $paged, 'offset' => 0, 'post__not_in' => array($last_post_ID,), 'category' => '', 'category_name' => '', 'orderby' => 'post_date', 'order' => 'DESC', 'include' => '', 'exclude' => '', 'meta_key' => '', 'meta_value' => '', 'post_type' => 'post', 'post_mime_type' => '', 'post_parent' => '', 'post_status' => 'publish', 'suppress_filters' => true ); // The Query $the_query = new WP_Query( $args ); 
+6
source

try exculde this works for me.

 $args = array( 'posts_per_page' => 3, 'orderby' => 'meta_value', 'order' => 'ASC', 'post_type' => 'fasthomepress_pt', 'exclude' => array(get_the_id()), 'meta_query' => array( array( 'key' => 'custom_richiesta', 'value' => array($custom_boxes['custom_richiesta'][0] - 10000, $custom_boxes['custom_richiesta'][0] + 10000 ), 'type' => 'numeric', 'compare' => 'BETWEEN' ) ) ); 

Hope this works for you.

+13
source

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


All Articles