Support

Account

Home Forums Front-end Issues Displaying a relationship field on a custom post type taxonomy page

Solved

Displaying a relationship field on a custom post type taxonomy page

  • I’m trying to display a relationship field inside of the WordPress loop on a custom post type taxonomy page (taxonomy-thetaxname.php). The code I’m using (from here) works fine on the archive page (archive-custompostname.php) for the custom post type, but it breaks the WordPress loop on the taxonomy page (it shows only the first entry).

    Here’s the code I’m using:

    <?php if ( have_posts() ) : ?>
    	<?php /* Start the Loop */ ?>
    	<?php while (have_posts()) : the_post(); ?>
    								
    		<h2><?php the_title(); ?></h2>				
    	
    		<?php 		 
    		$posts = get_field('store-type');		 
    		if( $posts ): ?>
    		    <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
    		        <?php setup_postdata($post); ?>
    		            
    		            <?php if(get_field('store-type-logo')) { ?>				
    		            	<?php $image = wp_get_attachment_image_src(get_field('store-type-logo'), 'Store_Logo'); ?>
    		            	<img src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_field('store-type-logo')) ?>" width="<?php echo $image[1]; ?>" height="<?php echo $image[2]; ?>" class="plain" />
    		            <?}?>
    		            
    		    <?php endforeach; ?>
    		    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    		<?php endif; ?>					
    					
    	<?php endwhile; wp_reset_query(); ?>	
    <?php endif; ?>

    If I remove the relationship field code, then the page works fine. Any idea why the ACF code isn’t working on this template page?

    Thanks!

  • Hi @megancm

    Just to clarify, this template is looping over $post objects filtered by a taxonomy? If so, then the issue is most likely your setup_postdata($post); code which can cause many problems during a loop.

    To test this, change your code to:

    <?php if ( have_posts() ) : ?>
    	<?php /* Start the Loop */ ?>
    	<?php while (have_posts()) : the_post(); ?>
    								
    		<h2><?php the_title(); ?></h2>				
    	
    		<?php 		 
    		$posts = get_field('store-type');		 
    		
    		echo '<pre>';
    			print_r( $posts );
    		echo '</pre>';
    		
    		?>				
    					
    	<?php endwhile; ?>	
    <?php endif; ?>

    This will simply output the relationship data, and the page shoudl work.
    Let me know if the page works, then we can go about re writing your loop to not use the setup_postdata($post); function.

    I believe there is a code example on the relationship field dcos regarding how to do this, so you might be able to figure it out before posting back.

    Thanks
    E

  • The template (taxonomy-product_type.php) is supposed to display items in a particular taxonomy. My custom post type is “products” and my taxonomy is “product_type”. So for example, it should just show me items in the “book” or “toys” category when I go to each URL.

    Everything works fine when I remove the relationship data, it loops through all the right entries. But once I add in the ACF code it breaks.

    I just changed the code and it does output the relationship data for the first entry. But then it shows empty HTML (h2, pre tags) for the other posts in the loop. So the same thing seems to be broken…

  • Hi @megancm

    If the WP function the_title is not working, then there is a big problem going on with the global $post object.

    Please make sure you don’t have any setup_postdata function running on the template or even in your functions.php.

    Thanks
    E

  • Hmm, I don’t know (I’m not a super advanced WP programmer). But I did a search in the code for “setup_postdata” on the template and in my functions file and didn’t come up with anything. I also tried removing any custom code I’ve added and the same thing happens when I add the following to my template. It’s like it’s looping through, but not showing any data other than the first item. But if I remove the code below, it works fine.

    
    <?php 		 
    $posts = get_field('store-type');		 
    echo '<pre>';
    print_r( $posts );
    echo '</pre>';
    ?>
  • Hi @megancm

    Sorry to hear you are still having the issue. There is definitely a conflict in code happening, as the relationship field works fine on my install and all others that I know of.

    Can you try changing $posts = get_field('store-type'); to $posts = get_field('store-type', false, false);

    This will prevent any loading of actual posts and if this fixes the issue, then the issue must be a filter hooked into the pre_get_posts or similar.

    Thanks
    E

  • So when I do that, it displays the title of the first post, and then this:

    Array
    (
        [0] => 78
    )

    And then it loops through the other entries but doesn’t output any data for them, just HTML.

  • Hi @megancm
    here is some code to test that the post_id is correct:

    
    <?php if ( have_posts() ) : ?>
    	<?php /* Start the Loop */ ?>
    	<?php while (have_posts()) : the_post(); 
    		
    		$post_id = get_the_ID();
    		
    		?>
    						
    		<h2>Title: <?php the_title(); ?></h2>				
    		ID: <?php echo $post_id; ?>
    
    		
    		ACF without ID
    
    		<?php
    	
    		echo '<pre>';
    			print_r( get_field('store-type', false, false) );
    		echo '</pre>';
    		
    		?>	
    		
    		
    		ACF with ID
    
    		<?php
    	
    		echo '<pre>';
    			print_r( get_field('store-type', $post_id, false) );
    		echo '</pre>';
    		
    		?>				
    					
    	<?php endwhile; ?>	
    <?php endif; ?>
    

    Please use this code and check that the post_id displayed are the ones you are expecting

    Thanks
    E

  • Yes, it’s displaying all the titles and post_ids correctly.

    Out of 12 posts, 4 of them are showing a different number in the “array”. Is that something important to note? So 8 show the number “78” and 4 show the number “75”.

  • Hi @megancm

    Your last reply is quite vague. Which array are you talking about?

    The above code was to help you see visually that the loop is working correctly.
    The array variable printed out is the selected relationship items/ Yes, these should be different, shouldn’t they?

    Please look at the outputted data and determine if the data is correct, or if it is not.

    Thanks
    E

  • Sorry, I was totally confused, I get it now. Yes, the data is correct. What do I do now?

  • Hi @megancm

    There are 2 print_r statements. One of them uses false, and the other uses $post_id.

    Do these 2 print_r statements display the same data?

    This test is to confirm that the issue is not with the $post_id.

  • Yes, both statements display the same data.

  • Hi @megancm

    We can now agree that ACF is correctly loading the value, the problem you originally posted must be that you have a plugin that is modifying the pre_get_posts filter and preventing ACF from loading the gallery images.

    Perhaps you can start debugging in the relationship field file. There is a function for format_value_for_api` – this is where the posts are loaded in from the array of IDS.

    This will be a good place to start.

    Thanks
    E

  • Thanks for your help Elliot, I appreciate it.

    Although, I should mention that this problem is still occurring even when I disable all plugins (except ACF) and use a different theme. I will see if I can get a programmer to look into this for me.

  • Hi! Just wanted to update you on this. I had a programmer look at it and apparently the issue was that the $posts global variable was being overridden.

    When I searched through the code, the only places I saw that variable mentioned was in the main WordPress files, the ACF plugin, and the Event Calendar plugin (which I tried disabling and didn’t make a difference). So I’m not sure exactly what was the culprit there, but once that variable name was changed, it worked fine.

    Here’s the code that ended up working in case anyone else has a similar issue:

    <?php $store_type_posts = (array) get_field('store-type'); ?>
    <?php foreach ($store_type_posts as $store_type_post): ?>
    <?php $store_type_logo_id = get_field('store-type-logo', $store_type_post->ID) ?>
    <?php if ($store_type_logo_id) : ?>
    <?php $image = wp_get_attachment_image_src($store_type_logo_id, 'Store_Logo'); ?>
    <?php $alt_text = get_the_title($store_type_logo_id); ?>
    <img src="<?php echo $image[0]; ?>" alt="<?php echo $alt_text; ?>" width="<?php echo $image[1]; ?>" height="<?php echo $image[2]; ?>" class="plain" />
    <?php endif ?>
    <?php endforeach ?>
  • I’m having the same exact problem you have but on my archive-custom-post-type.php page with Advanced Custom Fields Pro latest version.

    This is the code i’m using:

    <section id="debates-home">
    	<?php 
    	
    	$the_query = new WP_Query( array( 'post_type' => 'debate' , 'orderby' => 'date', 'posts_per_page' => 2 ) ); 
    	if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();  
    	
    	$term = wp_get_post_terms($post->ID, 'area', array("fields" => "names"));
    	$term_slug = wp_get_post_terms($post->ID, 'area', array("fields" => "slugs"));
    	
    	?>
    	
    	<article>
    		<?php the_post_thumbnail(); ?>
    		<div class="info-debate">
    			<a class="category-link" href="<?php echo get_site_url(); ?>/area/<?php echo $term_slug[0] ?>/?post_type=debate"><?php echo $term[0]; ?></a>
    			<h3><a href="<?php the_permalink(); ?>" title="Ver debate: <?php the_title(); ?>"><?php the_title(); ?></a></h3>
    		
    			<?php 
    			
    			$posts = get_field('columnas_del_debate');
    			
    			if( $posts ): ?>
    			    <ul>
    			    <?php foreach( $posts as $post): ?>
    			        <?php setup_postdata($post); ?>
    			        <li>
    			            <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_post_thumbnail('thumb'); ?></a>
    			            <span class="tooltip">
    			            	<em><?php the_title(); ?></em><br> <?php the_field('autor_de_columna'); ?>
    			            </span>
    			        </li>
    			    <?php endforeach; ?>
    			    </ul>
    			    <?php wp_reset_postdata(); ?>
    			<?php endif; ?>
    								
    		</div>
    	</article>
    	
    	<?php endwhile; ?>
    		
    		<?php wp_reset_postdata(); ?>
    	
    	<?php else : ?>
    		<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
    	<?php endif; ?>			
    </section>
    

    The code works fine on my index.php and will break on archive-custom-post-type.php page. The only difference in the code on the archive page is that the loop is just the plain version with no args since I don’t need to modify the loop.

    If turn wp_debug on i’ll see the following error on my index.php:

    Notice: Trying to get property of non-object in /wp-includes/query.php on line 4618
    
    Notice: Trying to get property of non-object in /wp-includes/query.php on line 4620
    
    Notice: Trying to get property of non-object in /wp-includes/query.php on line 4622
    
    Notice: Trying to get property of non-object in /wp-includes/query.php on line 4623
    
    Notice: Trying to get property of non-object in /wp-includes/query.php on line 4631
    
    Notice: Trying to get property of non-object in /wp-includes/query.php on line 4646

    I tried what Elliot said before and ACF seems to be working fine cause the array with the related posts appears within the print_r.

    Is there a way to use setup_postdata inside the loop on an archive-custom-post-type.php page?

    Thanks!

    PD: I lost my old forum account, dont know why? Was the forum reset or something?

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

The topic ‘Displaying a relationship field on a custom post type taxonomy page’ is closed to new replies.