0

My website has a custom post type. I'd like to allow users the ability to search within the custom post type. So I'd like the search results to only include posts of that particular post type.

I'd like to also offer normal search. That is, I'd like the site to have forms that have the standard wordpress search, allowing users the ability to search within the native post types (Post and Page).

Because I want to offer a normal search alongside a customised search, I can't filter the search query with the 'pre_get_posts' hook. So the best option is to custom the search form.

for the post type mypost with the slug myposts, I have this form:

<form role="search" method="get" 
      id="searchform block"
          class="search-form"
          action="<?php echo esc_url(home_url('/')); ?>"
          >
    <div class="relative">
        <label class="screen-reader-text" for="s"><?php esc_html_e('Search for:', 'text-domain'); ?></label>
        <input          id="search-field"
          type="search"
          class="search-field outline-none"
          value="<?php echo esc_html(get_search_query()); ?>" name="s" />
        <input type="hidden" name="post_type" value="myposts" />

        <button class="search-button cursor-pointer absolute right-0 top-0 bottom-0 m-auto py-5 px-4 ml-4" type="submit" id="searchsubmit">
            <span class="sr-only"><?php esc_html_e('Search', 'text-domain'); ?></span>

        </button>

    </div> </form>

In the input parameter value, I've tried both mypost and myposts.

For either value, a search will return zero results.

Instances of the custom post type do exist.

So: how can I create a working form that will search only for results in the target custom post type?

2
  • Have you checked whether exclude_from_search is set to false for your custom post type? This might be helpful: wordpress.stackexchange.com/questions/29447/…
    – atiquratik
    Commented Sep 13, 2022 at 8:11
  • @atiquratik: yes, 'exclude_from_search' is set to false.
    – bob
    Commented Sep 14, 2022 at 23:32

1 Answer 1

0

First, ensure that your custom post type is public and searchable

Second, you can't use post_type url query var, it will be strip, its reserve for custom post type archive.

you can however simply do a custom post type search with URL like this.

domain.com/custom-post-type-rewrite-slug/?s=My+Search+Term

so in your form, the action should be

<?php echo esc_url(home_url('/custom-post-type-rewrite-slug/')); ?>

add remove this hidden input for post_type

<input type="hidden" name="post_type" value="myposts" />

Additionally, you can also set your own query var then modify the query via pre_get_posts action hook so you can perform search on tld with one or more post_type support.

Simply add query var first

add_filter( 'query_vars',  function( $vars ) {
    $vars[] = 'ptype';
    return $vars;
});

Now, modify the query if ptype is present in the URL

add_action('pre_get_posts', function( $query) {

    if ( !is_admin() && isset( $query->query['s'] ) && isset( $query->query['ptype'] ) ) {
        $query->set('post_type', $query->query['ptype']);
    }
    return $query;
    
}); 

Once added, you can do a search like bellow;

for searching on single post type

domain.com/?ptype=whatever&s=search+term

for searching on multiple post type

domain.com/?ptype[]=whatever&ptype[]=post&s=search+term

so if you want to support multiple post type on your search form, you can simply add multiple hidden input

<input type="hidden" name="ptype[]" value="whatever" />
<input type="hidden" name="ptype[]" value="post" />

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.