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 except 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;
}

Add this to your site 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.

2 comments How to include specific posts

  1. Hi there!

    I would like to know if there is a way to give relevance to certain custom post. I mean, I have multiple post type on my website, however, I have one custom post named “programs” that it is the most importat, so what I want to do is, when a user search, por example, Executive Education, the first results should the custom post named “programs” and then the rest, por example the blog entries. Actually the searcher find all the results in all the post, so, if I have the word “Executive Education” wrote in a entry, the searcher gonna show me that entry, what is fine, but what I would like to see in first place is “Executive Education” as a program that it is the main tittle of that post and not inside of the post content.

    This is possible?

    1. This is easiest to do with Relevanssi Premium. Relevanssi Premium has a pinning feature, so you could go to the “Programs” post and pin it for the “Executive Education” search. Now when someone searches for “Executive Education”, that post comes up first.

      Relevanssi Premium also has an option to adjust post type weights from the settings so that you can increase the weight of all the posts in the “Programs” post type.

      If you don’t want to buy Premium, you can achieve all this with post type sorting and filtering, but it’s easiest with Premium.

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.