Support

Account

Home Forums Front-end Issues Custom Post Taxonomies and ACF

Solving

Custom Post Taxonomies and ACF

  • I have a bunch of post taxonomies for the site I am working on. They are: actors, studios, shows, films, directors, and franchises. They are created with the Custom Post Type UI plugin.

    I created an ACF record to display when editing a show taxonomy that would allow a site admin to select actors, so that if viewing a show, you could see stories about the actor in the show. All well and good.

    However, I am running into a WP_Query issue. In my custom taxonomy-show.php template, I have a normal WordPress post loop, and underneath, I query ACF get_field(‘actors’) which gives me the list of actors. However, the second I assign a show to a post, the WP_Query blows away the data from ACF, and when looking at the db queries, WP is setting post_meta from the posts in the forloop and causing the ACF field to render blank.

    I’ve tried wp_reset_postdata and wp_reset_query on both the have_post forloop and also the ACF query, but to no avail. The second I remove a post from that taxonomy, then the actors list re-appears.

    Template is below:

    <?php get_header(); ?>
    
      <h1 class="entry-title"><?php single_term_title(); ?></h1>
      <div class="archive-meta"><?php if ( '' != the_archive_description() ) { echo esc_html( the_archive_description() ); } ?></div>
    
      <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <article class="post">
          <?php if ( has_post_thumbnail() ) : ?>
            <div class="thumb">
              <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?></a>
            </div>
          <?php endif; ?>
          <div class="content">
            <h3><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
            <p class="excerpt-entry"><?php echo strip_tags( get_the_excerpt() ); ?></p>
          </div>
        </article>
      <?php endwhile; endif; wp_reset_postdata(); wp_reset_query(); ?>
    
      <h2>Actors</h2>
      <ul>
        <?php $queried_terms = get_field('actors'); if ($queried_terms): echo 'actors'; foreach($queried_terms as $query_term): setup_postdata($query_term); ?>
          <li><a href="<?php echo esc_url( get_term_link( $query_term ) ); ?>"><?php echo esc_html( $query_term->name ); ?></a></li>
        <?php endforeach; endif; wp_reset_postdata(); wp_reset_query(); ?>
      </ul>
    
    <?php get_footer(); ?>
    

    Has anyone run into this before? I suspect I need to somehow terminate the forloop query to allow ACF to work, but the normal elements recommended by WP aren’t working. I’m trying to build a tag page with posts and other useful information, so I assume it’s possible, but maybe I’m doing it wrong.

  • If actors is attached to a taxonomy, you need to reference the taxonomy in the get_field call for it. So it might be something like:

    get_field('actors','taxonomy_name_X');

    Where X is the ID of the taxonomy.

  • actors is a taxonomy, not a post type.

    foreach($queried_terms as $query_term):

    $query_term is a term in a taxonomy, not a post. You are attempting to set up the term as a post.

    setup_postdata($query_term);

    I have no idea what this is causing but it is likely a big part of the problem.

  • I reworked my template a bit, but still see the same error:

    <?php get_header(); ?>
    
      <h1 class="entry-title"><?php single_term_title(); ?></h1>
      <div class="archive-meta"><?php if ( '' != the_archive_description() ) { echo esc_html( the_archive_description() ); } ?></div>
    
      <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
        <article class="post">
          <?php if ( has_post_thumbnail() ) : ?>
            <div class="thumb">
              <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?></a>
            </div>
          <?php endif; ?>
          <div class="content">
            <h3><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
            <p class="excerpt-entry"><?php echo strip_tags( get_the_excerpt() ); ?></p>
          </div>
        </article>
      <?php endwhile; endif; ?>
    
      <h2>Actors</h2>
      <?php $get_actors = get_field('actors'); ?>
      <?php if( $get_actors ): ?>
        <ul>
          <?php foreach( $get_actors as $actors ): ?>
            <li><a href="<?php echo esc_url( get_term_link( $actors ) ); ?>"><?php echo esc_html( $actors->name ); ?></a></li>
          <?php endforeach; ?>
        </ul>
      <?php endif; ?>
    
    <?php get_footer(); ?>
    

    When I have nothing linked to the taxonomy, the ACF taxonomy data displays (example: https://a.cl.ly/X6uKqWrL).

    In the db query, I see the the following:

    
    SELECT t.term_id
    FROM wp_terms AS t
    INNER JOIN wp_term_taxonomy AS tt
    ON t.term_id = tt.term_id
    WHERE tt.taxonomy IN ('actor')
    AND t.term_id IN ( 403,404,284 )
    ORDER BY t.name ASC
    

    Those term IDs are correct. The full display of DQ queries also looks accurate (example: https://a.cl.ly/WnumRzdq).

    I take a single blog post, assign the shows taxonomy for “Broadchurch” for example (example: https://a.cl.ly/6qubm9kN) and save the post. When I refresh the taxonomy archive page, there are no queries at all for ACF, as shown here: https://a.cl.ly/P8u2ZvgJ.

    My ACF setup for the field is: https://a.cl.ly/GGu7qZL8 and the location rules are: https://a.cl.ly/8Luq2mdn

    I suspect that WP is overriding the ACF query entirely, or somehow preventing it from ever running in this instance where a post has a bunch of terms applied to it, but the term itself also has a term applied to it.

  • On your taxonomy archive, you’re not referencing your shows taxonomy in the get_field call for actors. So instead of just:

    <?php $get_actors = get_field('actors'); ?>

    This needs to be something like:

    <?php $get_actors = get_field('actors','shows_'. get_queried_object()->term_id); ?>

    You may need to edit the above if shows is your taxonomy slug for the taxonomy you attached the actors ACF field to. You just need a reference in that call to the term you’re pulling this data from and that’s placed in the get_field call as the second argument in the form of term-slug_term-id.

  • I altered my template per your suggestion @wpfieldwork to see what query would kick out of the db:

    
    SELECT option_value
    FROM wp_options
    WHERE option_name = '_shows_425_actors'
    LIMIT 1
    

    The only problem is that the database doesn’t have this option name set in wp_options. I searched for actor, show, etc, but nothing appears.

  • It wouldn’t be in the wp_options table. If you attach a field to a taxonomy term, it would be in the wp_termmeta table for that term. It would be the name of your field in the termmeta attached to the shows taxonomy term you adjusted the actors field for.

    Also the field in the database does not include the term-slug_term-id. It’s just the info you pass to the get_field ACF function to know where to find your ACF field’s data.

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

You must be logged in to reply to this topic.