Support

Account

Home Forums ACF PRO Duplicate Taxonomy with Reverse Relationship

Solved

Duplicate Taxonomy with Reverse Relationship

  • Hi Folks,

    I have a reverse relationship query where I’m trying to display each taxonomy then the posts of that taxonomy underneath. I’ve tried everything here and can’t figure this out. Here is what I have:

    <?php 
    
    			/*
    			*  Query posts for a relationship value.
    			*  This method uses the meta_query LIKE to match the string "123" to the database value a:1:{i:0;s:3:"123";} (serialized array)
    			*/
    
    			$research = get_posts(array(
    				'post_type' => 'research-data',
    				'meta_query' => array(
    					array(
    						'key' => 'show_on_page',
                    'value' => '"' . get_the_ID() . '"', // matches exaclty "123", not just 123. 
                    'compare' => 'LIKE'
                    )
    					)
    				));
    				?>
    				<?php if( $research ): ?>
    				<h3> Research & Data</h3>
    				<?php foreach( array_unique($research, SORT_REGULAR) as $r ): ?>
    
    				<!-- Begin custom tax loop -->
    				<?php
    
    				$categories = get_the_terms($r->ID, 'research-cats', $term_args);
    
    				
    
    				foreach (array_unique($categories, SORT_REGULAR) as $term) :
    					
    					
    					echo'<h2>' . $term->name . '</h2>';
    				
    				$posts = get_posts(array(
    					'post_type' => 'research-data',
    					'orderby' => 'menu_order',
    					'order' =>  'ASC',
    					'post__in' => array($r->ID),
    					'nopaging' => true,
    					'research-cats' => $term->slug
    					));
    
    					?>
    					<?php foreach(array_unique($posts, SORT_REGULAR) as $post):?>
    					<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?> </a></li>
    				<?php endforeach; ?>
    			</ul>
    		<?php endforeach; ?>
    	<?php endforeach; ?>
    <?php endif; ?>

    Here is the result of this:
    https://www.dropbox.com/s/oj34oehk5tlfka0/Screenshot%202015-10-05%2013.30.19.png?dl=0

    Any thoughts?

  • I think I might have been over-thinking this. I resolved this with the following:

    
    <?php
    $post_type = 'research-data';
    $tax = 'research-cats';
    $tax_terms = get_terms($tax,'hide_empty=0');
    
    //list everything
    if ($tax_terms) {
      foreach ($tax_terms  as $tax_term) {
        $args=array(
          'post_type' => $post_type,
          "$tax" => $tax_term->slug,
          'post_status' => 'publish',
          'posts_per_page' => -1,
          'caller_get_posts'=> 1
        );
    
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo "<h2 class=\"tax_term-heading\" id=\"".$tax_term->slug."\"> $tax_term->name </h2>";
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
            <?php
          endwhile;
         // echo "<p><a href=\"#top\">Back to top</a></p>";
        }
        wp_reset_query();
      }
    }
    ?>
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Duplicate Taxonomy with Reverse Relationship’ is closed to new replies.