Exclude posts from get_posts()

Good morning, I found many similar questions, but none of the answer fit to my problem. The point is very simple: I have a custom loop with get_posts(), and I want to exclude current post from being displayed.

The code is:

Read More
$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 with:

'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 array. Of curse, current post id is correctly echoed before this loop, and if I try echo($post->ID) and echo(get_the_ID()) I have the same, correct, result.

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

Marco

Related posts

Leave a Reply

2 comments

  1. Try exclude.

    $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'
                                      )
                              )
      );
    
  2. 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: say my category id is 22 then:

    $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 );