• Resolved gbbgadmin

    (@gbbgadmin)


    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

    (@msaari)

    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

    (@gbbgadmin)

    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.