Forums › Forums › Search & Filter Pro › Form on a single-post
- This topic has 3 replies, 2 voices, and was last updated 4 years, 5 months ago by Anonymous.
-
Anonymous(Private) July 22, 2020 at 1:50 pm #253570
Hi,
Is it possible to use an S&F form one a single-post page? I want to show results of another post-type, related to the post that’s currently being visited.
To be more specific, I have a post-type for ‘locations’. I want to show all ‘events’ (the other post-type) that are being held at that location. Can it be done?
Ross Moderator(Private) July 22, 2020 at 3:20 pm #253573Hi Kevin, you should be able to do this, but would need to use some PHP filters:
Essentially, you would need to modify the query to show only events from the current page location – you can do that using this filter:
https://searchandfilter.com/documentation/action-filter-reference/#edit-query-argumentsThanks
Anonymous(Private) July 23, 2020 at 1:32 pm #253659I’ve solved it with some workarounds. I will post my solution for other people:
Form settings:
– configured the form display method as ‘custom’;
– added a results url of a random page (it needs to be filled);
– enabled Ajax loading and configured a div as Ajax container;
– enabled ‘make searches bookmarkable’;
– enabled Ajax pagination.I added a filter in functions.php, so the results url for this specific form will always be overwritten with the current permalink:
function filter_function_name( $url, $sfid) { if (get_post_field('post_name', $sfid) == 'agenda-shadow-single-post') { $url = get_permalink(); } return $url; } add_filter( 'sf_results_url', 'filter_function_name', 10, 2 );
I implemented the form in template files as following:
<?php // retrieving the the form by using the slug $sf_form_obj = get_page_by_path('agenda-shadow-single-post', OBJECT, 'search-filter-widget'); if ($sf_form_obj) { $args['search_filter_id'] = $sf_form_obj->ID; $query = new WP_Query($args); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); get_template_part('template-parts/blocks/block-activiteit'); } } else { ?> <div class='search-filter-results-list' data-search-filter-action='infinite-scroll-end'></div> <?php } } ?>
I tried to alter the query args with the
sf_edit_query_args
filter, but it didn’t work for me. So I wrote some JavaScript to modify the value of the filter of the post-type that’s currently being viewed. After that I’ve hidden the filter, so it won’t be changed by the user. I did this with a function that’s being triggered on these ajax events:jQuery(document).on("sf:ajaxstart sf:init sf:ajaxfinish", ".searchandfilter", function () { fillFilterOnLocation(); });
-
AuthorPosts