AnonymousInactive
I tried to unset the slider using sf_edit_query_args
, but it doesn’t clear the search results. It still keeps the slider query!
This is what I have tried:
function filter_function_name( $query_args, $sfid ) {
global $searchandfilter;
$current_query = $searchandfilter->get(58376)->current_query()->get_array();
unset( $current_query['_sfm_LENGTH_KEY'] );
return $query_args;
}
add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
But it doesn’t do anything! Posts are still being filtered with the slider query!
AnonymousInactive
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!
AnonymousInactive
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!
Hi Thomas
So I’ve found a better way to do this (that can be search form specific).
The code in full (so remove everything else we’ve added:
function filter_input_object_radio($input_object, $sfid){
if(($input_object['name']!='_sf_post_type')||($input_object['type']!='radio'))
{
return $input_object;
}
if(isset($input_object['options'])){
//remove the first option from the array
$removed_option = array_shift($input_object['options']);
}
//now set the default value in the field
//make sure we only modify a field belonging to a specific Search Form, by ID
if($sfid==15485){ //replace 15485 with your search form ID
//make sure the field is not set
if(!isset($_GET['post_types'])){
//then set it to "post"
$input_object['defaults'] = array("post");
}
}
return $input_object;
}
add_filter('sf_input_object_pre', 'filter_input_object_radio', 10, 2);
function set_default_post_type_query( $query_args, $sfid ) {
//if search form ID = 225, the do something with this query
if($sfid==15485) //replace 15485 with your search form ID
{
//modify $query_args here before returning it
if(!isset($_GET['post_types'])){
$query_args['post_type'] = 'post';
}
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'set_default_post_type_query', 20, 2 );
The only thing you have to replace is the search form ID of 15485
(there are 2 instances of this) – swap this out to match the ID of the search form on the homepage and it should work 🙂
Best
AnonymousInactive
I am using the same filter to modify the main shop search too. This one works fine:
function update_search_category( $query_args, $sfid ) {
$query_args['tax_query'] = array(
array(
'taxonomy' => 'product_cat',
'terms' => '20',
'operator' => 'IN',
),
);
return $query_args;
}
add_filter( 'sf_edit_query_args', __NAMESPACE__.'\\update_search_category', 20, 2 );
AnonymousInactive
Yes it is a function, I was just showing you only the parts which you needed to see, here is it all:
/**
* USER PROFILE - Modify search form args
*/
function user_liked_products_search_filter( $query_args, $sfid ) {
if ( $sfid == 26 ) {
$query_args = array (
'meta_query' => array(
array(
'key' => '_likes',
'value' => 1,
),
),
);
}
return $query_args;
}
add_filter( 'sf_edit_query_args', __NAMESPACE__.'\\user_liked_products_search_filter', 100, 2 );
I think you will need to use this filter (the code goes in to your child theme functions.php file):
https://searchandfilter.com/documentation/action-filter-reference/#edit-query-arguments
Like this:
function exclude_password_protected_posts( $query_args, $sfid ) {
if($sfid==225)
{
$query_args['has_password'] = FALSE;
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'exclude_password_protected_posts', 20, 2 );
where you need to change the ID number (in the example it is 225, so use the ID number of your form).
AnonymousInactive
TJ, another quick example maybe to help:
function filter_function_name( $query_args, $sfid ) {
if($sfid==2978) {
global $arr_product_ids;
$query_args['tax_query'] = array(
array(
'key' => 'post_product',
'value' => $arr_product_ids,
'compare' => 'IN',
),
);
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
AnonymousInactive
Hi TJ,
On your second example, did you try adding: global $arr_product_ids
within the function too? Your trying to use $arr_product_ids variable.
function filter_function_name( $query_args, $sfid ) {
if($sfid==2978) {
global $arr_product_ids;
$query_args['post_product'] = $arr_product_ids;
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
AnonymousInactive
To be honest I am not sure how to create that function – it’s a bit beyond my abilities.
I tried this, but it made the search form stop working.
function filter_function_name( $query_args, $sfid ) {
//if search form ID = 225, the do something with this query
if($sfid==26869)
{
//modify $query_args here before returning it
$query_args[‘post_type’] = array( ‘practitioners’ );
}
return $query_args;
}
add_filter( ‘sf_edit_query_args’, ‘filter_function_name’, 20, 2 );
Just to simply matters, I have removed all the functions.php code altogether for the time being. As you will see on the page, the pagination does not seem to work with the search form on the page. When I put in a manual pagination link of https://staging-universallifetools.kinsta.cloud/our-teachers/page/2 it works, but the actual pagination buttons do not seem to do anything, just refresh the page.