Support

Account

Home Forums Backend Issues (wp-admin) How to store custom taxonomies' values via ACF fields?

Solving

How to store custom taxonomies' values via ACF fields?

  • I have a custom post type, “Trade Alerts”, which is registered via code. It uses ACF fields with location set to that post type. All is good there. I’ve created three taxonomies for this CPT: Pair, Trade Status, and Trade Type. These taxonomies already exist as ACF fields mentioned above. What I am trying to do is use them as taxes instead of just custom fields.

    What I’ve done
    – I’ve registered all three as taxonomies via code.
    – I’ve set the code to show them in the admin columns
    – I’ve set them to visible in quick edit for those I wanted
    – I’ve used acf/load_field/ to populate terms from the custom taxonomy

    Where I’m stuck
    New meta boxes, one for each taxonomy, are now visible on the Trade Alerts edit page, but I want to use the already existing ACF fields to set these values for each post. I realize they can be hidden via screen options but that doesn’t solve it.

    I’ve got those fields being populated by the custom taxes, but the fields do not save with the post. It’s expecting the values from the new meta boxes – how do I get my new custom taxonomies’ values to be stored via the already existing ACF fields mentioned?

    Here is my code:

    Taxonomy: Trade Status

    function cptui_register_my_taxes_trade_status() {
    
    	/**
    	 * Taxonomy: Trade Status.
    	 */
    
    	$labels = [
    		"name" => __( "Trade Status", "Avada" ),
    		"singular_name" => __( "Trade Status", "Avada" ),
    	];
    
    	
    	$args = [
    		"label" => __( "Trade Status", "Avada" ),
    		"labels" => $labels,
    		"public" => true,
    		"publicly_queryable" => true,
    		"hierarchical" => false,
    		"show_ui" => true,
    		"show_in_menu" => true,
    		"show_in_nav_menus" => true,
    		"query_var" => true,
    		"rewrite" => [ 'slug' => 'trade_status', 'with_front' => true, ],
    		"show_admin_column" => true,
    		"show_in_rest" => true,
    		"rest_base" => "trade_status",
    		"rest_controller_class" => "WP_REST_Terms_Controller",
    		"show_in_quick_edit" => true,
    		"show_in_graphql" => false,
    	];
    	register_taxonomy( "trade_status", [ "tradealert" ], $args );
    }
    add_action( 'init', 'cptui_register_my_taxes_trade_status' );
    
    /**
     * Populate the ACF field with terms from the custom taxonomy Trade Status type.
     */
    add_filter( 'acf/load_field/name=trade_status', function( $field ) {
      
        // Get all taxonomy terms
        $trade_status_options = get_terms( array(
          'taxonomy' => 'trade_status',
          'hide_empty' => false
        ) );
        
        // Add each term to the choices array.
        // Example: $field['choices']['review'] = Review
        foreach ( $trade_status_options as $option ) {
          $field['choices'][$option->slug] = $option->name;
        }
      
        return $field;
      } );

    Taxonomy: Pair.

    function cptui_register_my_taxes_pair() {
    
    	/**
    	 * Taxonomy: Pair.
    	 */
    
    	$labels = [
    		"name" => __( "Pair", "Avada" ),
    		"singular_name" => __( "Pair", "Avada" ),
    	];
    
    	
    	$args = [
    		"label" => __( "Pairs", "Avada" ),
    		"labels" => $labels,
    		"public" => true,
    		"publicly_queryable" => true,
    		"hierarchical" => true,
    		"show_ui" => true,
    		"show_in_menu" => true,
    		"show_in_nav_menus" => true,
    		"query_var" => true,
    		"rewrite" => [ 'slug' => 'pair', 'with_front' => true,  'hierarchical' => true, ],
    		"show_admin_column" => true,
    		"show_in_rest" => true,
    		"rest_base" => "pair",
    		"rest_controller_class" => "WP_REST_Terms_Controller",
    		"show_in_quick_edit" => false,
    		"show_in_graphql" => false,
    	];
    	register_taxonomy( "pair", [ "tradealert" ], $args );
    }
    add_action( 'init', 'cptui_register_my_taxes_pair' );

    Taxonomy: Trade Types.

    function cptui_register_my_taxes_trade_type() {
    
    /**
     * Taxonomy: Trade Types.
     */
    
    $labels = [
        "name" => __( "Trade Types", "Avada" ),
        "singular_name" => __( "Trade Type", "Avada" ),
    ];
    
    $args = [
        "label" => __( "Trade Types", "Avada" ),
        "labels" => $labels,
        "public" => true,
        "publicly_queryable" => true,
        "hierarchical" => false,
        "show_ui" => true,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "query_var" => true,
        "rewrite" => [ 'slug' => 'trade_type', 'with_front' => true, ],
        "show_admin_column" => true,
        "show_in_rest" => true,
        "rest_base" => "trade_type",
        "rest_controller_class" => "WP_REST_Terms_Controller",
        "show_in_quick_edit" => true,
        "show_in_graphql" => false,
    ];
    register_taxonomy( "trade_type", [ "tradealert" ], $args );
    }
    add_action( 'init', 'cptui_register_my_taxes_trade_type' );
    
    /**
     * Populate the ACF field with terms from the custom taxonomy Trade Status type.
     */
    add_filter( 'acf/load_field/name=type', function( $field ) {
      
        // Get all taxonomy terms
        $trade_type_options = get_terms( array(
          'taxonomy' => 'trade_type',
          'hide_empty' => false
        ) );
        
        // Add each term to the choices array.
        // Example: $field['choices']['review'] = Review
        foreach ( $trade_type_options as $option ) {
          $field['choices'][$option->slug] = $option->name;
        }
      
        return $field;
      } );
    

    Trade Alert CPT

    function cptui_register_my_cpts_TradeAlert() {
    
        /**
         * Post Type: Trade Alerts.
         */
    
        $labels = [
            "name" => __("Trade Alerts", "Avada"),
            "singular_name" => __("Trade Alert", "Avada"),
            "menu_name" => __("Trade Alerts", "Avada"),
            "all_items" => __("All Trade Alerts", "Avada"),
            "add_new" => __("Add Trade Alert", "Avada"),
            "add_new_item" => __("Add New Trade Alert", "Avada"),
            "edit_item" => __("Edit Trade Alert", "Avada"),
            "new_item" => __("New Trade Alert", "Avada"),
            "view_item" => __("View Trade Alert", "Avada"),
            "view_items" => __("View Trade Alerts", "Avada"),
            "search_items" => __("Search Trade Alerts", "Avada"),
            "not_found" => __("No Trade Alerts Found", "Avada"),
            "not_found_in_trash" => __("No Trade Alerts found in Trash", "Avada"),
        ];
    
        $args = [
            "label" => __("Trade Alerts", "Avada"),
            "labels" => $labels,
            "description" => "",
            "public" => true,
            "publicly_queryable" => true,
            "show_ui" => true,
            "delete_with_user" => false,
            "show_in_rest" => true,
            "rest_base" => "",
            "rest_controller_class" => "WP_REST_Posts_Controller",
            "has_archive" => false,
            "show_in_menu" => true,
            "show_in_nav_menus" => true,
            "exclude_from_search" => false,
            "capability_type" => "post",
            "map_meta_cap" => true,
            "hierarchical" => false,
            "rewrite" => ["slug" => "trade-alert", "with_front" => false],
            "query_var" => true,
            "menu_icon" => "dashicons-location-alt",
            "supports" => [
                "title",
                "editor",
                "thumbnail",
                "excerpt",
                "revisions",
                "page-attributes",
                "custom-fields"
            ],
            "taxonomies" => [ "pair", "trade_status", "trade_type" ],
        ];
    
        register_post_type("Trade Alert", $args);
    }
    
    add_action('init', 'cptui_register_my_cpts_TradeAlert');
  • To remove the existing WP meta boxes for the taxonomies, when registering each taxonomy set meta_box_cb to false

    To use ACF to update the taxonomy fields set Save and Load terms settings on the taxonomy field to true.

  • Hi John,

    Setting the meta_box_cb to false worked, thank you for that.

    For the save and load terms settings on the taxonomy, I need to add to the code for the custom taxonomies which i did not create using ACF – they are just coded.

    I looked and found that these should be the same as the settings in ACF:

    'capabilities'      => array(
    			'manage_terms'  => 'manage_trade_status',
    			'edit_terms'    => 'edit_trade_status',
    			'delete_terms'  => 'delete_trade_status',
    			'assign_terms'  => 'assign_trade_status'
    		)

    So i have added the above to the code for one of the taxes, and it does show the value on the post front end, but still does not save the value as seen in the admin columns. What else can i do or try?

    When i tried disabling the fields that are populated by the taxonomy terms, and creating them via ACF (because the settings you mentioned are right there), it created other issues like only getting the ID, instead of the value, and that would require lots of other coding to be changed in other places.

    Thanks.

    function cptui_register_my_taxes_trade_status() {
    
    	/**
    	 * Taxonomy: Trade Status.
    	 */
    
    	$labels = [
    		"name" => __( "Trade Status", "Avada" ),
    		"singular_name" => __( "Trade Status", "Avada" ),
    	];
    
    	
    	$args = [
    		"label" => __( "Trade Status", "Avada" ),
    		"labels" => $labels,
    		"public" => true,
    		"publicly_queryable" => true,
    		"hierarchical" => false,
    		"show_ui" => true,
    		"show_in_menu" => true,
    		"show_in_nav_menus" => true,
    		"query_var" => true,
    		"rewrite" => [ 'slug' => 'trade_status', 'with_front' => true, ],
    		"show_admin_column" => true,
    		"show_in_rest" => true,
    		"rest_base" => "trade_status",
    		"rest_controller_class" => "WP_REST_Terms_Controller",
    		"show_in_quick_edit" => true,
    		"show_in_graphql" => false,
    		"meta_box_cb" => false,
    		'capabilities'      => array(
    			'manage_terms'  => 'manage_trade_status',
    			'edit_terms'    => 'edit_trade_status',
    			'delete_terms'  => 'delete_trade_status',
    			'assign_terms'  => 'assign_trade_status'
    		)
    	];
    	register_taxonomy( "trade_status", [ "tradealert" ], $args );
    }
    add_action( 'init', 'cptui_register_my_taxes_trade_status' );
    
    /**
     * Populate the ACF field with terms from the custom taxonomy Trade Status type.
     */
    add_filter( 'acf/load_field/name=trade_status', function( $field ) {
      
        // Get all taxonomy terms
        $trade_status_options = get_terms( array(
          'taxonomy' => 'trade_status',
          'hide_empty' => false
        ) );
        
        // Add each term to the choices array.
        // Example: $field['choices']['review'] = Review
        foreach ( $trade_status_options as $option ) {
          $field['choices'][$option->slug] = $option->name;
        }
      
        return $field;
      } );
  • I don’t know why you are adding this

    
    'capabilities'      => array(
    			'manage_terms'  => 'manage_trade_status',
    			'edit_terms'    => 'edit_trade_status',
    			'delete_terms'  => 'delete_trade_status',
    			'assign_terms'  => 'assign_trade_status'
    		)
    

    that has to do with the permissions for people to be allowed to do those things and depends on those capabilities existing. That is beyond what I was talking about.

    I thought that you were using an ACF taxonomy field. To use a select field or whatever type of choice field that “trade_status” represents than in order to have the post updated with the correct terms you would need to use wp_set_object_terms() when saving the post (acf/save_post). It would be easier to use a taxonomy field unless there is a reason not to, like your field being in a repeater.

  • I was mistakenly using that ^ which explains why it doesn’t work! Ha.

    I’ll look closer at using a taxonomy field, as you’re advice always steers me in the right direction.

    Thanks.

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

You must be logged in to reply to this topic.