Support

Account

Home Forums Front-end Issues List terms which also have a certain relationship

Solving

List terms which also have a certain relationship

  • So I’m not sure if this fits strictly into ACF but I thought you guys might be able to point me in the right direction.

    My setup:

    • Stockists (Custom Post Type)
      • Countries (Custom Taxonomy)
    • Promotions (Custom Post Type)
      • Relationship Field (Stockists)

    I am working on the Promotions single page (single-promotions.php) and need to list the stockists included in the promotion. This by itself is quite simple and I can do. The clincher is that I want to group them under their country for easier browsing.

    So it would look something like this instead of a straight list.

    • Country A
      • Stockist
      • Stockist
    • Country B
      • Stockist
      • Stockist
      • Stockist
    • Country C
      • Stockist
      • Stockist

    I need to list only countries that have posts with the current relationship field. I can’t quite get my head around what I need to do for my meta_query. This is what I have so far which just returns the entire list of countries.

    <?php
    // https://www.advancedcustomfields.com/resources/relationship/
    
    // Grab the relationship field IDs
    $participating_ids = get_field('stockists_participating', false, false);
    
    $tax = 'countries';
    $tax_terms = get_terms( 
    	array(
    		'taxonomy'		=> $tax,
    		'hide_empty' 	=> 'true',
    		'tax_query'        	=> array (
    			'relation' 			=> 'IN',
    			array(
    				'post__in'		=> $participating_ids,
    			),
    		),
    	)
    ); 
    
    // List of countries with promotional stockists
    if ($tax_terms) : 
    
    	foreach ($tax_terms as $tax_term) : ?>
    
    		<div><?php echo $tax_term->name; ?></div>
    
    			<a href="#<?php echo $tax_term->slug; ?>">
    				<?php echo $tax_term->name; ?>
    			</a>
    
    			<?php // Loop through stockists related to this promotion and in this country (only) ?>
    
    		<?php endif;
    
    		wp_reset_query(); */
    
    	endforeach; 
    
    endif; ?>

    I’ve been able to do a good meta_query with getting two taxonomies but this is a little out of my league. Any ideas are hugely appreciated!

  • Hi @jakegonzales

    I believe it would be easier to just re-list and group the returned relationship value in an array like this:

    $result = array();
    
    $relationships = get_field("stockists_participating");
    
    foreach( $relationships as $thepost ) {
        
        $thepost_terms = get_the_terms($thepost->ID, 'countries');
        
        foreach( $thepost_terms as $thepost_term ) {
            
            // initialize if the term not listed yet
            if( !isset($result[$thepost_term->slug]) ) {
                $result[$thepost_term->slug] = array();
            }
            
            $result[$thepost_term->slug]['term'] = $thepost_term;
            $result[$thepost_term->slug]['value'][] = $thepost;
            
        }
        
    }
    
    print_r($result);

    With that code, you can loop trough the $result instead. Please take a look at the printed result to see the structure of the data.

    I hope this helps 🙂

  • Thanks James,

    I’m not sure if I’m not understanding your code or if I didn’t explain my problem properly. I believe I am already able to output what you have suggested with a query like this(?):

    $args = array (
    	'post_type'				=> 'stockists',
    	'post__in'				=> $participating_ids,
    	'meta_key' 				=> 'stockist_address_0_stockist_country',
    );

    This gives me a list of stockists, as does your code — as far as I can tell.

    I need a list of countries but only if they are on posts that have the relationship. Then also the country should only appear once. I am trying to use them as headings for the stockists in the promotion e.i.:

    Participating Stockists
    =======================
    England
    ——-
    – Stockist

    Scotland
    ——–
    – Stockist

    Wales
    —–
    – Stockist

    I was just asking about the first part of getting the countries to list out but I would then make sure the nested loop only outputted stockists in the country AND the current promotion relationship.

    I’m no programmer so forgive me if I’ve missed if your solution is actually doing that.

  • Hi @jakegonzales

    I believe the code you described only lists the stockist custom posts on the Promotions page. I think you can get the same result by using this code:

    $relationships = get_field("stockists_participating");.

    If you want to group the returned posts from above code, you need to check each of them and group it in an array.

    $thepost_terms = get_the_terms($thepost->ID, 'countries');

    Is used to get all of the terms that belong to a post. After that, you need to loop trough the terms and add a group if it does not exist yet. So if you have a promotion page with these posts in the relationship field:

    Post 1: term 1, term 2
    Post 2: term 2, term 3
    Post 3: term 1
    Post 4: term 3
    Post 5: term 4, term 5

    The result would be like this:

    Term 1: Post 1, Post 3
    Term 2: Post 1, Post 2
    Term 3: Post 2, Post 4
    Term 4: Post 5
    Term 5: Post 5

    Is that what you want?

    If you don’t have time to learn about PHP, I suggest you hire a developer to help you out with it, and I’d recommend looking for one on https://studio.envato.com/ or https://www.upwork.com/.

    I hope this helps 🙂

  • Hey James,

    That is close to what I want but I need it to also check if those posts are related to the other posts as well haha.

    I’ll see if I can get myself sorted out with what you’ve given me. It’s obvious I’ve gotten myself in a little deeper than I understand.

    Thanks for the all the information and your time! I’ll post my solution should I figure it out.

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

The topic ‘List terms which also have a certain relationship’ is closed to new replies.