AnonymousInactive
Might have made some progress. When I change the priority I now get no results instead of results in the wrong order. Here’s a code sample:
function filter_sort_by_start_date( $query_args, $sfid ) {
if( $sfid==2298 ) {
$query_args = array(
'meta_key' => 'class_date',
'orderby' => 'meta_value_num',
'order' => 'DESC'
);
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'filter_sort_by_start_date', 2000, 2 );
I’m getting the “no result” result even if I comment out the other ‘pre_get_posts’ calls.
Hi Max
I had a look, it seems to me there is something wrong with your code sample.
The filter receives a $query_args object, which contains the wp_query args that S&F defines, and at the end of the function it needs to return this $query_args object.
Inside the function you are free to modify this as you want, which it looks like you got close to do doing but not quite there..
You add your meta query to a new variable called $args, which never gets used.
I would do it like the following (untested – please also copy & paste into editor to see my comments, this is a bit clunky):
<?php
function filter_function_name( $query_args, $sfid ) {
if( $sfid == 940 ) {
// I would use some known values for testing (I just made some dates up here)
$tender_start = "20170101";
$tender_end = "20170101";
/*global $searchandfilter;
$sf_current_query = $searchandfilter->get(940)->current_query();
$array_data = $sf_current_query->get_array();
$tender_start = $array_data['_sfm_tender_start']['active_terms'][0]['value'];
$tender_end = $array_data['_sfm_tender_end']['active_terms'][0]['value'];*/
//if we want to play nice and not completely replace the existing meta query args
//there might be, then we need to check if some meta queries already exist and treat it differently
$meta_query = array(
'relation' => 'AND',
array(
'key' => 'tender_start',
'compare' => '>=',
'value' => $tender_start
),
array(
'key' => 'tender_end',
'compare' => '<=',
'value' => $tender_end
)
);
if((isset($query_args['meta_query']))&&(is_array($query_args['meta_query'])))
{//this means there are some meta queries already here, so push this one onto the end
array_push($query_args['meta_query'], $meta_query);
}
else
{//else there aren't any meta queries
//$query_args['meta_query'] should always be an array of meta queries according to wp docs
$query_args['meta_query'] = array($meta_query);
}
// not sure if you need these, you should set the order via the S&F admin (in the <code>posts</code> tab)
$query_args['meta_key'] = 'tender_start';
$query_args['orderby'] = 'meta_value_num';
$query_args['order'] = 'ASC';
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
I’ve written that in my editor so it should be correct syntax, but I haven’t actually tested it, its just showing you how to properly use the $query_args to make the changes you want.
Hope that helps
AnonymousInactive
Thank for your answer. Do you have a code sample? I tried some but no result.
function filter_function_name( $query_args, $sfid ) {
if( $sfid == 940 ) {
global $searchandfilter;
$sf_current_query = $searchandfilter->get(940)->current_query();
$array_data = $sf_current_query->get_array();
$tender_start = $array_data['_sfm_tender_start']['active_terms'][0]['value'];
$tender_end = $array_data['_sfm_tender_end']['active_terms'][0]['value'];
$args = array(
'meta_key' => 'tender_start',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'tender_start',
'compare' => '>=',
'value' => $tender_start
),
array(
'key' => 'tender_end',
'compare' => '<=',
'value' => $tender_end
)
)
);
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
AnonymousInactive
Hello,
I am having some issue with the filtering. I am trying to add an argument that will list my posts in order of date in descending order. Here is what I’ve come up with:
function filter_function_name( $query_args, $sfid ) {
//if search form ID = 225, the do something with this query
if($sfid==334)
{
$query_args = array(
'meta_key' => 'webinar_date',
'orderby'=> 'meta_value',
'order' => 'DESC'
);
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
But it comes up with no result with the above. Could you please help me with this.
Thanks in advance
mmm. OK. are you creating wordpress pages and then using a shortcode or similar to place a given group on a given page? Do you have a custom field on the pages to hold the ID of the group?
What I am thinking is that you could have a hidden field in the form which is the group (or group ID).
Then, in the child theme functions.php you can have some custom PHP that checks if a page is being loaded, and if it is, to find the value held in the custom field that holds the group ID, and if it does have a group ID, to then use our sf_edit_query_args filter to modify the group ID field in the form to be that of the custom field on that page.
The only problem that I can see is that S&F forms require a results url, which would be different for each page. I cannot see how you would get around this.
AnonymousInactive
Hi Ross,
Thanks for getting back to me on this.
Unfortunately i can’t share a link to the site at the moment as i’m working on it locally but i will try and upload it live somewhere today so you can have a look at it.
And yes, it displays the posts associated with the $post->ID correctly when you first go to the page and also shows the search options for just those posts. It’s when i make a search it then defaults back to all posts 🙁
However, if i don’t include an if statement and just use the code below it then shows the posts under the $post->ID 54.
function filter_function_name( $query_args, $sfid ) {
global $post;
if($sfid==42) {
$query_args = array (
'post_parent__in' => array('54'),
);
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'filter_function_name', 10, 2 );
AnonymousInactive
Hi Trevor,
Have you been able to forward this message to the developer yet?
I have realised there is another method i could try to achieve what i want but this would require to create multiple search forms and then target their IDs individually using the sf_edit_query_args function.
I understand the developer is a busy man and i don’t want to be a pest but are you able to give an estimated time for when you think he’ll get a chance to look at it?
This is for a project i’m working on for a client in which the deadline is at the end of the month so if the waiting list is long then i don’t mind going for the alternative method.
Many thanks
AnonymousInactive
Hi,
I’m trying to display posts related to their parent using just one search form. I’ve been trying to do this by using the sf_edit_query_args filter you have in your documentation but it’s only working when first visit the page. Once you start searching it defaults back to showing all the posts.
I’m using the shortcode to display in a single custom post type template with AJAX enabled.
Here’s what i have:
function filter_function_name( $query_args, $sfid ) {
global $post;
if($sfid==42) {
if($post->ID == 54) {
$query_args = array (
'post_parent__in' => array('54'),
);
}
else if($post->ID == 5) {
$query_args = array (
'post_parent__in' => array('5'),
);
}
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'filter_function_name', 10, 2 );
It works if i remove the if statements from within if($sfid==42) {. For example if this works:
function filter_function_name( $query_args, $sfid ) {
global $post;
if($sfid==42) {
$query_args = array (
'post_parent__in' => array('54'),
);
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'filter_function_name', 10, 2 );
Are you able to help me?
AnonymousInactive
I’ve started to work on the code by beginning with showing only the featured therapists.
I’ve got the code
add_filter( 'sf_edit_query_args', 'filter_featured_first', 20, 2 );
function filter_featured_first( $query_args, $sfid ) {
if($sfid==491)
{ $query_args = array('meta_query' => array(
'relation' => 'AND', array('key' => 'featured', 'value' => 1 )
), 'posts_per_page' => 4
);} return $query_args;}
However, I can’t get the pagination to work. The same posts are being shown on every page.
How do I fix the pagination? And how do I get it to work with the above function edit_posts_orderby()?
AnonymousInactive
Thanks so much for your help.
What do you mean by a ‘sort field’? Do you mean in the Search Form UI?
I’m using ‘post meta’ and ‘search’ fields. You can see the filter on http://gethelpisrael.mgtestsite.com/therapist/?_sfm_medical_professional_type=Therapist So would I be ok then?
Also, how would I add the code for the randomisation with the pagination?
Could I leave the filter: add_filter(‘posts_orderby’, ‘edit_posts_orderby’);
and add add_filter( ‘sf_edit_query_args’, ‘filter_function_name’, 20, 2 ); with my custom code and they would both work?
Or do I need to combine them into one function?