Forums Forums Search & Filter Pro List Search Results Separated by Custom Taxonomy Term

Viewing 2 posts - 1 through 2 (of 2 total)
  • Anonymous
    #253466

    Hi, I’m trying to display all custom posts separated by custom taxonomy term.
    Format:
    <h2>Term 1</h2>
    {All posts from taxonomy term 1}

    <h2>Term 2</h2>
    {All posts from taxonomy term 2}

    Can you tell me how to incorporate this into the loop in results.php?
    I saw some documentation saying add $args[‘search_filter_id’] = 123 but not sure.
    Thank you! (my loop below)

    $patient_story_types = get_terms('patient_story_type');
    		foreach($patient_story_types as $patient_story_type) :
    		  wp_reset_query();
    		  $patient_story_args = array(
    		  	'post_type' => 'patient_story',
    		  	'posts_per_page' => -1,
    		  	'tax_query' => array(
    		      array(
    		      	'taxonomy' => 'patient_story_type',
    		        'field' => 'slug',
    		        'terms' => $patient_story_type->slug,
    		      ),
    					array(
    		        'taxonomy' => 'patient_story_type',
    		        'terms' => array('featured'),
    		        'field' => 'slug',
    		        'operator' => 'NOT IN',
    					),
    		    ),
    		  );
    		  $custom_query = new WP_Query($patient_story_args);
    		  if($custom_query->have_posts()) : ?>
    		  	
    		  	<h3 class="patient-story-type"><?php echo $patient_story_type->name; ?></h3>
    		  	<div class="patient-stories">
    			  	<?php while($custom_query->have_posts()) : $custom_query->the_post(); ?>
    				
    						<div class="patient-story">
    							<div class="card">
    								<?php if(has_post_thumbnail()) { ?>
    									<a>"><?php the_post_thumbnail('large', array('class'=>'img-responsive aligncenter', 'alt'=>get_the_title(), 'title'=>get_the_title())); ?></a>
    								<?php } ?>
    								<div class="card-body">
    									<?php $patient_story_type_terms = get_the_terms($post->ID, 'patient_story_type'); ?>
    									<h4<?php if($patient_story_type_terms) { echo ' class="has-terms"'; } ?>><a>"><?php the_title(); ?></a></h4>
    									
    									<?php if($patient_story_type_terms) {
    													
    											echo "<ul class='list-inline patient-story-type-terms'>";
    												foreach($patient_story_type_terms as $patient_story_type_term) {
    													echo "<li>". $patient_story_type_term->name ."</li>";
    												}
    											echo "</ul>";
    									} ?>
    									<p><?php pva_custom_excerpt(array('length' => 25, 'suffix' => '&hellip;', 'link_text' => '')); ?></p>
    									<p><a>">Read Full Story</a></p>
    								</div><!-- .card-body -->							
    							</div><!-- .card -->
    						</div><!-- .patient-story -->
    							
    					<?php endwhile; ?>
    		  	</div><!-- .patient-stories -->
    		
    			<?php endif;
    		endforeach; ?>
    Ross Moderator
    #253555

    Hi Jerry

    You are correct, so what you would probably want to do is add it to the query args in here:

    $patient_story_args = array(
    	'post_type' => 'patient_story',
    	'posts_per_page' => -1,
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'patient_story_type',
    			'field' => 'slug',
    			'terms' => $patient_story_type->slug,
    		),
    		array(
    			'taxonomy' => 'patient_story_type',
    			'terms' => array('featured'),
    			'field' => 'slug',
    			'operator' => 'NOT IN',
    		),
    	),
    );

    So it would become:

    $patient_story_args = array(
    	'post_type' => 'patient_story',
    	'posts_per_page' => -1,
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'patient_story_type',
    			'field' => 'slug',
    			'terms' => $patient_story_type->slug,
    		),
    		array(
    			'taxonomy' => 'patient_story_type',
    			'terms' => array('featured'),
    			'field' => 'slug',
    			'operator' => 'NOT IN',
    		),
    	),
    	'search_filter_id' => '123456',
    );

    Where 123456 is the ID of your search form.

    I might add though, if you have a lot of posts (over a few hundred), then this approach might start to become slow, because you are running multiple WP_Query – there might be a better (but probably much more complicated) way of doing it in that case.

    Best

Viewing 2 posts - 1 through 2 (of 2 total)