0

I have a custom taxonomy page. This custom taxonomy has a few custom fields to populate it's template. A user is able to assign custom post types and media to this custom taxonomy and then those posts and media is displayed on the taxonomy archive page.

However, when a user is assigning my custom post types to the taxonomy, it causes the filled in custom fields to go blank on the taxonomy page. The custom post types are showing up though. The custom field values are still present in WP admin. When the custom post types are unassigned, the custom fields display on my page again. If I try to remove the WP_Query for the custom post types but DON'T unassign the custom post types, the custom fields are still blank.

So something is very clearly happening when I assign a custom post type to my custom taxonomy that is preventing me from displaying custom fields.

Here is a simplified version of my code.

taxonomy.php

<?php if (get_field('subtitle')) { ?>
    <p><?php the_field( 'subtitle' )?></p>
<?php } ?>
<?php
    $post_query = new WP_Query(array(
        'post_type' => ['release'],
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'orderby' => 'title', 
        'order' => 'ASC',
        'tax_query' => array(
            array(
            'taxonomy' => 'mk',
            'field' => 'slug',
            'terms' => $post_slug,
            )
        )
    ));

    if ( $post_query->have_posts() ) {
        while ( $post_query->have_posts() ) {
            $post_query->the_post();
            the_content(); 
        }
    } else {
        echo "No posts.";
    }

    wp_reset_query();
?>

functions.php

// Custom post types and taxonomies
add_action( 'init',  function () {
  // For custom post type
  register_post_type( 'release', array(
    'labels'        => array( ),
    'description'   => 'Press releases',
    'public'        => true,
    'menu_position' => 6,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'revisions' ),
    'has_archive'   => true,
    'show_in_rest'  => true,
    'menu_icon'     => 'dashicons-nametag',
    'taxonomies'    => array('tags', 'category', 'mk' ),
  )); 

  // For taxonomy
    register_taxonomy( 'mk', ['attachment', 'release', 'fact'], array(
        'labels'            => array(),
        'show_in_rest'      => true,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'mk' ),
  ));
});


// Custom fields for post types and taxonomies
add_action('acf/init', function () {

  // For media kit taxonomy
  acf_add_local_field_group(array(
    'key' => 'group_64dce26636860',
    'title' => 'Media Kit',
    'fields' => array(
      array(
        'key' => 'field_64dce2889cb6e',
        'label' => 'Hero Subtitle',
        'name' => 'subtitle',
        'type' => 'text',
        'instructions' => 'This optional subtitle will appear below page title.',
        'required' => 0,
        'conditional_logic' => 0,
        'wrapper' => array(
          'width' => '',
          'class' => '',
          'id' => '',
        ),
        'default_value' => '',
        'placeholder' => '',
        'prepend' => '',
        'append' => '',
        'maxlength' => '',
      ),
    ),
    'location' => array(
      array(
        array(
          'param' => 'taxonomy',
          'operator' => '==',
          'value' => 'mk',
        ),
      ),
    ),
    'menu_order' => 0,
    'position' => 'normal',
    'style' => 'default',
    'label_placement' => 'top',
    'instruction_placement' => 'label',
    'hide_on_screen' => array(
      0 => 'discussion',
    ),
    'active' => true,
    'description' => '',
    'show_in_rest' => 0,
  ));
});
1
  • Welcome! Questions about third-party plugins (such as Advanced Custom Fields) are off-topic here, and should be asked with the people who know it best, on ACF's support page.
    – YourManDan
    Aug 18, 2023 at 15:08

1 Answer 1

0

I ended up solving this on my own and thought I would come back to show my solution. On my taxonomy page I needed to get the current taxonomy term like so:

<?php $term = get_queried_object();
if (get_field('subtitle', $term)) { ?>
    <p><?php the_field( 'subtitle', $term )?></p>
<?php } ?>

Here is the documentation page: https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.