Forums › Forums › Search & Filter Pro › How to use Active Query Object contents in $args query
Tagged: Use Active Query Object Contents
- This topic has 4 replies, 3 voices, and was last updated 3 years, 10 months ago by Anonymous.
-
Anonymous(Private) January 29, 2020 at 10:17 pm #232559
I have a need to refresh the custom results page using an html form that controls how results are displayed, (map VS list). When the page gets called by itself, (form action = call this page), setting the WP_query $args, (in my case… $args = array(‘search_filter_id’ => 1576,’post_type’ => ‘post’,’paged’ => $paged); ), doesn’t work since the value for ‘search_filter_id’ => 1576 is NULL when the page gets refreshed. I’ve tried a few ways to contruct a new $args value that I set in a $_SESSION variable but can’t get it to work. Here’s what I’ve tried:
Executed on initial page display—Works
1. $sf_current_query = $searchandfilter->get(1576)->current_query();
2. $original_sf_query_args =array($sf_current_query->get_array());
3. $passed_args_array = array($original_sf_query_args,’post_type’ => ‘post’,’paged’ => $paged);
4. $_SESSION[‘passed_args_array’] = $passed_args_array;5. $args = array(‘search_filter_id’ => 1576,’post_type’ => ‘post’,’paged’ => $paged);
6. $query = new WP_Query($args);Executed on subsequent page displays—Doesn’t Work
1. $args = $_SESSION[‘passed_args_array’];
2. $query = new WP_Query($args);What might I be doing wrong?
Anonymous(Private) January 30, 2020 at 3:31 am #232561I see now why this won’t work as the WP_Query $args value on subsequent page calls is meaningless to WP_Query .
What I’m attempting to do now is to extract the search term and selected form value for each of the search terms and then construct a conventional $args array using database item names and the values entered into the SFM form. I’ve not been successful yet in using any of the examples in the “Accessing the Search Data” section in the documentation other than accessing the value for any search text entered. I’ve got a form item named “_sfm_listing-city” but when I try to find the value selected in the form I just can’t get the proper syntax on my PHP call.Trevor(Private) January 30, 2020 at 8:47 am #232565To fetch the search terms, the https://searchandfilter.com/documentation/accessing-search-data/ guide is basic but you can extend the idea to grab lots of other data. If you have other filters, then it becomes a little more complex, but I can give you links. This thread might help you:
https://support.searchandfilter.com/forums/topic/accessing-field-slug-on-search-results/
… and this search will give similar threads I think:
https://support.searchandfilter.com/forums/search/sf_current_query+get_array
Note, if you are using Ajax refreshing of the results, any PHP needs to be inside the Ajax Container, or it will not update.
You could temporarily use this code in your results template to display the full filter array so that you can work out the exact array part that holds the data you want:
<?php global $searchandfilter; $sf_current_query = $searchandfilter->get(1391)->current_query()->get_array(); echo '<pre>',print_r($sf_current_query,true),'</pre>'; ?>
Where the ID needs to match that of your form.
-
AuthorPosts