14

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:

$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

3
  • Try removing your meta_query now for testing and check if it exculdes the current post. The syntax is correct, it may be the problem with the meta query I think so.
    – user46487
    Commented Feb 20, 2015 at 10:42
  • 1
    I solved it now, it was my mistake!! I tried this get_posts() loop inside another loop, and I didn't placed it well. I moved this loop outside the main loop and everything worked well, thank for your support!
    – Marco
    Commented Feb 20, 2015 at 10:52
  • Hi @Marco, If my answer is helpful for you and other users kindly please accept my answer. I think it's more useful than others. Commented Oct 1, 2021 at 4:49

2 Answers 2

26

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
  • 1
    Thank you for your answer. Unfortunately, it doesn't work, I tried with exclude ---> array() and exclude ----> values, the result is always the same.
    – Marco
    Commented Feb 20, 2015 at 10:43
  • 1
    SOLVED! It was my mistake. I tried this get_posts() loop inside another loop, and I didn't placed it well. I moved this loop outside the main loop and everything worked well, thank for your support!
    – Marco
    Commented Feb 20, 2015 at 10:51
9

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.