-
Search Results
-
Topic: Adding own select fields
Hi there!
I’m a bit out of ideas at the moment… I have a search form for a custom post type where I also need to search meta fields of a custom taxonomy within this post type. Depending on what is chosen in the form, the search values for the taxonomy changes, so I can not add the information directly to the post type.
What I think about now as a possible solution is to alter the meta_query with filter “sf_edit_query_args”, use own non-dynamic select fields in the search form, make my queries in the function and just exclude posts with post__not_in.
I can add the select fields with jQuery or maybe manipulate a templates? But these fields also need to update the ajax with their GET values so that I can use them with the filter…
I hope you understand me and I can send you whatever you need. Maybe there is another way, too. I’m very open for any idea.
Thank you very much!
Hi there,
So I have a list of reference IDs (ACF Field for each post) in an array which I use in a meta_query to filter posts further after search and filter does its thing. This array contains post references in a specific order and the order I would like the posts to display.
Here is the query I am using:
‘paged’ => int 1
‘search_filter_id’ => int 47
‘search_filter_override’ => boolean false
‘posts_per_page’ => int 10
‘post_status’ =>
array (size=1)
0 => string ‘publish’ (length=7)
‘meta_query’ =>
array (size=1)
0 =>
array (size=3)
‘key’ => string ‘post_reference’ (length=13)
‘value’ => ARRAY OF POST REFS HERE
‘compare’ => string ‘IN’ (length=2)
‘post_type’ => string ‘custom_post’ (length=8)
‘orderby’ => string ‘meta_value’ (length=10)`Now obviously we can’t override post__in which would be ideal as I can pass an aray of post IDS rather than references, is there no way whatsoever we can do this?
I tried to order by the meta_value which is the array of post refs, but it just orders them alphabetically rather than in the order they are in the given in the array.
Can you think of a way I can work with SAFP to get this working correctly?
Thanks
Hello!
I’m having some trouble editing the search query. I’m using the plugin alongside Relevanssi and want results to be sorted by relevance as well as post_date or modified date. I tried adding this to the functions file
function filter_function_name( $query_args, $sfid ) { //if search form ID = 225, the do something with this query if($sfid==5671) { //modify $query_args here before returning it $query_args['orderby'] = 'post_date'; } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );But it doesn’t amend the search URL or results.
Any help would be greatly appreciated!
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