1

How do I get WP search results to include custom post type (CPT) archives? For example, if I have CPT 'career' whose title (in the template php) is 'Careers', how do I get search results to include the CPT archive URL (www.my-domain.com/careers) for "careers", "career", "jobs".

I suppose the question might be, can one insert their own results into search, and how?

I am/can optionally use the Relevanssi search plugin as well. I installed that to try and solve this problem, but it seems that alone is not enough.

Thank you for your attention.

  • when you registered the post type did you set public to true and or exclude_from_search to false? – rudtek Nov 5 '18 at 22:49
  • 1
    You can't. WordPress search only searches posts (including pages and custom post types), and it only searches the title and content of those posts. Nothing from the template. So it doesn't return archives of any kind. Whether a plugin allows this is something you'd need to ask its developers. Asking about plugins, or for plugin recommendations, is off-topic here. If you wanted to do it yourself, it would involve developing a custom search engine and indexer, which is too broad a topic for a single question. – Jacob Peattie Nov 6 '18 at 0:19
0

WordPress will not return a post type archive page in search results. Relevanssi might work, but a slightly easier route is to create a custom page template that lists the career items. Name the page "Careers" and it will show up in search results.

Another advantage to this approach is that the page's text content can be displayed above the listing of career items, meaning you can manage that in WordPress rather than hard-coding that text in the template.

There are disadvantages as well - paginating the career items is significantly more complex, so you may be tempted to disable pagination entirely and display them all on a single page. If you have dozens or hundreds of items, that is not a good idea.

0

You can modify the query with pre_get_posts, here's an example:

function add_cpt_to_search($wp_query){
  if(!is_admin() && is_main_query() && is_search()){
    $wp_query->set('post_type'=>array('post','careers','page'));
  }
}
add_action('pre_get_posts','add_cpt_to_search');

In the if-statement above there are 3 conditions:

  • !is_admin(): Ensures this rule only happens on the frontend of wordpress, not in the wp-admin area
  • is_main_query(): Makes sure that the rule is applied only to the search-feed loop, rather than say, the loop which outputs the nav menu items.
  • is_search(): Focuses the rule to only when search results page is being displayed. Feel free to tweak this to suit your needs. If you're not displaying the search-results on the search-page you might want to check against $wp_query->get('s')
0

Just create a blank page named 'Careers'. This will appear in the search results. By clicking on 'www.my-domain.com/careers' wordpress redirects you to the CPT archive.

-1

You don't need Relevanssi for this.

You can just add this to the loop in your the archive.php:

$post_type = get_post_type_object( get_post_type( $post ) );
if( $post_type ) {
  echo '<p class="search-result-post-type">' . esc_html( $post_type->labels->singular_name ) . '</p>';
}

I'm pretty sure that I got it from another post in here and used it myself. But I can't find the other post, - so I can't pass on the credit. :-/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.