2

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.

2
  • when you registered the post type did you set public to true and or exclude_from_search to false?
    – rudtek
    Commented Nov 5, 2018 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. Commented Nov 6, 2018 at 0:19

6 Answers 6

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

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.

0

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

<?php
function add_cpt_to_search( $wp_query ) {
    if ( ! is_admin() && is_main_query() && is_search() ) {
        $wp_query->set( 'post_type', [ '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

You can simply modify and use this

function wb_search_filter_archived( $query ) {
    if ( ! is_admin() && $query->is_main_query() ) {
        // Not a query for an admin page.
        // It's the main query for a front end page of your site.
            $query->set( 'post_status', array( 'publish' ) );
        }
    }
add_action( 'pre_get_posts', 'wb_search_filter_archived');
1
  • This snippet does not solve the problem in the question, it just sets the query to search for published posts (which is the default behaviour anyway). "Simply modify" means nothing to someone who's having to ask the question in the first place.
    – Chris Cox
    Commented Jun 1 at 12:19
0
function include_custom_post_types_in_search($query) {
    if ($query->is_search() && $query->is_main_query()) {
        $custom_post_types = array('career');

        $query->set('post_type', array_merge(array('post', 'page'), $custom_post_types));
    }
}
add_action('pre_get_posts', 'include_custom_post_types_in_search');
1
  • This answer doesn't solve OP's question. Their custom post type is called career, not product or portfolio.
    – Chris Cox
    Commented Jun 1 at 13:48
-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 and acknowledge you have read our privacy policy.

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