-
AuthorSearch Results
-
September 12, 2018 at 2:38 pm #188232
Topic: sf_filter_query_args to hide option from dropdown
in forum Search & Filter Pro
AnonymousInactiveI’m using the code bellow to hide posts that are older than the current date. It’s working on the default results but it’s not working on the filter(dropdown).
Is there a way to exclude old posts from showing on the filter(Search Form UI) too?
Thanks
add_filter( ‘sf_edit_query_args’, ‘kp_exclude_old_post’, 100, 2 );
function kp_exclude_old_post( $query ) {$time_ago = date(‘Y-m-d’, strtotime(‘-1 day’));
$query[‘meta_query’] = array(
array(
‘key’ => ‘acfDia’,
‘value’ => $time_ago,
‘compare’ => ‘>=’,
‘type’ => ‘DATE’
)
);return $query;
}September 12, 2018 at 12:49 pm #188189In reply to: Duplicate posts in infinity scroll
AnonymousInactiveHi Trevor,
We are getting close 🙂
It’s important that they are properly ranked. That’s why I used following hook to change the order:
/* Change query Search and filter pro */ function filter_function_name( $query_args, $sfid ) { if($sfid==1710) { //modify $query_args here before returning it $query_args['meta_key'] = 'prioriteit_mandaat'; $query_args['orderby'] = 'meta_value_num'; $query_args['order'] = 'ASC'; } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
August 8, 2018 at 9:02 am #184825In reply to: Injecting custom dropdowns to the filter bar
AnonymousInactiveFair enough!
Should anyone else have the same problem: I’ve managed to find a hacky but reasonable solution for now: when parsing the drop-down contents (like turning “last_quarter” into a start and an end date) I inject some JavaScript into the document that sets the dropdown to the right value.
if (isset($_GET["_sfm_pseudofield_daterange"])) { $daterange = $_GET["_sfm_pseudofield_daterange"]; .... Handle the date stuff // Now remove the GET parameter so it doesn't get used as a filter and lead to 0 results unset($_GET["_sfm_pseudofield_daterange"]); // Use JavaScript to correctly set the dropdown again even though GET parameter was unset add_action( 'wp_head', function() use($daterange) { ?><script>jQuery(document).ready(function(){ jQuery("[name='_sfm_pseudofield_daterange[]']").val("<?= htmlentities($daterange);?>");});</script><? } ); }
As a feature request, it would be awesome to have a new “empty” field type (Text field, dropdown, check box, etc.) that gets passed along with the rest of the form, plus a documented interface for altering query parameters in the
sf_edit_query_args
hook.August 1, 2018 at 8:48 pm #184347In reply to: Exclude current post from results
RossKeymasterHi Dan
There appears to be a small typo in the code Trevor supplied,
post__not_in
needs to be an array –function filter_post__not_in( $query_args, $sfid ) { //if search form ID = 264, the do something with this query if($sfid==264) { global $post; $postid = $post->ID ; $query_args['post__not_in'] = array($postid); } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_post__not_in', 20, 2 );
Thanks
July 30, 2018 at 5:35 pm #183979In reply to: Exclude current post from results
TrevorParticipantNot if you have already specified that in the search form setup. You might have to do this:
function filter_function_name( $query_args, $sfid ) { //if search form ID = 264, the do something with this query if($sfid==264) { //modify $query_args here before returning it // $query_args['somearg'] = 'newvalue'; global $post; $postid = $post->ID ; $query_args['post__not_in'] = $postid } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
July 30, 2018 at 3:04 pm #183922In reply to: Exclude current post from results
TrevorParticipantHi
Would it not be:
function filter_function_name( $query_args, $sfid ) { //if search form ID = 264, the do something with this query if($sfid==264) { //modify $query_args here before returning it // $query_args['somearg'] = 'newvalue'; $postid = get_the_ID(); $query_args['post__not_in'] = $postid } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
July 30, 2018 at 1:27 pm #183892In reply to: Exclude current post from results
AnonymousInactiveHi,
Below is the code snippet I have used but it doesn’t seem to be working. I get ‘No results found’. Can you see anything obviously wrong with it?
function filter_function_name( $query_args, $sfid ) { //if search form ID = 264, the do something with this query if($sfid==264) { //modify $query_args here before returning it // $query_args['somearg'] = 'newvalue'; $postid = get_the_ID(); $query_args = array( 'post_type' => 'artist', 'post__not_in' => $postid, ); } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
July 26, 2018 at 6:52 am #183569In reply to: Filter events by upcoming
AnonymousInactiveThank you both so much for your help, I really appreciate it!
I would like for all of the events to show as normal, and have all the keyword, category etc filters work as normal, I just want to change the behaviour of the date filter.
Once a date is selected I would like the results to only include events that occur on that date (which it does already), but to also show events that occur after that date.
I’ve updated my previous query to this which I think should work, except that it seems the date filter’s original programming is overriding my meta query.
function upcoming_events( $query_args, $sfid ) { if ( $sfid == 72 ) { if(isset($_GET['_sfm_start_date'])){ $dateField = $_GET['_sfm_start_date']; $query_args['meta_query'] = array( array( 'key' => 'start_date', 'value' => $dateField, 'compare' => '>=', 'type' => 'NUMERIC' ) ); } } return $query_args; } add_filter( 'sf_edit_query_args', 'upcoming_events', 20, 2 );
By using the print_r($query_args); I am able to see that the query does run when a date is entered, except it seems to be ignored as it doesn’t have any effect on the results. The results still only show events that occur on the exact date which is entered.
How do I go about overriding the date filters current programming?
July 24, 2018 at 7:24 am #183232In reply to: Filter events by upcoming
AnonymousInactiveOk, awesome! I took a stab at it today and I’ve written this which pretty much works backwards. It removes all the past events on page load and maintains that when using the keywords and category filter. Then when I enter in a date it reverts to showing just the events that happen on that date, nothing upcoming. Would you be able to help me alter it to achieve what I need? I’m also not sure how to pull the users input from the form, or how to check a specific field if it has been utilized. I apologize if I’ve gone completely down the wrong track!
function upcoming_events( $query_args, $sfid ) { if ( $sfid == 72 ) { $dateField = ''; //get users input, if it exists if ( $dateField != '' ) { $query_args['meta_query'] = array( array( 'key' => 'start_date', 'value' => $dateField, 'compare' => '>=', 'type' => 'NUMERIC' ) ); } } return $query_args; } add_filter( 'sf_edit_query_args', 'upcoming_events', 20, 2 );
July 20, 2018 at 5:10 pm #183103In reply to: Show child posts only
AnonymousInactiveThank you! Here’s what worked for me, in case anyone needs it:
function filter_function_name( $query_args, $sfid ) { if($sfid==22) { $query_args['post_parent__not_in'] = array(0); } return $query_args; } add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );
-
AuthorSearch Results
-
Search Results
-
I’m using the code bellow to hide posts that are older than the current date. It’s working on the default results but it’s not working on the filter(dropdown).
Is there a way to exclude old posts from showing on the filter(Search Form UI) too?
Thanks
add_filter( ‘sf_edit_query_args’, ‘kp_exclude_old_post’, 100, 2 );
function kp_exclude_old_post( $query ) {$time_ago = date(‘Y-m-d’, strtotime(‘-1 day’));
$query[‘meta_query’] = array(
array(
‘key’ => ‘acfDia’,
‘value’ => $time_ago,
‘compare’ => ‘>=’,
‘type’ => ‘DATE’
)
);return $query;
}