Showing the search query

Forums Forums Search & Filter Pro Showing the search query

Viewing 7 posts – 1 through 7 (of 7 total)
  • Anonymous
    #91803

    I can’t seem to get the search query to show on my page. It does persist in the search box but Ideally I’d like to add a title to the page saying “You searched for…”

    Echoing out the get_search_query(); isn’t returning anything. I’ve tried it both inside and outside of the loop.

    It’s a pretty simple template …. here’s what I have:

    
    <?php get_header(); ?>
    <?php if ( have_posts() ) : ?>
        <?php $search_query = get_search_query(); ?>
        <?php print_r($search_query);?>
    
    <div id="thumbnails">
    <?php while ( have_posts() ) : the_post();?>
    
            <?php
            // Get the Post category
            $categories = get_the_category();
            $className = strtolower(esc_html( $categories[0]->name ));
            ?>
    
        <div class="thumbnail <?php echo $className;?>">
            <a class="thumbnail-link" href="<?php the_permalink();?>">
                <div class="thumbnail-overlay-text">
                    View Image
                </div>
            </a>
    
            <?php the_post_thumbnail('thumbnail'); ?>
        </div>
    
    <?php endwhile; ?>
    </div>
    <?php endif; ?>
    
    <?php get_footer(); ?>
    Anonymous
    #91984

    Still Trying to get this to work to no avail. The WordPress default
    get_search_query();
    returns nothing.

    Trevor
    #92106

    You need instead to use code like this to get the query:

    global $searchandfilter;
    $sf_current_query = $searchandfilter->get(339)->current_query();

    Where 339 is the form ID.

    You might need to dump the array to get an idea what is in it, but a search of the forum reveals some code snippets:

    https://support.searchandfilter.com/forums/search/sf_current_query+searchandfilter+current_query/

    I think it is possible that the next major release (V3, which we are working on right now) will have a much better way of doing this).

    Anonymous
    #92142

    OK thats I got it.

    For anyone else who stumbles across this here’s the full code to output the Search Query

    Firstly this gets the Query (change number to your search form ID) – place in the loop in your template:

    global $searchandfilter;
      $sf_current_query = $searchandfilter->get(54)->current_query();
      $searchTerm = $sf_current_query->get_search_term();

    and then you can echo out the query in your anywhere in your template
    <?php echo $sf_current_query; ?>

    Anonymous
    #92146

    Opps sorry that second line should be:

    <?php echo $searchTerm; ?>

    Trevor
    #92152

    I can close this thread?

    Anonymous
    #92177

    Sure can!

Viewing 7 posts – 1 through 7 (of 7 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.