AnonymousInactive
function filter_function_name( $query_args, $sfid ) {
//if search form ID = 19856, the do something with this query
if($sfid== 19856)
{
//modify $query_args here before returning it
// args
$args = array(
‘post_type’ => ‘shares’,
‘meta_query’ => array(
‘relation’ => ‘OR’,
array(
‘key’ => ‘type’,
‘value’ => ‘%v86%’,
‘compare’ => ‘LIKE’
),
array(
‘key’ => ‘type’,
‘value’ => ‘%gs75%’,
‘compare’ => ‘LIKE’
)
)
);
}
return $query_args;
}
add_filter( ‘sf_edit_query_args’, ‘filter_function_name’, 20, 2 );
AnonymousInactive
Hello,
So I have 2 S&F forms on my pages (different results pages). Is it possible to pass the query from my main text search to a sidebar filters S&F form I have? The filters on the left live on results page from the main search bar, and if you try to use them they reset the query from the text search (which makes sense technically). I am wondering if there is a way that if a user hits ones of those filters I can pass the query from the other S&F form?
I saw this:
function filter_function_name( $query_args, $sfid ) {
if ( is_search() ) {
//var_dump($sf_current_query);
}
//if search form ID = 225, the do something with this query
if($sfid==225)
{
//modify $query_args here before returning it
$query_args[‘somearg’] = ‘newvalue’;
}
return $query_args;
}
add_filter( ‘sf_edit_query_args’, ‘filter_function_name’, 20, 2 );
But wasn’t sure if that was the right path to grab the current query and pass it along to the other form.
Thank you!
AnonymousInactive
I’ve solved it with some workarounds. I will post my solution for other people:
Form settings:
– configured the form display method as ‘custom’;
– added a results url of a random page (it needs to be filled);
– enabled Ajax loading and configured a div as Ajax container;
– enabled ‘make searches bookmarkable’;
– enabled Ajax pagination.
I added a filter in functions.php, so the results url for this specific form will always be overwritten with the current permalink:
function filter_function_name( $url, $sfid) {
if (get_post_field('post_name', $sfid) == 'agenda-shadow-single-post') {
$url = get_permalink();
}
return $url;
}
add_filter( 'sf_results_url', 'filter_function_name', 10, 2 );
I implemented the form in template files as following:
<?php
// retrieving the the form by using the slug
$sf_form_obj = get_page_by_path('agenda-shadow-single-post', OBJECT, 'search-filter-widget');
if ($sf_form_obj) {
$args['search_filter_id'] = $sf_form_obj->ID;
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
get_template_part('template-parts/blocks/block-activiteit');
}
} else {
?>
<div class='search-filter-results-list' data-search-filter-action='infinite-scroll-end'></div>
<?php
}
}
?>
I tried to alter the query args with the sf_edit_query_args filter, but it didn’t work for me. So I wrote some JavaScript to modify the value of the filter of the post-type that’s currently being viewed. After that I’ve hidden the filter, so it won’t be changed by the user. I did this with a function that’s being triggered on these ajax events:
jQuery(document).on("sf:ajaxstart sf:init sf:ajaxfinish", ".searchandfilter", function () {
fillFilterOnLocation();
});
AnonymousInactive
Hello!
From what I’ve seen in other forum posts, geolocation search functionality is not yet available.
We are planning to implement it ourselves, but I would like you to confirm if I could do it with the filters available in your plugin.
First of all, you would need to include a new field in the form, with a Km distance selector. With which filter could I do it? I have not seen this specifically in the documentation
Secondly, for searches I have seen that I could use a plugin like this: https://gschoppe.com/wordpress/location-searches/ that extends the WP Query class and then use your sf_edit_query_args filter to add the filter for X km away from the user’s location. Have you done any tests with this type of systems? Would it work well in conjunction with your plugin?
I await your response, thank you very much!
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
Howdy,
This was originally going to be a support question, but I ended up finding a way to make it work for me. if anything, I’d love feedback on whether or not you had a better method. Otherwise, I’m dropping my code snippet here for other users to find.
Woocommerce has a custom theme support for theme developers that allows for specifying the number of rows and columns on a catalog page. https://docs.woocommerce.com/document/woocommerce-theme-developer-handbook/#section-7
In Search & Filter Pro, there is a setting to specify the results per page, which will completely override this customizer setting.
Here is the code snippet I used to get around this:
function prefix_filters_post_count( $query_args, $sfid ) {
// If search form ID = 150, the do something with this query. edit to your form ID.
if ( 150 === $sfid ) {
if ( function_exists( 'wc_get_theme_support' ) ) {
$product_grid = wc_get_theme_support( 'product_grid' );
$rows = isset( $product_grid['default_rows'] ) ? get_option( 'woocommerce_catalog_rows', absint( $product_grid['default_rows'] ) ) : 5; // 5 rows as a fallback
$columns = isset( $product_grid['default_columns'] ) ? get_option( 'woocommerce_catalog_columns', absint( $product_grid['default_columns'] ) ) : 3; // 3 cols as a fallback
$count = max( 1, $rows * $columns ); // return rows*columns, or fallback to 1 in case of error
$query_args['posts_per_page'] = $count;
}
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'prefix_filters_post_count', 20, 2 );
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!