[Resolved] custom searchform on search results pages

Home Forums Support [Resolved] custom searchform on search results pages

Home Forums Support custom searchform on search results pages

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1364541
    Leslie

    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. Leslie

    #1364899
    Tom
    Lead Developer
    Lead Developer

    Hi 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

    #1364968
    Leslie

    Hi 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.

    #1365879
    Tom
    Lead Developer
    Lead Developer

    Hi 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.

    #1367086
    Leslie

    Thank 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.

    #1367208
    Tom
    Lead Developer
    Lead Developer
    #1368002
    Leslie

    Much 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

    #1368119
    Tom
    Lead Developer
    Lead Developer

    Perhaps that would just need a separate hook with the regular shortcode targeting those archives?

    #1368157
    Leslie

    I am trying that right now. PS I created a child theme! And I put my Simple CSS into the child style.css file. Onwards…

    #1368830
    Tom
    Lead Developer
    Lead Developer

    Awesome 🙂

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.