I have a search field for the blog posts, but I need an other for a custom post type. How can I create this custom search form with a different search result layout?
Leave a Reply
You must be logged in to post a comment.
I have a search field for the blog posts, but I need an other for a custom post type. How can I create this custom search form with a different search result layout?
You must be logged in to post a comment.
Here is what I’ve tried and got a solution with 3 steps. Let’s say your custom post type is “products“
1 . Add Function Code here you can specify the archive-search.php
2 . Create search result template for custom post type ( archive-search.php )
Build Search Form
In this Search Form, the value “products” is hidden and it will search only product posts.
for more, I would like to link you to here
http://www.wpbeginner.com/wp-tutorials/how-to-create-advanced-search-form-in-wordpress-for-custom-post-types/
Here is what works for me. Not as clean but I couldn’t get any of these other answers to work.
Search form for Custom Post Type:
In functions.php:
In search.php:
Naturally in all three places you’ll need to replace “book” with your custom post type.
Hope this helps someone!
A short code more actualized
I was looking to use two different forms for my normal searches and my searches on a custom post type.
My custom post type uses a different header than normal pages, on my normal page, the call to my search form is:
And the call to my search form in the custom post type header is:
Which has an additional field:
In the functions file I have the following code that you have provided.
Which detects if the search form is doing a search within custom fields, thus showing the search in a custom template, otherwise use the normal template.
Edit: fixed the get_search_form() function call which would have returned true no matter what.
To fix the empty input search issue you can substitute the function code with this:
On a WooCommerce (product post type) search case,
just copy the woocommerce/templates/archive-product.php file to your child theme and then customize it… 1hour for this!! 🙁
I have 10 CPTs each with it’s own search result page (different layouts each) and using this was not working for me.
After more digging, I found this this approach where you condition the search.php template to load another template if there is a CPT in the URL.
basically in search.php you insert this:
and the search form would be something like this:
I know this is an old thread, but I use custom shortcodes in my functions.php file to enable searches tied to specific post types and in my case custom taxonomies. I use Pods for a video lecture archive by year, and wanted searches to be within that year only. This is currently working (in my functions.php):
I then have a page with the highlight videos from 2020, with the search form shortcode above it so users can search the rest of 2020.
name="video_year"
is the custom taxonomy created by Pods, the post type is also created by Pods and is called “videos”, andvalue="video-archive-2020"
is the slug from the Pods video year I need. If you had a custom post type of “books”, a taxonomy of “book-genre” and you are trying to restrict the search to “adventure”, “book-genre” would be the ‘name’ entry and “adventure” would be the ‘value’. Hope this helps someone, and thank you to all the above answers!