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 with relevanssi_indexing_restriction. This is a great solution, if you have a fairly small and stable number of posts you want to include.

add_filter( 'relevanssi_indexing_restriction', 'rlv_index_only_some_posts' );
function rlv_index_only_some_posts( $restriction ) {
  $posts_to_include       = implode( ',', array( 1, 2, 3 ) );
  $restriction['mysql']  .= " AND ( ( post.post_type = 'post' AND post.ID IN ( $posts_to_include ) ) OR post.post_type != 'post' ) ";
  $restriction['reason'] .= ' Unwanted post';
  return $restriction;
}

Put this function in functions.php and list the posts you want to include in the $posts_to_include array (instead of 1, 2, 3). If you want this to target a post type other than post, adjust that in the post.post_type fifelds. 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

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.