I have a form for a client where you can select various fields and this will do an AJAX search, and return the results for a custom post type.

Now, what the client wants is to have a 'back to search results' button when he clicks on the single result (certain single CPT).

My idea was to attach a query parameter on the url of each result's permalink that will have json of the query arguments, something like

<a href="' . get_permalink() . '"?query_string=' . wp_json_encode( $args ) . '>Find out more<a>

I could then read out the query parameter, and pass it on the single custom post type page to the back link, which will then again be passed as a query parameter once the user clicks on it. This way I can check if such parameter exists, and based on that parse it and run the query I usually do on my ajax search.

This sounds ok, but I'd need to encode the parsed array, and there could be tons of issues with it down the road (for instance just passing it as wp_json_encode doesn't work, I probably need to use htmlentities or something like that).

I was thinking of temporarily storing the arguments array in the transient, but this isn't good, because what if someone else did another search while the first person is on the single page? That would change the results, and ultimately wouldn't work.

I also added that query to the response object in my ajax, but this is only valid on the results page. Is there a way to localize that object temporarily, or that query string, so that it persists on the next page?

Any ideas are welcomed.


The json of the query arguments is

            "Car parking"
  • Could you please provide us with an example of your search queries? This way i will know what i'll be dealing with.
    – Johansson
    Jun 10, 2017 at 16:32
  • developer.mozilla.org/en-US/docs/Web/API/History_API or github.com/browserstate/history.js for supporting older browsers
    – inarilo
    Jun 10, 2017 at 17:25
  • Will check out the history api, thanks! @JackJohansson It's a custom query that has both tax and meta query in it (I know, the meta query is a slow query but I can't avoid it). I'll paste the json string in the question
    – dingo_d
    Jun 10, 2017 at 18:25
  • Can you please make up your code. We don't need a single line several kilometers long. Please, format it for readability
    – Max Yudin
    Jun 10, 2017 at 21:06

1 Answer 1


I solved this using the HistoryAPI.

Since my ajax returns objects that has response, html and query, I pushed the html results of the search to the history state with

window.history.pushState( date_out.html, null, null);

where data_out is the object returned from the ajax call.

Then I added a button on the single advert page that will do


on click. This will send me to the search results page. Then I can check the url of the search page (this can be hardcoded or pulled from the localization object where it's set with WordPress), and if there is anything in the history.state which corresponds to the search html I just place it after the desired div.

Then inside that check I also push the empty state in the history just to be sure that when you go to some other page and push back that you won't see any search results.

Seems to be working fine :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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