Title: Exclude Posts programmatically
Last modified: December 22, 2021

---

# Exclude Posts programmatically

 *  [tayssir.ch](https://wordpress.org/support/users/tayssirch/)
 * (@tayssirch)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/exclude-posts-programmatically/)
 * Hello,
    I need to exclude posts which has a custom field value 1,
 *     ```
       function exclude_discontinued_search($query) {
         //only run for the main query and don't run on admin pages
         if (!is_admin() && $query->is_main_query()) {
           //now check to see if you are on a search results page
           if ($query->is_search) {
             //get sponsor posts that are NOT advertorials, so we can exclude their IDs from search
             $args = array(
               //get posts of the custom post type sponsor_post
               'post_type' => 'product',
               //get all posts
               'posts_per_page' => -1,
               //return an array of post IDs
               'fields' => 'ids',
               //now check for posts that have a sponsor_post_type that is not 'advertorial'
               'meta_query' => array(
                 'relation' => 'OR',
                 array(
                   'key' => 'discontinued',
                   'value' => '1',
                   'compare' => '!='
                 ),
               )
             );
             //now get the posts
             $excluded_ids = get_posts($args);
             //add these post IDs to the 'post__not_in' query parameter
             $query->set('post__not_in', $excluded_ids);
           }
         }
       }
       add_action('pre_get_posts', 'exclude_discontinued_search');
       ```
   
 * But its not working with me,
    But When i export the var_dump($excluded_ids) i
   can see the IDS correctly.`
 * am I doing anything wrong not compatible with Relevanssi ?
 * I noticed in there is an option called relevanssi_exclude_posts.
    there is any
   hook we can use it to add the list of ID into this ?
 * Thank you

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Plugin Author [Mikko Saari](https://wordpress.org/support/users/msaari/)
 * (@msaari)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/exclude-posts-programmatically/#post-15188013)
 * Relevanssi should understand `post__not_in`. Your logic seems to be backwards,
   though: `$excluded_ids` has the IDs of the posts where `discontinued` is not `
   1`. Shouldn’t this be the other way around? Ie. change the `compare` to `=` or`
   post__not_in` to `post__in`.
 * However, if you never want to see those posts in the search results, I would 
   recommend using a different approach. This requires extra queries and just removes
   the posts from the results; with Relevanssi, you can also make sure the posts
   aren’t in the index in the first place, which is more effective.
 * This is done with `relevanssi_indexing_restriction` filter hook. I added your
   case as an example to [the documentation](https://www.relevanssi.com/user-manual/filter-hooks/relevanssi_indexing_restriction/),
   since there wasn’t an example of custom field blocking.
 *  Thread Starter [tayssir.ch](https://wordpress.org/support/users/tayssirch/)
 * (@tayssirch)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/exclude-posts-programmatically/#post-15188075)
 * Thanks Mikko for the quick response!,
 * After I added the hook “Exclude products where the discontinued custom field 
   is set to 1:” in my function.php
    and I rebuild Index, I got this :
 *     ```
       Indexed 0 posts (total 0), processed 0 / 0.
       Indexing complete. 0 posts excluded.
       ```
   
 * , and nothing appear in my search result anymore
    when i remove the function,
   everything work fine
 *  Plugin Author [Mikko Saari](https://wordpress.org/support/users/msaari/)
 * (@msaari)
 * [4 years, 3 months ago](https://wordpress.org/support/topic/exclude-posts-programmatically/#post-15188179)
 * There’s at least one typo: $wpdb->postmeta is misspelled.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Exclude Posts programmatically’ is closed to new replies.

 * ![](https://ps.w.org/relevanssi/assets/icon-256x256.png?rev=2025044)
 * [Relevanssi - A Better Search](https://wordpress.org/plugins/relevanssi/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/relevanssi/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/relevanssi/)
 * [Active Topics](https://wordpress.org/support/plugin/relevanssi/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/relevanssi/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/relevanssi/reviews/)

 * 3 replies
 * 2 participants
 * Last reply from: [Mikko Saari](https://wordpress.org/support/users/msaari/)
 * Last activity: [4 years, 3 months ago](https://wordpress.org/support/topic/exclude-posts-programmatically/#post-15188179)
 * Status: not resolved