phpwordpressadvanced-custom-fieldsacfpro

How do I display the Category description field within the dropdown generated by ACF in my custom post type?


I'm using a custom post type to display a taxonomy category within itself, when editing a post from this type, and I would like the dropdown (or perhaps some other way to select) to be able to also display the Description field from the category so that the simple 'region 1', 'region 2' has an explanation as to what geographical areas are contained in those regions.

Setting up the taxonomy for the CPT is simple enough,

    $clubregions = array(
        'labels' => array(
            'name' => 'Club Regions',
            'singular_item' => 'Club Region',
            'add_new_item' => 'Add New Club Region',
            'edit_item' => 'Edit Club Region',
        ),
        'hierarchical' => true,
        'public' => true,
        'show_ui' => true,
        'show_admin_column' => true,
        'show_in_nav_menus' => false,
        'show_in_rest' => false,
        'show_tagcloud' => false,
    );
    register_taxonomy( 'clubregions', array( 'clubdetails' ), $clubregions );

but how do I get the page that ACF Pro generates to edit the custom post type with, to use the description field ?

screenshot of region dropdown generated by ACF


Now, you might think, "just save as draft, go back up to the CPT top level and click over to the category to read the descriptions", however we are additionally using Publishpress Capabilities Manager to have limited-access users for this custom post type, so they cannot see the category section from that level.


Solution

  • Cases like this one, where you need custom functionality beyond what ACF offers by default, are a great opportunity to create a new field type.

    ACF offers an official Field Type Template, available at github, to speed-up the development of new field types. This includes all the files you need and has great inline commenting that walks you through the process. It also contains all the possible functions you can use.

    There are also many great tutorials, like this one, on how to use this plugin to create your own custom fields.

    As a summary, you would need to create a custom method, eg. get_regions, to return all your available Regions and register that as field options. You would do that in the create_options() method like this:

    do_action('acf/create_field', array(
        'type'      =>   'select',
        'name'      =>   'fields['.$key.'][initial_value]',
        'value'     =>   $field['initial_value'],
        'choices'   =>   $this->get_regions()
    ));
    

    Then it will all come down to creating the control the user will actually see. This is done in the create_field() method. You'll need to create the HTML for your <select> field and that's where you can add more data for each option:

    function create_field( $field ) {
        $field = array_merge($this->defaults, $field);
        ?>
        <div>
            <select name='<?php echo $field['name'] ?>'>
                <?php
                    foreach( $this->get_regions() as $region ) :
                        // get your $description here
                ?>
                    <option <?php selected( $field['value'], $region ) ?> value='<?php echo $country ?>'>
                        <?php echo $region . ' - ' . $description; ?>
                    </option>
                <?php endforeach; ?>
            </select>
        </div>
        <?php
    }
    

    You can of course opt for a different element, like checkboxes or radio buttons, instead of a select.