Form on a single-post

Forums › Forums › Search & Filter Pro › Form on a single-post

Viewing 4 posts – 1 through 4 (of 4 total)
  • Anonymous
    #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
    #253573

    Hi 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-arguments

    Thanks

    Anonymous
    #253647

    Hi Ross,

    Thanks for your answer, I tried to work this out but I’m not getting there yet. I think I’m still confused about what display method I should use for showing results on a single-post. Can you inform me about this?

    Anonymous
    #253659

    I’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();
    });
    
Viewing 4 posts – 1 through 4 (of 4 total)
Search & Filter Support
Privacy Overview

This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

We also use cookies to store items in your cart as well as allowing your to login on the site.

You can adjust all of your cookie settings by navigating the tabs on the left hand side.

By continuing to use this site, you also agree to our Privacy Policy.