Support Forums

Hey there, it looks like you've been redirected from designsandcode.com - as you can see we have a new home - read more

Looking for support? You can access the support system via your account.

Forums Forums Search & Filter Pro Showing the search query

Viewing 7 posts - 1 through 7 (of 7 total)
  • Ian Clarke
    #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(); ?>
    Ian Clarke
    #91984

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

    Trevor Moderator
    #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).

    Ian Clarke
    #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; ?>

    Ian Clarke
    #92146

    Opps sorry that second line should be:

    <?php echo $searchTerm; ?>

    Trevor Moderator
    #92152

    I can close this thread?

    Ian Clarke
    #92177

    Sure can!

Viewing 7 posts - 1 through 7 (of 7 total)

The topic ‘Showing the search query’ is closed to new replies.