Support

Account

Home Forums ACF PRO Looping CPT with Taxonomy field returns CPTs but not ACF fields in the CPT

Solved

Looping CPT with Taxonomy field returns CPTs but not ACF fields in the CPT

  • I’m missing a step in a loop. I want to display a the images and links from a custom post type of “Partners”. The CPTs are displayed according to a taxonomy field which returns ‘Field ID’.

    The loop returns the right Partner’s but I can’t get the images and URLs to pull through.

    Any advice on how I need to modify $the_query portion of my code would be gratefully received.

    <div id="partners" class="association-partners" data-aos="fade-up" data-aos-duration="4000" data-aos-easing="ease-in-out">
    
          <h2><?php the_field('pg_title'); ?></h2>
          <ul class="partners-list">
          <?php
                $pgcat = get_field('partner_category');
                $args = array(
                'post_type' => 'partners',
                'posts_per_page' => '-1',
                'category__in' => $pgcat,
                'orderby' => 'rand',
                
                );
                $the_query = new WP_Query ( $args ); 
                ?>
          
          <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
                <li>            
                   <a href="<?php the_field('youtube_link'); ?>" target="_blank"><p><?php the_title(''); ?></p>
                   <?php 
                      $image = the_field('partner_logo');
                      if( !empty( $image ) ): ?>
                   <img src="<?php echo esc_url($image['url'], $pgcat); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
                   <?php endif; ?></a>
                </li>
                <?php 
                   // Reset the global post object so that the rest of the page works correctly.
                   wp_reset_query(); ?>
                <?php endwhile; endif; ?>
          </ul>
    
       </div>

    What the loop returns

  • Is this in an ACF block? Or a template? Usually when this happens, it’s an issue with the post ID being used for the_field within the loop.

    So if, for example, you had this loop inside of an ACF block, it may be looking for those fields on the block itself, not the looped post. Or it could be looking for these fields on the page the loop is on vs. the individual looped post.

    You could try passing the post ID to the_field calls within the loop to see if that works. Without knowing really the context of where this loop exists, it’s hard to say for sure.

  • Thank you for your comment, context would have been useful @wpfieldwork!

    The code is in a custom block. I’ve been trying to play around with where the ID is passed but haven’t found a solution yet…

  • this is incorrect

    
    $image = the_field('partner_logo');
    

    the_field() echos the value of the field, it does not return a value.

    You have to use

    
    $image = get_field('partner_logo');
    
  • What John noted, the error in your code, does exist, you’re going to want to change the_field to get_field. However, because you said this is also within a block and none of your fields are displaying, not just the image due to said error, it’s because you do need to pass the ID when using the_field, as well, so I would try something like:

    $image = get_field('partner_logo',get_the_ID());

    And ensure the ID is getting passed on any calls within a loop in an ACF block.

  • @wpfieldwork Yes!

    That fixed things perfectly. I’ve been working through a PHP course and wanted to push myself with a taxonomy query in an ACF block. I could sort of see that the ID was required but couldn’t puzzle it out.

    Thank you so much for your time and advice, you’ve made a dev very happy.

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

You must be logged in to reply to this topic.