Support

Account

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

Solved

Sorting Relationship Field Entries by Custom Tax Terms

  • I’m hitting a dead end on attempting to sort out my relationship field entries into categories. Basically, I’m wanting to set up an accordion menu that takes the custom post type listed in the relationship field and lists those items in a tab with the tab label coming up as a custom taxonomy listing. I’ve been wracking my head around this for far too much time and am struggling to find any good solutions for this online. Here’s what I’ve got going:

    $associated_downloads = get_field('associated_downloads');
    $custom_terms = get_terms('download_category');
        
    foreach($custom_terms as $custom_term) {
    
    	if ($associated_downloads) {
    	  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											 
    	                            )
    							),
    										 
                'meta_query' => array(											 
    	            'key' => 'associated_downloads',											 
    	            'value' => '"'.get_the_ID().'"',											 
    	            'compare' => 'LIKE'
    	    	),
            );
    
    									
    
            echo '<ul class="accordion" data-accordion data-allow-all-closed="true">';
    								
    									 
            if(count($associated_downloads)>=1 and $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()) {
    				
    				while($req_query->have_posts()) {
    					echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
    				}
    				echo '</ul>'.'</div>'.'</li>';
    			}
    
    		wp_reset_postdata;
    
    		}
    	}
    }

    This solution keeps on spinning and seems to be getting hung up in my innermost loop which means my query is off… but can’t figure out what to do. Any help is greatly appreciated.

  • You’re missing the_post()

    
    while($req_query->have_posts()) {
      the_post();
    
  • When I input just the_post(); as suggested, I get stuck in the loop and the page never fully loads. I got it to pull in all of the posts with $req_query->the_post();.

    $req_query->the_post(); brings in all of the custom post type posts, and sorts them accordingly, but not just what was selected in the relationship field from the page editor.

  • I would do it like this. Get the relationship field without formatting, this will return an array of post IDs

    
    $associated_downloads = get_field('associated_downloads', false, false);
    

    and the query args would look something like this

    
    $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
    );
    

    Which should get the post selected in a specific term

  • That did the trick John. Thanks for the help!

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

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