-
Search Results
-
Topic: Date Range Filter
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_argsfunction. 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_argsfunction, 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
Hi there,
I have search and filter form where I need to set a custom meta query based on the values held within a dynamic array variable based on the user who is logged in (which controls what they see).
So manually I tried the following:
Post Meta > post_product = 1134
Which is similar to what I want, and of course that works BUT I need the value of that query to be a dynamic array of product ID’s instead (held within $arr_product_ids in my example) depending on the user logged in, ie as follows:
'meta_query' => array( 'key' => 'post_product', 'value' => $arr_product_ids, 'compare' => 'IN', ),With that in mind, I have tried: https://searchandfilter.com/documentation/search-results/custom/ and added the following to my functions.php file but sadly this has not worked:
global $arr_product_ids; $args = array( 'post_type' => 'post', 'posts_per_page' => 12, 'category_name' => 'news', 'meta_query' => array( 'relation' => 'AND', 'post_product' => array( 'key' => 'post_product', 'value' => $arr_product_ids, 'compare' => 'IN', ), 'post_urgent' => array( 'key' => 'post_urgent', 'compare' => 'EXISTS', ), 'post_priority' => array( 'key' => 'post_priority', 'compare' => 'EXISTS', ), ), 'orderby' => array( 'post_priority' => 'DESC', 'post_urgent' => 'DESC', 'date' => 'DESC' ), ); $args['search_filter_id'] = 2978; $query = new WP_Query($args);I then looked at https://searchandfilter.com/documentation/action-filter-reference/ and tried adding the following to functions.php, but sadly again no luck:
function filter_function_name( $query_args, $sfid ) { if($sfid==2978) { $query_args['post_product'] = $arr_product_ids; } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );Please could you ket me know where I’m going wrong with this as I think I’m going around in circles sadly, whereas you will know the issue immediately I’m sure :).
Many thanks,
TJI have a WP search bar that when the user types in the keyword, it directs them to the search.php template. There, the results display and there is a Search & Filter Pro form above the results.
The good news is that for the same keyword, both the WP search and S&F Pro return the same number of results. The bad new is, that the order isn’t matching. I’m not sure why yet.
I’m using the
sf_edit_query_argshook to alter the query arguments for S&F. I’ve made it so the query vars from S&F match to the WP search which I’m also altering usingpre_get_posts.The only thing that does not match, is that S&F has the
post__inarg with IDs stored whereas the default WP search has it as an empty array. Could this be why the order of the results from S&F pro differ from that of the WP search results? I tried setting it to an empty array on the hook, and set the priority to 15 but it doesn’t seem to overwrite it.If it’s helpful, I’ve set these query args both for WP search and S&F search:
order = DESC
post__not_in = some IDs I’ve passed
post_in = empty array (only for S&F where I’m trying to force it, WP already does this)
orderby = ‘meta_value date’
post_status = ‘publish’
meta_query – Relation: OR, checks two keys whether they EXISTS/NOT EXISTSIt seems like the meta query that’s not kicking in. On the WP search, it takes the posts with the post meta I’m looking for that exists, and places them on top of the results with the date/desc as the secondary sorting. Then the remaining results follow also in date/desc order.
The S&F Pro results don’t seem to be doing that correctly. Anything I can try or other args/query vars I can check?
Thanks.
Hey Trevor,
Thanks for trying to help me this am. I have implemented the hook in functions php but it has brought on other issues now.
Issue #1 –
Current page is not recognized. So when you click page 2, it does bring load page 2 via ajax but the “current” class is not applied to the number 2 in pagination. So “1” is still treated as the current page from css standpoint thus to the user it looks like they are on page 1.Issue #2 –
The goal is for the homepage to display shows today and in future. But for the user to be able to search for all shows past and present. This is how I successfully had it set up at first. When using the below function, yes the homepage only displays shows in the future when you initially load the page but it also makes it so you can not search shows in the past. Again, the goal is for home page to display today and future shows but for the user to be able to search both past and future.function include_exclude_search_filter( $query_args, $sfid ) {
`//if search form ID = 1113, the do something with this query
if($sfid==1113)
{
//modify $query_args here before returning it
$query_args[‘meta_query’] = array(‘relation’ => ‘AND’,
array(
‘post_type’ => ‘show’,
‘posts_per_page’ => 12,
‘max_num_pages’ => 99999,
‘meta_key’ => ‘show_date’,
‘order’ => ‘ASC’,
‘orderby’ => ‘meta_value’,
),
array(
‘key’ => ‘show_date’,
‘compare’ => ‘>=’,
‘value’ => $today,
),
);
}
elseif($sfid==1234)
{}
return $query_args;
}
add_filter( ‘sf_edit_query_args’, ‘include_exclude_search_filter’, 20, 2 );`