Support

Account

Home Forums General Issues get_fields stops working when taxonomy term used in custom post type

Helping

get_fields stops working when taxonomy term used in custom post type

  • hey,

    I have done the following –

    1) I created a custom post type called ‘courses’. i created some course content but didnt add any categories.
    2) I created a taxonomy for post type called ‘course_categories’.
    These were both created by custom post type UI.

    3) I create some a field group called ‘course categories’ and adding 3 image fields and a text field.
    4) I added the rule ‘Show this field group if Taxonomy term is equal to ‘Course Categories’.
    5) I then added content / images to the taxonomies.
    6) In the template file – taxonomy_course-categories.php, I displayed the field group content using lines like –
    <div class=”page-banner” style=”background: url(‘<?php echo get_field(‘hero_image’)[‘url’]; ?>’) no-repeat center”>
    <p><?php echo get_field(‘introduction’);?></p>

    This all displayed as expected. However..
    7) I then added a ‘course_categories’ selection to the ‘courses’ I had created. For any ‘course_category’ that has a count of ‘courses’ greater than 0, the get_field() calls in the ‘taxonomy_course-categories’ no longer work (and I checked inside get_fields and the data is no longer available).
    8) Mind blown, that makes no sense to me. Have you seen this before? So in conclusion get_fields works fine until the taxonomy is associated with a custom post type and then it no longer works. This seems totally unrelated to me i.e. on different pages.

  • Can you explain more what you mean by this statement?

    “7) I then added a ‘course_categories’ selection to the ‘courses’ I had created.”

    What do you mean by ‘selection’? Radio buttons? A Select drop-down?

    Just off the top of my head, if you are adding a taxonomy to your custom post type, you don’t need to add an additional ACF selection field, especially with the same slug/name. This will create unexpected results.

    Once you’ve created the custom taxonomy, you can use the built-in WordPress support for taxonomies.

    In any case, you have two options so pick one but you shouldn’t really use both:

    1) If you want to use your custom taxonomy, make sure your cpt supports the custom taxonomy and vice-versa that your custom taxonomy is attached to your custom post type.

    Then, make sure your edit screen for your custom post type has your taxonomy box checked in ‘Screen Options’. It should look just like the category box for regular Posts.

    Select your taxonomies for your custom post type posts using that box (no ACF select field necessary).

    — OR —

    2) Use an ACF select field (either Select or Checkbox) and then you can query posts by that field (scroll down to Examples): https://www.advancedcustomfields.com/resources/query-posts-custom-fields/

    Thus, you should either use your custom taxonomy or an ACF field but not both – you are just re-selecting something that is already there.

    All that said, there could be uses for grabbing your taxonomies in ACF but make sure that your ACF fields do not have the same slugs as your custom taxonomy.

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

You must be logged in to reply to this topic.