![]() |
I want to create a search form that will only search a custom post type. And I want to control the template page that serves up the results. I've Googled and Googled and cannot get anything to work.
My site is built using the Underscores starter theme from Automattic.
Here's what I'm trying currently:
In the functions.php file:
And my modified search form: Solution - 1is your custom post type "publicly_queryable" ? It should be. Did you check what this line is returning? Is it array or single value? Will you be able to provide access of your website? or custom post type code, functions.php code and search template code? Solution - 2Is it the only search present on the site or there is another default search too? If it is the only search then you can edit search.php file. Solution - 3You can check out this page for building a custom search page to search for specific post types: https://www.smashingmagazine.com/2016/03/advanced-wordpress-search-with-wp_query/ Solution - 4Hi Kyler, Could you add this into functions.php
Solution - 5Hi Kyler, there is nothing wrong with the code above. i have tried it and it working perfectly so, i assume the problem is not this part. Solution - 6Hi, Bellow step : 1- change search form (custom post 'project') Search project2- add code into function.php function template_chooser($template) { global $wp_query; $post_type = get_query_var('post_type'); if( $wp_query->is_search && $post_type == 'project' ) { return locate_template('archive-search.php'); // redirect to archive-search.php } return $template; } add_filter('template_include', 'template_chooser'); 3- Create custom search result template into theme archive-search.php Its working for me, if any further help, Please contact to me. Thanks, KrishnaSolution - 7You can do it in your SEARCH.PHP file with the following code This code I assume that you also have a template for any other search (default) |
Wordpress |
Type: | Code Example |
Category: | Coding |
Sub Category: | Code Example |
Uploaded by: | Admin |
Views: | 23 |