Title: problem with association fields
Last modified: August 31, 2016

---

# problem with association fields

 *  Resolved [stardeuche](https://wordpress.org/support/users/stardeuche/)
 * (@stardeuche)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/problem-with-association-fields/)
 * hi,
 * i use carbon fields for my project and i want to put a assocaition field. in 
   my back office, the association field works but in my front office i have got
   some problems for use all the datas.
 * in my exmaple i want take a terms values and the post of custom post type. with
   my code i can take the terms with the ID but i cannot take the post. My code 
   is here :
 *     ```
       //start by fetching the terms for the catalogue taxonomy
       $terms = get_terms( 'catalogue', array(
           'orderby'    => 'portique-antivol',
           'order'      => 'DESC',
           'hide_empty' => 0,
       	)
       );
   
       //variables for carbon fields association
       $id = get_the_ID();
       $name = 'crb_association';
   
       //array of carbon fields association
       $arraySols = carbon_get_post_meta($id, $name, 'association');
   
       //check arraySols with a foreach : i take a term ID...
       foreach ($arraySols as $arraySol) {
   
          if( $arraySol['type'] == 'term' ){
       	$idTerm = $arraySol['id'];
   
       	$terms = get_terms( 'catalogue', array(
       		'order'      => 'DESC',
       		'hide_empty' => 0,
       	        'include'    => $idTerm
       		);
   
       	foreach ($terms as $term) {
   
       	//Images of my custom category
       	$image = wp_get_attachment_image_src( carbon_get_term_meta( $term->term_id, 'crb_thumb' ), array( 381, 286 ) );
   
       		echo '<div class="global-solutions">';
       			echo '<img src="' . $image[0] . '" alt="solutions" />';
   
       	 		echo '<div class="desc-solutions">';
   
       	    // Define the query
       	    	$args = array(
       	        	'post_type' => 'solutions',
       	        	'catalogue' => $term->slug
       	    	);
       							    	              $querySolutions = new WP_Query( $args );
   
       // output the term name in a heading tag
       echo '<h2>' . $term->name . '</h2>';
   
       echo '<ul>';
   
       										// Start the Loop
       							        	while ( $querySolutions->have_posts() ) : $querySolutions->the_post();
       											echo '<li class="solutions-listing" >';
       										        echo '<a href="'. get_permalink() .'">';
       										        	echo the_title();
       										        echo '</a>';
       									        echo '</li>';
   
       							        	endwhile;
   
       echo '</ul>';
   
       // use reset postdata to restore orginal query
       						   			wp_reset_postdata();
   
       echo '</div>';
   
       echo '</div>';
       					}
       				}
       			}
       ```
   
 * with this code i can take a term of this association fields but i can’t take 
   the post…
 * whats the procedure for take the post of the association fields?
 * thanks so lot
 * doogie1
 * [https://wordpress.org/plugins/carbon-fields/](https://wordpress.org/plugins/carbon-fields/)

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Plugin Author [htmlBurger](https://wordpress.org/support/users/htmlburger/)
 * (@htmlburger)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/problem-with-association-fields/#post-7270214)
 * Hi [@stardeuche](https://wordpress.org/support/users/stardeuche/),
 * Can you please describe what you need to do in short? An example would also be
   helpful.
 * Thanks.
 *  Thread Starter [stardeuche](https://wordpress.org/support/users/stardeuche/)
 * (@stardeuche)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/problem-with-association-fields/#post-7270328)
 * hi,
 * with this code i want in a list the term with the post.
 * this informations is checking in back office by administrator.
 * it’s sufficient for you
 * thanks
 *  Plugin Author [htmlBurger](https://wordpress.org/support/users/htmlburger/)
 * (@htmlburger)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/problem-with-association-fields/#post-7270338)
 * Hi again,
 * We’re unable to understand what you mean by this:
 *     ```
       with this code i want in a list the term with the post.
       ```
   
 * Can you please clarify? An example will probably be helpful.
 *  Thread Starter [stardeuche](https://wordpress.org/support/users/stardeuche/)
 * (@stardeuche)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/problem-with-association-fields/#post-7270349)
 * hi,
 * i’m very, very sorry for my explications. it’s difficult to describe my idea.
   
   i restart again with other explications.
 * i use carbon fields association, and in front office i have got a array like 
   this :
 *     ```
       array (size=6)
         0 =>
           array (size=3)
             'id' => string '20' (length=2)
             'type' => string 'term' (length=4)
             'taxonomy' => string 'catalogue' (length=9)
         1 =>
           array (size=3)
             'id' => string '17' (length=2)
             'type' => string 'term' (length=4)
             'taxonomy' => string 'catalogue' (length=9)
         2 =>
           array (size=3)
             'id' => string '524' (length=3)
             'type' => string 'post' (length=4)
             'post_type' => string 'solutions' (length=9)
         3 =>
           array (size=3)
             'id' => string '522' (length=3)
             'type' => string 'post' (length=4)
             'post_type' => string 'solutions' (length=9)
         4 =>
           array (size=3)
             'id' => string '510' (length=3)
             'type' => string 'post' (length=4)
             'post_type' => string 'solutions' (length=9)
         5 =>
           array (size=3)
             'id' => string '21' (length=2)
             'type' => string 'term' (length=4)
             'taxonomy' => string 'catalogue' (length=9)
       ```
   
 * with this datas i want a list, with the term (id: 20,17,21) and the selected 
   post (id: 510, 522, 524) corresponding to terms :
 *     ```
       <h2>term name</h2>
       <ul>
       <li>selected post by administrator</li>
       <li>selected post by administrator</li>
       <li>selected post by administrator</li>
       </ul>
       ```
   
 * i put again the code with an other organisation :
 *     ```
       //variables for carbon fields association
       			$id = get_the_ID();
       			$name = 'crb_association';
   
       			//array of carbon fields association
       			$arraySols = carbon_get_post_meta($id, $name, 'association');
       			var_dump($arraySols);
   
       			//check arraySols with a foreach : i take a term ID...
       			foreach ($arraySols as $arraySol) {
   
       				if( $arraySol['type'] == 'term' ){
       					$idTerm = $arraySol['id'];
   
       					$terms = get_terms( 'catalogue', array(
       						'order'      => 'DESC',
       			    		'hide_empty' => 0,
       			    		'include'    => $idTerm
       						)
       					);
   
       					foreach ($terms as $term) {
   
       						//Images of my custom category
       						$image = wp_get_attachment_image_src( carbon_get_term_meta( $term->term_id, 'crb_thumb' ), array( 381, 286 ) );
   
       						echo '<div class="global-solutions">';
       							echo '<img src="' . $image[0] . '" alt="solutions" />';
   
       						 		echo '<div class="desc-solutions">';
   
       								    // Define the query
       							    	$args = array(
       							        	'post_type' => 'solutions',
       							        	//'post__in'  => $posts,
       							        	'catalogue' => $term->slug
       							    	);
       							    	$querySolutions = new WP_Query( $args );
   
       							    	// output the term name in a heading tag
       									echo '<h2>' . $term->name . '</h2>';
   
       							    	echo '<ul>';
   
       										// Start the Loop
       							        	while ( $querySolutions->have_posts() ) : $querySolutions->the_post();
   
       											echo '<li class="solutions-listing" >';
       										        echo '<a href="'. get_permalink() .'">';
       										        	echo the_title();
       										        echo '</a>';
       									        echo '</li>';
   
       							        	endwhile;
   
       						        	echo '</ul>';
   
       						       		// use reset postdata to restore orginal query
       						   			wp_reset_postdata();
   
       						 		echo '</div>';
   
       						echo '</div>';
       					}
       				}
       			}
       ```
   
 * it’s difficult to me to describe my idea, i hope you find a solution…
 * thanks so lot
 *  Thread Starter [stardeuche](https://wordpress.org/support/users/stardeuche/)
 * (@stardeuche)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/problem-with-association-fields/#post-7270353)
 * hi,
 * yes i find. in the beginning of my code i put an other foreach for check the 
   ID of the post like this :
 *     ```
       //check arraySols with a foreach : i take a ID of post
         foreach ($arraySols as $arraySol) {
       	if( $arraySol['type'] == 'post' ){
          $idPosts[] = $arraySol['id'];
         }
       }
       ```
   
 * and i use this array of post ID in a wp query :
 *     ```
       // Define the query
       $args = array(
         	'post_type' => 'solutions',
          	'post__in'  => $idPosts,
          	'catalogue' => $term->slug
       	);
       $querySolutions = new WP_Query( $args );
       ```
   
 * finally my code is like this :
 *     ```
       //variables for carbon fields association
       $id = get_the_ID();
       $name = 'crb_association';
   
       //array of carbon fields association
       $arraySols = carbon_get_post_meta($id, $name, 'association');
   
       //check arraySols with a foreach : i take a ID of post
       foreach ($arraySols as $arraySol) {
       	if( $arraySol['type'] == 'post' ){
       		$idPosts[] = $arraySol['id'];
       	}
       }
   
       //check arraySols with a foreach : i take a term ID...
       foreach ($arraySols as $arraySol) {
   
       	if( $arraySol['type'] == 'term' ){
       		$idTerm = $arraySol['id'];
   
       	//object of terms catalogue
       	$terms = get_terms( $taxo, array(
       		'order'      => 'DESC',
           		'hide_empty' => 0,
           		'include'    => $idTerm
       		)
       	);
   
       		foreach ($terms as $term) {
   
       		//Images of my custom category
       		$image = wp_get_attachment_image_src( carbon_get_term_meta( $term->term_id, 'crb_thumb' ), array( 381, 286 ) );
   
       		echo '<div class="global-solutions">';
       		echo '<img src="' . $image[0] . '" alt="solutions" />';
   
        		echo '<div class="desc-solutions">';						
   
       		// Define the query
       		$args = array(
       		   	'post_type' => 'solutions',
       		   	'post__in'  => $idPosts,
       		   	'catalogue' => $term->slug
       				);
       		$querySolutions = new WP_Query( $args );
   
       	// output the term name in a heading tag
       	echo'<h2><a href="' . get_term_link($term->slug, $taxo) . '">' . $term->name . '</a></h2>';
       echo '<span class="border-bottom"></span>';
   
          	echo '<ul>';
   
       	// Start the Loop
              	while ( $querySolutions->have_posts() ) : $querySolutions->the_post();
   
       										echo '<li class="solutions-listing" >';
       										    echo '<a href="'. get_permalink() .'">';
       										       	echo the_title();
       										    echo '</a>';
       									    echo '</li>';
   
              endwhile;
   
             echo '</ul>';
   
             	// use reset postdata to restore orginal query
              wp_reset_postdata();
   
       	echo '</div>';
       	echo '</div>';
       		}
       	}
       }
       }
       ```
   
 * i think that my code is so complicated but it’s work…if you have an other solutions
   with less code, i take it 😉
 * thanks so lo
 *  Plugin Author [htmlBurger](https://wordpress.org/support/users/htmlburger/)
 * (@htmlburger)
 * [10 years, 1 month ago](https://wordpress.org/support/topic/problem-with-association-fields/#post-7270408)
 * Hi [@dewy](https://wordpress.org/support/users/dewy/),
 * We believe you’ll need to organize your fields in a little different way to make
   it easier to display in the front end. Here is how we envision it:
 * 1. Create a complex field with a Select field in it. That select field would 
   allow you to select a term.
    2. In the complex field, there should be an association
   field that would allow you to select posts. These will be the posts that will
   appear under each term.
 * Then, when displaying that complex field in the front end, you will easily display
   each term and its corresponding posts.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘problem with association fields’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/carbon-fields_cacbcc.svg)
 * [Carbon Fields](https://wordpress.org/plugins/carbon-fields/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/carbon-fields/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/carbon-fields/)
 * [Active Topics](https://wordpress.org/support/plugin/carbon-fields/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/carbon-fields/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/carbon-fields/reviews/)

 * 6 replies
 * 2 participants
 * Last reply from: [htmlBurger](https://wordpress.org/support/users/htmlburger/)
 * Last activity: [10 years, 1 month ago](https://wordpress.org/support/topic/problem-with-association-fields/#post-7270408)
 * Status: resolved