Posted on

How to include specific posts

Hello, I’m using WooCommerce and Relevanssi and they work great.

My Site Search only searches Products, however I want to let certain posts appear in the search results.

Is there a way to allow certain posts to be in the results?

Yes. There are many ways to do this, and the best way depends on how many posts you want to include and how often you need to change the posts included.

I’d do this by enabling posts in the site search, but then disabling indexing for all posts excepts those you want to include. This is a great solution, if you have a fairly small and stable number of posts you want to include.

add_filter('relevanssi_do_not_index', 'post_indexing_filter', 10, 2);
function post_indexing_filter($index, $post_id) {
    $posts_to_include = array(1, 2, 3);
    if (in_array($post_id, $posts_to_include)) return true;
    return false;
}

Put this function in functions.php and list the posts you want to include in the $posts_to_include array. Then rebuild the index, and only the posts you want to include will be indexed.

This question was originally asked on the WP support forums.

Leave a Reply

Your email address will not be published. Required fields are marked *