Support

Account

Home Forums General Issues Sorting Relationship Field Entries by Custom Tax Terms part 2

Unread

Sorting Relationship Field Entries by Custom Tax Terms part 2

  • This is in relation to my last post here. After some testing, I’ve determined I need this to function a little differently than initially planned.

    Initially, I only needed to have my custom taxonomies come in, and have all of my custom post types be sorted into it, but only if they were selected in the relationship field on the back end.

    Right now, the relationship posts are sorted into the appropriate taxonomies as they should, and are listing only under what they’re supposed to. All working well there.

    The problem has become that now it is pulling any taxonomy listing that has a post assigned to it, whether a post is selected for the taxonomy or not. As more taxonomy entries are added, this is becoming quite cumbersome on the user end. Now, I’m trying to get it to only pull the taxonomies of the posts that have been selected and no others. Again, having no luck and not finding much on this from search. Any help is greatly appreciated.

    Here’s the code for reference:

    <?php
    
    	$associated_downloads = get_field('associated_downloads', false, false);
    	$custom_terms = get_terms('download_category');
    	foreach($custom_terms as $custom_term) {
    
    		if ($associated_downloads and count($associated_downloads) >= 1) {
    			if (!is_array($associated_downloads)) {
    				$associated_downloads = array($associated_downloads);
    			}
    
    			$args = array(
    				'post_type' => 'downloads',
    				'post_status' => 'publish',
    				'tax_query' => array(
    					array(
    						'taxonomy' => 'download_category',
    						'field' => 'slug',
    						'terms' => $custom_term->slug
    					)
    				),
    
    				'post__in' => $associated_downloads
    			);
    
    			echo '<ul class="accordion" data-accordion data-allow-all-closed="true">';
    			
    			if($custom_term->parent) {
    				echo '<li class="accordion-item" data-accordion-item>'.'<a href="#" class="accordion-title">'.$custom_term->name.'</a>'.'<div class="accordion-content" data-tab-content>'.'<ul class="menu vertical">';
    
    				$req_query = new WP_Query($args);
    				if ($req_query->have_posts()) {
    					//print_r($req_query);
    					while($req_query->have_posts()) {
    						$req_query->the_post();
    						echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
    					}
    				
    				}
    
    				echo '</ul></div></li>';
    			
    			}
    			echo '</ul>';
    
    		}
    	}
    	wp_reset_postdata();
    ?>
Viewing 1 post (of 1 total)

The topic ‘Sorting Relationship Field Entries by Custom Tax Terms part 2’ is closed to new replies.