• Resolved Griff

    (@blgerber)


    Hi I have kind of an unusual task. I would like to change the query string shown in the url bar to use “?q=” instead of “?s=”

    I’ve looked into altering the form as well as using some php to intercept and alter the query before sending it to Relevanssi.

    I am using Elementor, but am okay creating a custom search submission form if need be.

    I am not very experienced in the realm of search so any advice or resources would be very appreciated.

    Thanks!

    • This topic was modified 1 year, 3 months ago by Griff.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Mikko Saari

    (@msaari)

    This isn’t about Relevanssi; using s as the query string parameter runs pretty deep in WordPress core. You can try modifying the query in the parse_query hook: move the query string to s and set is_search to true there:

    add_action( 'parse_query', function( $query ) {
    if ( isset( $_REQUEST['q'] ) && ! empty( $_REQUEST['q'] ) ) {
    $query->set( 's', $_REQUEST['q'] );
    $query->is_search = true;
    }
    } );
    Thread Starter Griff

    (@blgerber)

    Hi Mikko,

    Thanks for taking the time to respond even though I now see this might not be the most relevant place to post!

    I added your code, and interestingly now I can change the s to q in the url string and the query succeeds and returns results (it did not before), but when I just use the search bar, it still defaults to using s.

    Any other ideas? No worries if not, I have a lot better idea where to explore now!

    Plugin Author Mikko Saari

    (@msaari)

    You need to change the name of the input field in your search form from s to q.

    Thread Starter Griff

    (@blgerber)

    Oh wow, you’re my hero! Thanks so much for taking the time to help out! And best part is I’ve learned a little about how all this works. Very cool.

    I’ve broken my counter function, but I’m pretty sure I can hopefully figure out a work around for this one.

    function total_search_results() {
    if (is_search()) {
    global $wp_query;
    return $wp_query->found_posts;
    }
    }

    function search_results_count_shortcode() {
    return 'Total Results: ' . total_search_results();
    }
    add_shortcode( 'search_results_count', 'search_results_count_shortcode' );

    Thanks again for taking the time, Relevanssi is super cool and I’m excited to use it!

    • This reply was modified 1 year, 3 months ago by Griff.
    Thread Starter Griff

    (@blgerber)

    Actually in case anyone else finds this with similar goals, jet engine supports the count out of the box with dynamic tags! Thanks again for the help and making the community such a positive experience!

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Change query var from s to q’ is closed to new replies.