Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude posts from get_posts()

Tags:

php

wordpress

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

like image 892
Marco Avatar asked Feb 20 '15 10:02

Marco


People also ask

How do I exclude a post in WordPress?

To Exclude a Page From WordPress Search Results Excluding a page is done the same way you exclude a post. Go to your list of pages and click the “Edit” link for the post you would like to exclude. Scroll down to the bottom right of the post. Check the box for “Exclude from Search Results.”

What is Post __ Not_in?

Avoid post__not_inIt's usually used to exclude certain post IDs from a query's results.

How do I hide the first post on WordPress?

add_action( 'pre_get_posts' , 'wpsites_exclude_latest_post' , 1 ); This code will exclude the latest post from displaying on your home page loop. The offset is set to one, so only the first post will be hidden.

How do I find category posts in WordPress?

Now, if you want to display all your posts from a specific category on a separate page, WordPress already takes care of this for you. To find the category page, you simply need to go to Posts » Categories » View page and click on the 'View' link below a category.


2 Answers

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'
                                  )
                          )
  );
like image 170
Renish Khunt Avatar answered Nov 04 '22 08:11

Renish Khunt


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 );
like image 24
Said Erraoudy Avatar answered Nov 04 '22 06:11

Said Erraoudy