- This topic has 9 replies, 2 voices, and was last updated 3 years, 8 months ago by Tom.
-
AuthorPosts
-
July 15, 2020 at 8:42 am #1364541Leslie
Hi GP team. I have a question about search results pages. I am using Relevanssi for search, with this shortcode to limit searches to a custom post type: [searchform post_types=”my_cpt”]. I have two of these on my site, let’s call them fruits and vegetables. I have used GP’s “after header” hook to display the fruits
searchform on the fruits pages (fruits archive, fruits’ custom taxonomy archives, single fruits pages etc.) and I’ve done the corresponding thing for the vegetables pages. What I’m trying to add now are the correct searchforms on the search results pages. So, if a user searches on a fruits page, they see fruit results, with the searchform that allows them to continue searching within fruits. Coding-wise, I think the instruction is something like: “On any given search results page, display the same searchform (fruits, vegetables or non-custom) that was used for the search.” I can easily accomplish this for a single searchform simply by adding “search results” to the display locations for the “after header” hook. But that affects the whole site. So, to customize the search results pages, I copied search.php into search-fruits.php (without any other edits yet), but I’ve stalled because the search-fruits.php template isn’t being displayed and I can’t figure out why. Once I solve that, I think I need to customize it with get_search_form to display the fruits searchform. And then create and customize search-vegetables.php. Am I on the right track here? I sense that I might be making this harder than it needs to be. Instead of creating search-fruits.php, should I write a site-specific plugin to conditionally modify the output of search.php, based on which searchform the search came from? I appreciate any help you can give me. Thanks very much. LeslieJuly 15, 2020 at 2:14 pm #1364899TomLead DeveloperLead DeveloperHi there,
Somewhat complex, but should be do-able.
When you use one of these search forms, do you see anything in the URL after searching that looks like this?:
?post-type=fruits
July 15, 2020 at 3:43 pm #1364968LeslieHi Tom, yes that’s the good news. When I use the non-custom search form, for example on the home page, in the search results url I see:
?s=Whatever+Search+Terms+I+Typed
When searching from pages with [searchform post_types=”fruits”], I see this in the results page url:
?s=Whatever+Search+Terms+I+Typed&post_types=fruits
And the same thing works fine for post_types=vegetables.
I am using this to make sure I can see which template is being loaded:
add_action( ‘admin_bar_menu’, ‘show_template’ );
function show_template() {
global $template;
print_r( $template );
}In every case the template is search.php.
FYI, I might be engaging in a bad practice. I have put my custom search results template here: wp-content/themes/generatepress/search-fruits.php. I don’t have a child theme. Is this a very bad idea? My site successfully loads a couple of other custom templates that I’ve made, but if it’s important to use a child theme, please let me know. I am not a developer — teaching myself as I go. Thank you Tom.
July 16, 2020 at 9:24 am #1365879TomLead DeveloperLead DeveloperHi there,
Definitely a bad idea to add anything to the core theme – it will be deleted when you update. Always use a child theme for that kind of thing.
As for the form, I don’t think you need a custom template.
In your Hook, try this:
<?php $type = ''; if ( isset( $_GET['post_type'] ) ) { $type = esc_attr( $_GET['post_type'] ); } echo do_shortcode( '[searchform post_types="' . esc_attr( $type ) . '"]' ); ?>
Now it should pick up on the current post type being displayed. Just make sure to “Execute PHP” and “Execute Shortcodes” in the Hook.
July 17, 2020 at 11:45 am #1367086LeslieThank you Tom, I will create a child theme immediately. Your solution works beautifully. But do you think I can make it iterative? From the search results page, the user might want to input a second (and third, etc.) search. When I tested this, I got “no results,” with this in the url:
?s=My+Search+Input&post_types=<%3Fphp+echo+esc_attr%28+%24type+%29%3B+%3F>
Thank you again Tom.
July 17, 2020 at 2:48 pm #1367208TomLead DeveloperLead DeveloperStrange, can you try the updated code?: https://generatepress.com/forums/topic/custom-searchform-on-search-results-pages/#post-1365879
July 18, 2020 at 11:31 am #1368002LeslieMuch appreciated Tom, this is working – awesome. Now I have to look at my taxonomy term archives. A user can arrive at those via a re-directed search that I’ve set up (when the user’s search phrase EXACTLY matches the taxonomy term name, I am re-directing them to the term archive instead of showing search results). On those redirects, I want to display one of my regular searchforms, not the “search results searchform” that you just helped me set up. I don’t know if this is possible but I will work on it myself, and hopefully I can ask for your help again if I can’t solve it. However, if a solution comes to your mind, please let me know. Thanks again. Leslie
July 18, 2020 at 3:06 pm #1368119TomLead DeveloperLead DeveloperPerhaps that would just need a separate hook with the regular shortcode targeting those archives?
July 18, 2020 at 4:43 pm #1368157LeslieI am trying that right now. PS I created a child theme! And I put my Simple CSS into the child style.css file. Onwards…
July 19, 2020 at 9:41 am #1368830TomLead DeveloperLead DeveloperAwesome 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.