Forums › Forums › Search & Filter Pro › Adding custom functions to S&F filtering
Tagged: custom function, Enfold
- This topic has 8 replies, 3 voices, and was last updated 4 years ago by Ross.
-
Anonymous(Private) December 16, 2020 at 11:02 am #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(Private) December 16, 2020 at 11:41 am #269961I 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(Private) December 16, 2020 at 12:12 pm #269971Thanks 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?
Anonymous(Private) December 16, 2020 at 6:31 pm #270052Sorry 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
Ross Moderator(Private) December 21, 2020 at 2:08 pm #270521This reply has been marked as private. -
AuthorPosts