AnonymousInactive
The code got all mixed up, so here I have posted it again
I have couple of issues
See this page
https://acu2020dev.wpengine.com/story
I have created a page template and added the shortcodes
echo do_shortcode('[searchandfilter id="133"]');
echo do_shortcode('[searchandfilter id="133" show="results"]');
I am showing a featured post first and then I am showing the listing
As I dont want to show the featured post I added the following code
function filter_function_name( $query_args, $sfid ) {
//echo “Coming here”;
//if search form ID = 225, the do something with this query
if($sfid==133)
{
//modify $query_args here before returning it
if (is_page_template(‘page-templates/stories.php’)) {
//echo “This is “.get_queried_object_id();
$featured_story = get_field(“featured_story_landingpage”,get_queried_object_id());
$exclude = array($featured_story->ID);
$query_args[‘post__not_in’] = $exclude;
}
}
return $query_args;
}
add_filter( ‘sf_edit_query_args’, ‘filter_function_name’, 20, 2 );
This works alright when the page is loaded, but when I click the Clear filter button or when a category is selected, the above exclusion is ignored and the featured post is shown in the listing
What am I doing wrong? or is this a bug in the plugin?
Also how can I show a numbered pagination?
regards
AnonymousInactive
I have couple of issues
See this page
https://acu2020dev.wpengine.com/story
I have created a page template and added the shortcodes
echo do_shortcode('[searchandfilter id="133"]');
echo do_shortcode('[searchandfilter id="133" show="results"]');
I am showing a featured post first and then I am showing the listing
As I dont want to show the featured post I added the following code
function filter_function_name( $query_args, $sfid ) {
//echo "Coming here";
//if search form ID = 225, the do something with this query
if($sfid==133)
{
//modify $query_args here before returning it
if (is_page_template('page-templates/stories.php')) {
//echo "This is ".get_queried_object_id();
$featured_story = get_field("featured_story_landingpage",get_queried_object_id());
$exclude = array($featured_story->ID);
$query_args['post__not_in'] = $exclude;
}
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
This works alright when the page is loaded, but when I click the Clear filter button or when a category is selected, the above exclusion is ignored and the featured post is shown in the listing
What am I doing wrong? or is this a bug in the plugin?
Also how can I show a numbered pagination?
regards
AnonymousInactive
I used this just to accomplish the first step:
function mk_purchased_filter_function( $query_args, $sfid ) {
//if search form ID = 225, the do something with this query
if($sfid==62057)
{
//modify $query_args here before returning it
$include = array(154400,148684);
$query_args[‘post__in’] = $include;
}
return $query_args;
}
add_filter( ‘sf_edit_query_args’, ‘mk_purchased_filter_function’, 20, 2 );
____________________
BUT: It does not influence my results. Maybe because Relevanssi is used for the filter, too?
Or is there any other explanation?
[searchandfilter id=”62057″]
[searchandfilter slug=”woocommerce-shop”]
AnonymousInactive
Nevermind. I didn’t realize I had to also manually add the meta_query to get the search order to work. Here is my working code:
function filter_function_name( $query_args, $sfid ) {
//if search form ID = 225, the do something with this query
if($sfid==23142) {
$query_args["meta_query"] = array(
'relation' => 'AND',
'subject_code' => array(
'key' => 'subject_code',
'compare' => 'EXISTS',
),
'course_number' => array(
'key' => 'course_number',
'compare' => 'EXISTS',
),
'start_date' => array(
'key' => 'start_date',
'compare' => 'EXISTS',
),
);
$query_args["orderby"] = array(
'subject_code' => 'asc',
'course_number' => 'asc',
'start_date' => 'asc',
);
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
Please feel free to close this ticket. Thank you!
AnonymousInactive
Thank you. I restored the Posts settings to the default order and used the following code, which is not affecting the sort order:
function filter_function_name( $query_args, $sfid ) {
if($sfid==23142) {
$query_args["orderby"] = array(
'subject_code' => 'asc',
'course_number' => 'asc',
'start_date' => 'asc',
);
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
It works if I set just a single order like so:
function filter_function_name( $query_args, $sfid ) {
if($sfid==23142) {
$query_args["orderby"] = 'subject_code';
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
Do I need to format the $query_args differently? That’s how I do it in a regular WP_Query…
Thank you!
AnonymousInactive
Hi there,
we would like to add a reltion OR about two field.
We can’t set it in admin panel so we try to integrate it by php.
In function.php we add this:
function filter_results( $query_args, $sfid ) {
if($sfid==3981) {
$query_args = array(
"meta_query" => array(
'relation' => 'OR',
array(
'key' => 'immobile_disponibile',
'compare' => 'NOT EXISTS',
),
array(
'key' => 'immobile_disponibile',
'value' => '1',
'compare' => 'NOT LIKE',
),
),
"meta_key" => "immobile_disponibile",
'orderby' => 'menu_order',
"order" => "ASC",
);
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'filter_results', 20, 2 );
But… not work.
No not really – we don’t have much in terms of devs accessing our functions to do DB queries etc our way – so so the sf_edit_query_args is usually the way to go.
If your meta query requires a meta key to be an exact value, I can provide some code to set this in the query which should be much faster – its a bit clunky (we hope to improve this) but it taps in to our tables for a performance boost.
Let me know what you’re trying to do (share the snippet of code if you like)
Thanks
Hi Sigurd
After scratching my head I managed to figure out the issue.
We perform our search outside of the regular WP_Query
, so if you choose a category in our search form, WP doesn’t actually “know” its restrciting to a specific category, because our search does that, so WP appends the stick post…
I’ve managed to come up with some code, that add the WP category to the WP_Query
(even though our plugin has already restricted the posts to this category), but now WP also knows that at a category restriction is happening, and excludes the sticky:
add_filter("sf_edit_query_args", "add_wp_category_to_query", 100, 2);
function add_wp_category_to_query($query_args, $sfid){
//if($sfid == 1234){ //you can restrict this behaviour to a search form ID
//get user category selection from the search form (ie, detect which categories have been selected)
global $searchandfilter;
$query_data = $searchandfilter->get($sfid)->current_query();
$query_data_array = $query_data->get_array();
if(isset($query_data_array['_sft_category'])){
$category_values = $query_data_array['_sft_category']['active_terms'];
$current_category_ids = array(); //this will contain all the IDs of the categories that have been selected in the search form
foreach($category_values as $category){
array_push($current_category_ids, $category['id']);
}
//restrict query to WP category
$query_args['category__in'] = $current_category_ids;
}
//}
return $query_args;
}
As there may be some small overhead when implementing this code, you can uncomment the first if(
line, and restrict this to a specific search form / query.
I hope that makes sense & works!
Best
Hi Armando
So I’ve been thinking a bit about this, and obvously at some stage in the future we want to improve the way all this works, from within the plugin.
Let me just say, you can’t alter the query in the way you want using sf_edit_query_args
– this is because we actually perform our search outside of the WP_Query, and insert the results… anyway, for now I see you have only two options going forward:
1) Assign a default value (lets say 0
) to all posts that don’t have a value, and make sure the range slider starts at 0. This is a lot easier to do than it may seem. You run a new WP_Query
to find all posts that don’t have this value assigned (NOT EXISTS
) – for example you can see here:
https://wordpress.stackexchange.com/questions/80303/query-all-posts-where-a-meta-key-does-not-exist
Then when you look through each results, all you do is use add_post_meta()
to set it to 0
– https://developer.wordpress.org/reference/functions/update_post_meta/
2) As you mentioned at the beginning, you wanted the results on page load, to match those after a search has been performed once the default range slider values are added. You can in fact do this, by going to the post meta
tab in your search form, and adding two conditions in, to ensure all posts are above and below a specific values.
I hope that explains things a bit and gives you some viable options to work with!
Let me know if you have any follow up questions.
Thanks
Hi again
So I’ve had a chance to digest this ticket.
In regards to your two issues which are both part of the same problem:
The way a form submits / resets is to include the sliders at their min / max value. We will try to address this in v3 within the plugin, but for now, if you want to have the same set of results when you reset as when you land on the page you have 2 options:
a) set a default value on all other posts – you can write a query to loop through all posts where there is no value for this post meta, and then auto add a value like 0
b) restict the meta query to the slider min/max params (you would have to use fixed min/max limits), you can do that from the post meta
tab in your search form – this way the results that are shown when landing on the page, are teh same as when the form is reset / submitted, and the range slider params are added to the URL.
—
In regards to the filter: sf_edit_query_args
, this is run before S&F applies its workings… S&F does a different kind of query, outside of your WP_Query, which is then combined with the WP_Query, so you can never truly unset that value. Your best bet would be to simply do unset($_GET['filter_name']);
(you can dump this in the bottom of your functions.php to test) before S&F grabs the value and applies its sorting.
I hope that clears a few things up for you?
Let me know if you have any further questions.
Thanks