Trigger search submit

Forums › Forums › Search & Filter Pro › Trigger search submit

Viewing 5 posts – 1 through 5 (of 5 total)
  • Anonymous
    #213745

    Hi,

    Is there a way to trigger the search form to submit aside from clicking the actual submit button?

    I currently have functionality on my search results page, that if it finds matches for a particular post type, it’ll give the user a suggestion like, “We’ve found events related to your search term – click here to view results.”

    I want the search form to re-submit/update the query – which is really the equivalent of manually using the post type filter dropdown and setting it to that post type if they used the submit button. But I want to trigger the same thing from a different button outside the form. Is that possible? Or do I have to hook into that form and add my custom button/text suggestion?

    Thanks!

    Trevor
    #213805

    Do you need to change a value in the form first?

    Anonymous
    #213834

    Yes, I need to make sure the post type is set to a specific post type before it re-submits the form.

    Anonymous
    #213838

    Actually I figured out a way. Since I’m working in WordPress, most of what I need is in the query vars. So I manually re-created the url that the form generates when it filters by post type, and just ensured that I was pulling the shortcode ID, the search term that is encoded dynamically so that it wasn’t hardcoded.

    Worked like a charm without having to worry about hooks or JS or anything fancy. I added a setting on the admin so that I could add the ID of the shortcode and have it dynamically pull into the template on the shortcode call without having to hardcode that either. So it’s there as a fall back in case the shortcode ID isn’t present in the query vars.

    Here is an example of what the code looks like, in case it helps someone:

    
    $search_term        = get_search_query();
    $get_shortcode_id   = Search_Helpers::get_sf_pro_shortcode_id();
    $shortcode_id_query = isset( $wp_query->query_vars['sfid'] ) && ! empty( $wp_query->query_vars['sfid'] ) ? $wp_query->query_vars['sfid'] : $get_shortcode_id;
    $raw_search_term    = rawurlencode( $search_term );
    
    <div class="alert alert-info event">
    					<p>We've found upcoming events related to your search term "<?php echo $search_term; ?>".
    						<a href="<?php echo home_url() . '/?sfid=' . $shortcode_id_query . '&_sf_s=' . $raw_search_term . '&post_types=event'; ?>">
    							Go to related events
    							<span aria-hidden="true" class="glyphicon-chevron-right glyphicon"></span>
    						</a>
    					</p>
    				</div>
    
    Trevor
    #213840

    Thanks for sharing. Great to see you got it sorted.

Viewing 5 posts – 1 through 5 (of 5 total)
Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.