Forums Forums Search & Filter Pro Adding custom functions to S&F filtering

Viewing 9 posts - 1 through 9 (of 9 total)
  • Anonymous
    #269955

    Hi, I have a specific problem with my search page. I use Enfold as main theme. I created two different pages showing custom post types in two different ways. In one of the page I added the S&F.

    Posts have custom fields (ACF): one of these ACF is a number corresponding to the year. I had to show in one page only posts that have the “year” ACF older than NO MORE than 2 years from a timestamp; the other page shows posts that are older than >2 years from the timestamp. I created a custom function (avia_post_slide_query_mod) to do that and added as filter to the blog post enfold element. Here is the function:

    add_filter('avia_post_slide_query', 'avia_post_slide_query_mod',10, 2);
    function avia_post_slide_query_mod($query, $params) {
    	$posts = get_posts($query);
    	$today_year = date('yy');
    	$two_years_ago = $today_year - 2;
    	$current = array();
    
    		foreach( $posts as $post ) {
    			$meta = get_post_meta($post->ID);
    			
    			
    			if (is_page(112)){
    				$acf_year = $meta['anno'][0];
    				if($acf_year >= $two_years_ago & $acf_year <= $today_year) {
    				$current[] = $post->ID;
    				}	
    
    			}
    			else if (is_page(204))	{
    				$acf_year = $meta['anno'][0];
    				if($acf_year < $two_years_ago) {
    				$current[] = $post->ID;
    				}	
    			}
    			else if (is_archive()) {
    				return $query;
    			}
    		}
    
    	$query['post__in'] = $current;
    
    	return $query;
    }

    Everything works fine. However the S&F that I created doesn’t work well in this case. It is visible in the page 204. It should filter and sort on the following parameters: post title / year (acf field) / location (acf field). Actually the dropdown proposes all the possible years (or locations) present in the database. Hence the query works well only for those acf field values that are displayed in the page based on the filtering function showed above. If in case the user selects on the of the values that shouldn’t be shown on the page, then S&F shows everything messed up.

    So my question is: how can I add a specific filter to the S&F form, that includes the function above? So that I can force the form to search only among years and locations that are currently displayed in the page?

    Trevor
    #269961

    I think what you want is the filter function that allows you to do the same to our query, which is this:

    https://searchandfilter.com/documentation/action-filter-reference/#edit-query-arguments

    Anonymous
    #269971

    Thanks maybe that the thing, but I am not very expert and I don’t know how to use it.

    If I modified the function you suggest and I var_dump the results, I find exactly the list of post I should have, but still the filter doesn’t work

    function filter_function_name( $query_args, $sfid ) {
    	
    	//if search form ID = 225, the do something with this query
    	if($sfid==225)
    	{
    	$posts = get_posts($query_args);
    	$today_year = date('yy');
    	$two_years_ago = $today_year - 2;
    	$current = array();
    
    		foreach( $posts as $post ) {
    			$meta = get_post_meta($post->ID);
    
    				$acf_year = $meta['anno'][0];
    				if($acf_year < $two_years_ago) {
    				$current[] = $post->ID;
    
    		}
    	}
    	$query_args = $current;
    	}
    	
    	return $query_args;
    }
    add_filter( 'sf_edit_query_args', 'filter_function_name', 20, 2 );

    Could you help through it?

    Trevor
    #270011

    What is the form/post ID of your form?

    Anonymous
    #270013
    This reply has been marked as private.
    Trevor
    #270043

    I see that the form ID is 243 on that page, but your code uses 225?? Try changing those instances in the code.

    Anonymous
    #270052

    Sorry I already did it, but forgot to change it in the message to you. Still it doesn’t work.

    There is an alternative solution, which actually I do not like too much (I tested it on my local website). I display the results in a the custom post type archive page -> that is not affected by the function avia_post_slide_query_mod, thus results are fine. To avoid showing wrong “year” posts I added a specific condition to the S&F form setup under the post meta tab. However this is not a good solution, as the condition should be manually reset every year (there is not a way to automatically define it on the timestamp year). Also, I do not like too much results being displayed in another page.

    Hence, I would like to understand how can I force the S&F ajax call on the form taking in consideration that the search and filtering must be done only on displayed posts that are under the condition acf_field year < 2 years from timestamp year

    thanks

    Trevor
    #270109
    This reply has been marked as private.
    Ross Moderator
    #270521
    This reply has been marked as private.
Viewing 9 posts - 1 through 9 (of 9 total)