Title: Exclude custom postmeta
Last modified: February 22, 2017

---

# Exclude custom postmeta

 *  Resolved [gbbgadmin](https://wordpress.org/support/users/gbbgadmin/)
 * (@gbbgadmin)
 * [9 years, 1 month ago](https://wordpress.org/support/topic/exclude-custom-postmeta/)
 * Hello. I have a site that has articles rated from G, PG, R, X, etc. These carry
   a numeric wp_postmeta value of 1, 2, 3, or 4. How can I exclude posts whose “
   post_rating” > 2 from the search results?
 * Do I add something like array(‘meta_key’ => ‘post_rating’,’meta_value_num’ =>
   3,’meta_compare’ => ‘<‘) to the query as a function? Or what would you suggest
   is the best way to filter out posts with a meta_key of post_rating and a meta_value
   greater than 2?

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

 *  Plugin Author [Mikko Saari](https://wordpress.org/support/users/msaari/)
 * (@msaari)
 * [9 years, 1 month ago](https://wordpress.org/support/topic/exclude-custom-postmeta/#post-8838095)
 * Yes, that’s a good way, or formulate a meta_query. A good place to add that is`
   pre_get_posts` filter hook.
 * However, if you never want to see those posts in search results, it’s even better
   to exclude them from the index. Add a function on `relevanssi_do_not_index` filter
   hook, like this:
 *     ```
       add_filter('relevanssi_do_not_index', 'rlv_no_r_x', 10, 2);
       function rlv_no_r_x($skip, $post_id) {
           $rating = get_post_meta($post_id, 'post_rating', true);
           if ($rating > 2) $skip = true;
           return $skip;
       }
       ```
   
 * Add this to theme functions.php and rebuild the index. Then the R and X rated
   posts won’t appear in search results.
 *  Thread Starter [gbbgadmin](https://wordpress.org/support/users/gbbgadmin/)
 * (@gbbgadmin)
 * [9 years, 1 month ago](https://wordpress.org/support/topic/exclude-custom-postmeta/#post-8838633)
 * Wow. I don’t know how I missed your relevanssi_do_not_index filter when I was
   reading the documentation. Thank you for pointing this out. This is MUCH cleaner
   and global. It works perfectly! Thank you.

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

The topic ‘Exclude custom postmeta’ 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/)

 * 2 replies
 * 2 participants
 * Last reply from: [gbbgadmin](https://wordpress.org/support/users/gbbgadmin/)
 * Last activity: [9 years, 1 month ago](https://wordpress.org/support/topic/exclude-custom-postmeta/#post-8838633)
 * Status: resolved