-
AuthorSearch Results
-
May 10, 2019 at 8:04 am #210688
AnonymousInactiveThank you. So stick this in the theme functions, set to the correct form ID and add the same args per my wp_query. So I have ended up with:
function filter_function_name( $query_args, $sfid ) { //if search form ID = 225, the do something with this query if($sfid == 4672) { //modify $query_args here before returning it $query_args['numberposts'] = -1; $query_args['posts_per_page'] = 25; $query_args['post_type'] = 'product'; $query_args['post_status'] = 'publish'; $query_args['meta_key'] = 'special_offer'; $query_args['meta_value'] = true; } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
I have left the rest of the settings as per my last message (including the wp_query on the special_offers template where I am displaying the products). When I view the page the filters are not populating as expected, I should be seeing options for colour and size but I just see one colour option, which when I apply renders no results.
Am I doing something else wrong here?
April 4, 2019 at 11:50 pm #207594In reply to: Including taxonomies
AnonymousInactiveThanks Trevor.
I managed to get it working with this:
function filter_aus_library ( $query_args, $sfid ) { //if search form ID = 225, the do something with this query if($sfid==14334) { //modify $query_args here before returning it $query_args['tax_query'] = array( 'relation' => 'OR', array( 'taxonomy' => 'library_country', 'terms' => '1497', ), array( 'taxonomy' => 'library_author', 'terms' => '2192', ) ); } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_aus_library', 20, 2 );
April 4, 2019 at 1:00 pm #207512In reply to: Changing the Deafult order
AnonymousInactiveOk,
I added the next code to function.php
function filter_function_name( $query_args, $sfid ) { //if search form ID = 225, the do something with this query if($sfid==6178) { //modify $query_args here before returning it $query_args['orderby'] = 'post_views'; } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 10, 2 );
But now if I use the filter it is only showing 2 downloads:
https://www.dl-sounds.com/member/downloads/?sort_order=name+asc&_sfm_bpm=0+1000without the filter it is working:
https://www.dl-sounds.com/member/downloadsI must be doing something wrong?
April 2, 2019 at 3:14 am #207161In reply to: No Results – DateTime Range
AnonymousInactiveYes, and the element was removed, but it had no effect on the query. It still processed it as if the element was in the query.
So I had to work around it by unsetting the element that was set in the $_GET variable. It works for now, but I was sure it would have been straight forward using the sf_edit_query_args function.
April 1, 2019 at 3:19 pm #207085In reply to: Query vars and results order don't match
AnonymousInactiveI commented out the function that used the
sf_edit_query_args
hook. Then I added the query set to my existingmodify_search_results_order
function that’s usingpre_get_posts
after all the query args I already set. For some reason it caused a fatal error regarding memory size. I thought maybe it was the syntax, so I changed it to$query->query_vars['search_filter_id'] = 493580;
and that seemed to cause endless looping with constant undefined index errors.Am I applying it correctly?
On my WP admin settings, I have the “Display Results” tab set where the method is “As an archive” and the template options are using a custom template, search.php. No slug set and no ajax.
Thanks.
March 7, 2019 at 4:15 pm #204275In reply to: Set Default Radio box checked to different category
RossKeymasterOk, so what we did was simply update form field “defaults” but we didn’t update the query as well.
So what we need to do is, under the same conditions, restrict the query to that taxonomy term:
function filter_query_categories( $query_args, $sfid ) { //if search form ID = 252649, the do something with this query if($sfid==252649) { //if the marketplace taxonomy is not already in use / selected if(!isset($_GET['_sft_marketplace'])){ //modify $query_args here before returning it $query_args['tax_query'] = array( 'relation' => 'AND', array( 'taxonomy' => 'marketplace', 'field' => 'slug', 'terms' => array("best-wordpress-app-themes") //restrict it ) ); } } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_query_categories', 20, 2 );
That should just about do it.
Let me know how you get on.
Thanks
January 29, 2019 at 11:28 am #200431In reply to: Include AND exclude category
TrevorParticipantAh, try this type of method then:
function include_exclude_search_filter( $query_args, $sfid ) { //if search form ID = 135, the do something with this query if($sfid==135) { //modify $query_args here before returning it $query_args['tax_query'] = array( 'relation' => 'AND', array( 'taxonomy' => 'question_category', 'field' => 'term_id', 'terms' => array( 254 ), 'operator' => 'IN', ), array( 'taxonomy' => 'question_category', 'field' => 'term_id', 'terms' => array( 253 ), 'operator' => 'NOT IN', ) ); } elseif($sfid==1234) { more stuff } return $query_args; } add_filter( 'sf_edit_query_args', 'include_exclude_search_filter', 20, 2 );
January 28, 2019 at 8:06 pm #200325In reply to: Include AND exclude category
RossKeymasterHi Jeroen
Thanks for your patience, I finally understood the use case!
I had a little think about this, my first issue was I thought a
WP_Query
couldn’t do this, because how would it know which rule takes precedent..?Anyway, turns out it is possible, which mean you can use our filter (
sf_edit_query_args
) to do this programmatically. This worked for me locally (you’ll have to replace some variables)://include and exclude a tax / category function include_exclude_search_filter( $query_args, $sfid ) { //if search form ID = 135, the do something with this query if($sfid==135) { //modify $query_args here before returning it $query_args['tax_query'] = array( 'relation' => 'AND', array( 'taxonomy' => 'question_category', 'field' => 'term_id', 'terms' => array( 254 ), 'operator' => 'IN', ), array( 'taxonomy' => 'question_category', 'field' => 'term_id', 'terms' => array( 253 ), 'operator' => 'NOT IN', ) ); } return $query_args; } add_filter( 'sf_edit_query_args', 'include_exclude_search_filter', 20, 2 );
Make sure to add this to your
functions.php
…Things that need to be replaced in the above code:
1) at the top you need to replace the search form ID with your own, so replace135
with17289
2) The taxonomy name I was using wasquestion_category
, replace this with the name of your taxonomy
3) The part that has theIN
condition, is the equivalent ofinclude
–
'terms' => array( 254 ),
So change 254 for the ID you want to include.
4) The next part is the exclude:
'terms' => array( 253 ),
So change 253 for the ID of the category you want to exclude.If you need to know the IDs of your categories/taxonomies, just go to the edit page and check the URL for the ID.
And thats it! You should be good to go, let me know how you get on.
Thanks
November 30, 2018 at 2:50 pm #195017Topic: sf_edit_query_args not working with post__in
in forum Search & Filter Pro
AnonymousInactiveI’m having problems filtering sf_edit_query_args with the post__in query arg. Here’s a quick prototype that I have:
add_filter( 'sf_edit_query_args', function( $query_args, $sfid ) { if ( 20205 === $sfid ) { $query_args['post__in'] = array( 20696, 20694, 20612 ); } return $query_args; }, 20, 2 );
I double-checked that 20205 is the id of the search&filter form and that 20696, 20694, 20612 are actual custom post type ids. The form works as it should with the usual search&filter search field, custom post type filters, tag dropdowns, etc. But I’m not able to filter it programmatically with the provided post ids (however, it works correctly with a provided custom post type –
$query_args['post_type'] = 'custom';
). It simply returns no results. Is the post__in query arg not supported?Thanks!
October 18, 2018 at 6:49 pm #191286In reply to: modify $query
AnonymousInactiveNow I thought about the problem again and looked at the documentation. I found the following: “Edit Query Arguments”.
Then I tried the following:
function filter_function_name( $query_args, $sfid ) { if($sfid==163) { $query_args['tax_query'] = array( 'relation' => 'OR', array( 'taxonomy' => 'region', 'field' => 'slug', 'terms' => array( 'Österreich' ), ), ); } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
Unfortunately this works exactly the other way round to what I want.
Now only articles with the tag Austria are displayed.Is there any way to modify it to work the way I think it should?
-
AuthorSearch Results
-
Search Results
-
I’m having problems filtering sf_edit_query_args with the post__in query arg. Here’s a quick prototype that I have:
add_filter( 'sf_edit_query_args', function( $query_args, $sfid ) { if ( 20205 === $sfid ) { $query_args['post__in'] = array( 20696, 20694, 20612 ); } return $query_args; }, 20, 2 );
I double-checked that 20205 is the id of the search&filter form and that 20696, 20694, 20612 are actual custom post type ids. The form works as it should with the usual search&filter search field, custom post type filters, tag dropdowns, etc. But I’m not able to filter it programmatically with the provided post ids (however, it works correctly with a provided custom post type –
$query_args['post_type'] = 'custom';
). It simply returns no results. Is the post__in query arg not supported?Thanks!