Preselecting taxonomy?

Forums › Forums › Search & Filter Pro › Preselecting taxonomy?

Viewing 5 posts – 1 through 5 (of 5 total)
  • Anonymous
    #57005

    Hi there,

    The plugin works great as a default blog page.

    What we would also like is to also use it on other pages to display posts with a certain term from a custom taxonomy.

    Is it possible to preselect a term, so that only posts from that term are displayed by default but the visitor still has the option to choose other terms and expand the search?

    These pages exist and have the taxonomy id set as a custom field (using ACF) so a solution using this would be perfect.

    Anonymous
    #57011

    Ok, so I managed selecting the checkbox with some jQuery added via functions.php:

    //Select sector in posts filter
    add_action('wp_footer', 'sectorpostsfilter');
    function sectorpostsfilter(){
    	if (function_exists('get_field')) :
    		$sectorobject = get_field('taxonomy_sector');
    		$sectorid = $sectorobject->term_id;
    		if ($sectorid) : ?>
    		<script type='text/javascript'>
    			jQuery = jQuery.noConflict();
    			jQuery(function( $ ) {
    				$('.sf-item-<?php echo $sectorid; ?> input').prop('checked', true); 
    			});
    		</script><?php 
    		endif;
    	endif;
    }

    And by altering the query within the template:

    <?php 
    if (function_exists('get_field')) :
    	$sectorobject = get_field('taxonomy_sector');
    	$sectorslug = $sectorobject->slug;
    endif;
    $loop = new WP_Query( array( 'sectors' => $sectorslug ) ); 
            while ( $loop->have_posts() ) : $loop->the_post();
    			get_template_part( 'loop', 'post' );	
    	endwhile; wp_reset_postdata(); 
    ?>
    Trevor
    #57021

    This works for you. Kudos! Can I close the thread as self-resolved?

    Anonymous
    #57348

    Hi Trevor, would S&F do this out of the box or do we need to follow Samantha’s advice?

    Trevor
    #57362

    You would need to follow Samantha’s advice, as it cannot do it out of the box.

Viewing 5 posts – 1 through 5 (of 5 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.