0

I am trying to display the current WordPress search term on single.php, index.php, and category.php...basically when someone searches for something I'd like WordPress to remember what the last search term was and display it anywhere on my site, is that possible?

The function get_search_query only works on search.php

Thanks,
Josh

6
  • 1
    What do you mean by "remember" precisely? For each user? Anonymous visitor? For how long? It's not quite "current" when you are no longer in search context.
    – Rarst
    Jul 21, 2014 at 19:05
  • Just until the next search has been performed or after the standard php session time (24 minutes) Jul 21, 2014 at 19:38
  • 1
    I imagine it would need to be saved in a cookie, it can't be user meta because the visitor may not be logged in. Keep in mind this won't work if the solution is PHP based and you're relying on page caching, it'd have to be javascript based, which probably means local storage. get_search_query only works on search.php because search.php is used when searching. If you're not searching it won't work as there is no search query to get. This implies a lack of understanding of how the templating system works and how templates are loaded
    – Tom J Nowell
    Jul 21, 2014 at 20:01
  • Also WordPress doesn't use PHP Sessions it uses cookie sessions
    – Tom J Nowell
    Jul 21, 2014 at 20:07
  • See comment on my answer to your related question: wordpress.stackexchange.com/questions/155331/… Jul 21, 2014 at 21:44

1 Answer 1

0

Tom,

You're right! I didn't know how templates function, this was not something major, I just wanted to get that value and then display that value on my single.php page - but now I see that's not how WordPress works. I decided to just leave off that functionality :-)

Thanks,
Josh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.