AnonymousInactive
I have a filter set up to find all posts with a relationship to an ACF called “people”, it returns all results with a relationship to a “person” post but I would also like to return the “person” post itself in the search results.
What is the easiest way to include it in my results?
I have tried something like this:
function people_filter( $query_args, $sfid ) {
if($sfid == 7287)
{
if ($_GET["_sfm_people"] != "") {
$query_args['p'] = intval($_GET["_sfm_people"]);
}
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'people_filter', 10, 200 );
But this returns the “person” post only, I would instead like to add it to the already compiled results.
AnonymousInactive
Hello,
I am trying to add a custom filter to search a meta field, based on an input field that the user types in.
function filter_function_name( $query_args, $sfid ) {
//if search form ID = 1880, then do something with this query
if($sfid==1880)
{
//modify $query_args here before returning it
$meta_query = array(
'key' => 'NAM-Authors',
'value' => $query_args['s'],
'compare' => 'LIKE'
);
$query_args['meta_query'] = array($meta_query);
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
This is not returning the results I am looking for. Any idea what I am doing wrong? Thank you for your time.
The first thing to do is, in the code, before you make a change, use the PHP print_r function to output the query array to the screen, and that, after you modify it, do the same again, and compare the arrays. Like this:
echo '<pre>',print_r($sf_current_query,true),'</pre>';
I have not actually used the sf_edit_query_args function myself.
AnonymousInactive
Hi Trevor.
I tryed using print_r and var_dump in functions.php but nothing is showing up on front-end. Like below:
function filter_function_name( $args, $sfid ) {
//if search form ID = XX, the do something with this query
if ( $sfid == 2817 ) {
var_dump($args['post__in']);
print_r($args['post__in']);
}
return $args;
}
add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 200 );
I tryed using var_dump($args['post__in']); in my custom page template and it returns NULL.
AnonymousInactive
Hello guys,
I have exactly the same problem of this topic (resolved):
https://support.searchandfilter.com/forums/topic/adding-post-ids-to-returned-results/
I did what you suggest, but it returns all posts instead my array on $args[‘post__in’].
If I use $args[‘p’] or $args[‘meta_query’] for example, it works fine.
Here is my code:
function filter_function_name( $args, $sfid ) {
//if search form ID = XX, the do something with this query
if ( $sfid == 2817 ) {
$args['post__in'] = array(3662, 3667); // for example
}
return $args;
}
add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 200 );
Thanks in advance 🙂
AnonymousInactive
Hi Trevor,
We renewed our license. Thanks for the reminder!
I’m trying to walk through the doc you shared and having a little trouble. Essentially, I just want to say that for Search Form 2051 the Category should always be All Items. Even when a user has selected a category that is being used with the other Search result filter on the page.
Here’s the code from the doc with the appropriate Search Form ID:
function filter_function_name( $query_args, $sfid ) {
//if search form ID = 2051, the do something with this query
if($sfid==2051)
{
//modify $query_args here before returning it
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
I’ve tried to figure out the syntax for saying Category == All without luck.
Would appreciate your help.
Thanks!
AnonymousInactive
Hi Trevor,
some of my WooCommerce products have a meta key “_tribe_event_starttime” and with your plugin I have setup an AJAX filter ordering these products by that meta key.
Everything was fine for weeks but today I realized that the order has fallen back to the WC default (creation) “date”. You can see the result list by clicking on one of the big images on the top.
https://obsthof-am-steinberg.de/produkt-kategorie/event-feste-kinder-musik/
As you can see the AJAX requests has the GET parameter orderby=date and I tried to alter this order by hooking into your filter which I found here on your website:
// alter query for search & filter results
add_filter( ‘sf_edit_query_args’, ‘sf_orderby_eventdate’, 10, 2 );
function sf_orderby_eventdate ($query_args, $sfid) {
if ($sfid == 6047) {
$query_args[“meta_key”] = ‘_tribe_event_starttime’;
$query_args[“order_by”] = ‘meta_value’;
$query_args[“order”] = ‘ASC’;
}
return $query_args;
}
Without success. I have installed WP Super Cache last week. Could this be the reason? Is there anything I missed or doing wrong?
Thank you for your support!
Oliver
Now I am
AnonymousInactive
Wooo, that got it. Thank you for your patience.
For anyone else with this issue please see my code below. Probably not perfect but it works. 🙂
// Exclude the default range values from the search as they are meaning the search returns no results
function remove_default_range( $query_args, $sfid ) {
//if search form ID = 225, the do something with this query
if($sfid==25 && $query_args) {
if((isset($query_args['meta_query']))&&(is_array($query_args['meta_query']))) {
global $searchandfilter;
$sf_current_query = $searchandfilter->get(25)->current_query()->get_array();
/* echo "<pre>";
print_r($sf_current_query);
echo "</pre>";*/
echo $sf_current_query['_sfm_minimum_per_round'][0]['value'];
if($sf_current_query['_sfm_minimum_per_round'][0]['value'] == '0' && $sf_current_query['_sfm_minimum_per_round'][0]['value'] == '50000') {
unset($sf_current_query['_sfm_minimum_per_round']);
}
if($sf_current_query['_sfm_minimum_per_season'][0]['value'] == '0' && $sf_current_query['_sfm_minimum_per_season'][0]['value'] == '1000000') {
unset($sf_current_query['_sfm_minimum_per_season']);
}
}
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'remove_default_range', 20, 2 );
AnonymousInactive
Sorry to bother you, I’m trying to modify the query, however the $query_args[‘meta_query’] is returning a blank array whenever I print it out. This is even after I have altered a field and submitted the form.
function remove_default_range( $query_args, $sfid ) {
//if search form ID = 225, the do something with this query
if($sfid==25 && $query_args) {
if((isset($query_args['meta_query']))&&(is_array($query_args['meta_query']))) {
print_r($query_args['meta_query']);
}
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'remove_default_range', 20, 2 );