Forums › Forums › Search & Filter Pro › Date Range Filter
Tagged: ajax, custom query, date range, sf_edit_query_args, V3
- This topic has 3 replies, 2 voices, and was last updated 5 years, 2 months ago by Trevor.
-
Anonymous(Private) September 14, 2019 at 11:37 am #221016
hi Ross, hi Trevor,
you guys are really supportive and i read a lot of posts on the forum. i still can’t figure out what i am trying to do alone, so i thought i would reach out.
1 – latest WP 5.2.3
2 – S&F Pro
3 – ACF Pro (latest)
4 – custom theme from scratchon the news search page (which is the standard posts type of WP), i am trying to do this:
1 – allow a category filter
2 – a search box
3 – allow a date range filter with the terms “past month, 6 month, past year, older than 1 year”1 and 2, no problem. works perfectly.
for date range, since that’s not an existing feature, i am trying to trick it to working.
i read about the action filters, and i read about the various hooks.
i realize that’s not something that can be done with a regular date field on Post Date.
so i need to find another way.I am able to add a
'date_query'
in asf_edit_query_args
function. When I hard code it like that it works just fine. But I want to give the visitor the dropdown that allows that choice.I tried to inject a dropdown in the form but it was being ignored by the Ajax function.
I tried to use a dummy ACF field, called “sf-date-range” which contains the options I want for that (-1 month, -6 month, etc.) and build the date_query from that. I am able to do so with thesf_edit_query_args
function, like this:/* custom search and filter query update based on date range */ function custom_date_query_for_sf( $query_args, $sfid ) { global $searchandfilter; $sf_current_query = $searchandfilter->get(2467)->current_query(); $customFieldsArray = $sf_current_query->get_array(); $customHumanDate = $customFieldsArray["_sfm_sf-date-range"]["active_terms"][0]["value"]; //if search form ID = 2467 // news search, the do something with this query if($sfid == 2467) { if ($customHumanDate && $customHumanDate != NULL) { $query_args['date_query'] = array(array('after' => $customHumanDate,)); } echo "<pre>"; print_r($query_args); echo "</pre>"; } return $query_args; } add_filter( 'sf_edit_query_args', 'custom_date_query_for_sf', 20, 2 );
however, since that ACF field is not really being used, the search returns blank since those fields aren’t checked, ever. the query is built ok, and again when I hard code it without the use of that ACF field, works fine:
this example, hard-coded, returns exactly what’s expected (posts older than 1 year and younger than 2 years) :
`
Array
(
[paged] => 1
[search_filter_id] => 2467
[search_filter_override] =>
[posts_per_page] => 10
[post_status] => Array
(
[0] => publish
)[category__not_in] => Array
(
[0] => 42
)[meta_query] => Array
(
)[post_type] => post
[orderby] => Array
(
[date] => DESC
)[date_query] => Array
(
[0] => Array
(
[before] => -1 year
)[1] => Array
(
[after] => -2 year
))
)
‘so I know my query works.
when i use the ACF field to generate the dropdown and make it all Ajax and neat, i don’t get the right results, even though I get the right query built (I have to refresh the screen to see the query, but i see it and it’s good. by the way, can I make the print_r that’s got the query printout ajaxified so that it shows me the right values on form change?)…
I think the issue is that the ACF field I am using as a dummy dropdown generator is being read and obviously no values are in there since it’s not really being used. I tried to “unset” or “ignore” the ACF field which I only use to make it ajax and friendly…
do you have a suggestion on how to accomplish what I am trying to accomplish? I know that v3 will allow me to make my own fields (which is what I am trying to do really here but without that) – but that’s not something I can wait on at the moment.
i can send you a link in private if you want to see what I am doing.
much appreciated,
thanks —
Kaz
Trevor(Private) September 16, 2019 at 1:33 pm #221076That was a long post Kaz!
I think that, in the V3 production cycle we will add more flexibility to searching a sorting to handle what happens if a post does not have a value for the required meta key, but for now, what you did is the way to go.
Also, when using Ajax, the Ajax Container has to exist even if no posts are found, so the page HTML structure has to be planned. Generally, that container needs to be outside the while have_posts loop an the if have_posts part needs an else to display the message.
-
AuthorPosts