AnonymousInactive
Hello there,
I see a new “sf_edit_query_args” filter is available since S&F v2.0. Does this mean I can pass my ?display= parameter in the URL with the plugin now?
Thank you.
AnonymousInactive
Hey,
So I attempted to add additional arguments and tested it with my original search form/results page and received this error – Warning: trim() expects parameter 1 to be string, array given in /home/andreymh/public_html/v2/wp-includes/class-wp-meta-query.php on line 577
This is the argument code I put in functions.php –
function filter_function_name( $query_args, $sfid ) {
//if search form ID = 225, the do something with this query
if($sfid==12)
{
//modify $query_args here before returning it
$query_args = array(
‘meta_key’ => ‘property_type’,
‘meta_compare’ => ‘LIKE’,
‘meta_value’ => array(
‘modular_with_land’,
‘manufactured_with_land’,
‘mobile_with_land’
)
);
}
return $query_args;
}
add_filter( ‘sf_edit_query_args’, ‘filter_function_name’, 20, 2 );
In this case 12 is the ID of the main search page form, not the curated page I want this to actually be applied to (which I’m not sure how that will work since that page only has a results shortcode).
As for the infinite scroll, yes. I created a search form for the main search page and then duplicated it to create the curated pages. On those forms, I deleted all the fields from the form UI per your suggestion, changed what URL the results get displayed on, and changed the post meta filters.
AnonymousInactive
Trevor, I appreciate the help so far. This is the filter I tried in our functions.php file:
function filter_function_name( $query_args, $sfid ) {
//if search form ID = 35922, the do something with this query
if($sfid==35922)
{
//modify $query_args here before returning it
$query_args['post_mime_type'] = 'application/pdf';
}
return $query_args;
}
add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
I got the ID by looking at the query string in the URL on the edit page, as well as verifying it through data-sf-form-id="35922"
on the form itself using Chrome inspector.
For our search page, we’re including posts, pages, and media (trying to only include PDFs from media). After I added this to functions.php, it actually stopped ALL search items from loading.
Any ideas?
AnonymousInactive
So this is the code block I’d be using?
function filter_function_name( $query_args, $sfid ) {
//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 );
Can you give me any information on how I’d apply your suggestion to this filter? Sorry, I’m not much of a coder.
Hi Pablo
Apologies for the delay coming back.
Our filter, sf_edit_query_args
is meant especially for this reason, so your changes are reflected across our fields.
I just did a quick test, with a different meta query (using price, on my shop) and it worked exactly as expected, with all the fields updating their count numbers:
add_filter( 'sf_edit_query_args', 'kp_exclude_sold_older_than_two_months', 100, 2 );
function kp_exclude_sold_older_than_two_months( $query ) {
$query['meta_query'] = array(
array(
'key' => '_price',
'value' => 11,
'compare' => '>=',
'type' => 'NUMERIC'
)
);
return $query;
}
The only thing is, some fields, such as post type, don’t have a dynamic count, so would remain unaffected regardless.
Could this be the issue you are seeing?
Not easily no. Our date range system only has equals as the operator. v3 of the plugin will have greater than and less than added.
It IS possible to modify the query sent to the database by using one of our filters, as here:
https://searchandfilter.com/documentation/action-filter-reference/
The Edit Query Argument filter. It would need to be coded though by yourself or a third party coder. There may be snippets other users have posted in these forums, if you search for sf_edit_query_args
AnonymousInactive
We are in the need to customize a bit more the criteria for fetching using dynamic information during the searches.
We are looking for altering the query posts arguments to exclude some posts from the search.
But basically the process of selection of those posts are dynamic and we can’t built it using the UI.
Example:
Exclude post which custom meta field ‘ListDate’ is older than two months.
The following code kind of works but leaves all complex filtering counts unaltered in the dropdown filters we have.
Is there a way to make the dropdown filters created to be aware of this update?
add_filter( 'sf_edit_query_args', 'kp_exclude_sold_older_than_two_months', 100, 2 );
function kp_exclude_sold_older_than_two_months( $query ) {
$time_ago = date('Y-m-d', strtotime('-2 months'));
$query['meta_query'] = array(
array(
'key' => 'ListDate',
'value' => $time_ago,
'compare' => '>=',
'type' => 'DATE'
)
);
return $query;
}
Thanks in advance.
Pablo.
AnonymousInactive
Hi,
thank you Ross for your answer. The project was kind of on hold for a while, so I’m only replying now.
I’ve played a bit with sf_edit_query_args
and got it working.
So my setting was as follows:
I have a home.php template set up, containing:
<?php echo do_shortcode('[searchandfilter id="' . BLOG_FILTER . '"]'); ?>
<?php echo do_shortcode('[searchandfilter id="' . BLOG_FILTER . '" show="results"]'); ?>
I also have a page named Blog that is set up as the Posts Page on /wp-admin/options-reading.php
In backend I have a filter whose Display settings are:
Display results method: Using a Shortcode
Results URL: http://website.com/blog
Load results using Ajax? Checked
Make searches bookmarkable? Checked
I then have a filter (usually put in functions.php):
/**
* Modify Search & Filter PRO plugin's query so that it returns posts of authors that are not anonymous.
*/
function domain_sf_filter_query_args($query_args, $sfid)
{
if ($sfid == 95 && isset($_GET['authors'])) {
$query_args['meta_key'] = 'anonimni_avtor';
$query_args['meta_value'] = 1;
$query_args['meta_compare'] = '!=';
}
return $query_args;
}
add_filter('sf_edit_query_args', 'domain_sf_filter_query_args', 10, 2);
So, when visiting an unfiltered page it returns all posts, but when filtering through authors, it returns only posts from that author that are not marked as anonymous author.
Thank you all for your help 🙂
Have a great start of next week!
Domen
AnonymousInactive
Getting the plugin to use the text field to search in an ACF repeater field is turning out to be a real challenge.
Right now I have a search form set up with an regular search text field and three taxonomy select lists. It works well finding results based on standard WP fields like title or content, but I can’t figure out if there is a way to get it to search through an ACF repeater field.
In the posts, I have an ACF repeater field called “Authors/Speakers”, which allows you to choose posts from another post type called “person”. The person post type is essentially our list of employees, so this allows us to choose multiple people on any given post, who are then displayed as authors or speakers.
The idea is for when someone searches for “Joe Smith”, then the results would include posts where Joe Smith was selected in the Authors/Speakers field. Unfortunately the plugin only searches through the non-ACF fields like title or content, so it never finds posts with Joe Smith.
I have tried using the ‘sf_edit_query_args’ filter, but I can’t enter the correct query args at this point in the query. I think what would have to happen is I need to do two queries: one is whatever the plugin queries, and another manual WP_Query to search for the author/speakers. Then merge the resulting post arrays. But then the problem is that I can’t figure out how to get the plugin’s result array.
How do I get the plugin to use the search field to also search through an ACF repeater field?
Hi Domen
It looks like the problem is your usage of sf_edit_query_args
The reason for the filter, is so that it works effectively for all our queries and ajax.
One of the issues I see with your code is actually with this line:
if (is_author()) {
A typical ajax request will be performed at wp-admin/admin-ajax.php
, the function is_author
will always return false, this is applicable when using our filter sf_edit_query_args
and your display method is set to shortcode
I added an extra line in our example to show how to set query arg properties using our function – https://www.designsandcode.com/documentation/search-filter-pro/action-filter-reference/#Edit_Query_Arguments
If you wanted is_author
to work correctly, you would need to use the display method archive
where our ajax requests are loaded from the same page.
Hope that makes sense
Thanks