Carbon Fields - Selected terms are not associated with a post

484 Views Asked by At

I'm starting to use Carbon Fields (with Wordpress) and I'm having a problem.

  1. I installed Wordpress v.6.0.1 on localhost (Windows 10, OpenServer, PHP 8.0, MySQl 8.0);
  2. Then I installed Carbon Fields using composer.
  3. I created several categories and one post.
  4. I created an association field using Carbon Fields to select post categories from the field
use Carbon_Fields\Container;
use Carbon_Fields\Field;

add_action( 'carbon_fields_register_fields', 'crb_attach_post_fields' );
function crb_attach_post_fields() {
  
  Container::make( 'post_meta', 'Post settings' )
      ->where( 'post_type', '=', 'post' )
      ->add_fields( array(
          Field::make( 'association', 'crb_category', 'Category' )              
              ->set_types( array(
                  array(
                      'type'      => 'term',
                      'taxonomy'  => 'category',
                  )
              ) )
      ));
}

add_action( 'after_setup_theme', 'crb_load' );
function crb_load() {
    require_once( 'vendor/autoload.php' );
    \Carbon_Fields\Carbon_Fields::boot();
}

image 1

  1. After selecting category 1 and saving the post, this category is not associated with the post. image 2 image 3

  2. If I select a category in the sidebar it works fine. image 4

  3. What is the problem? How to fix it?

1

There are 1 best solutions below

0
On

You are mixin two topics that could seem related:

  1. The categories on worpress
  2. The associated relations on CarbonFields.

The front side right panel on categories as you show in your image 4 is managed by WordPress along with the count column that you have established. Even though when you use the association carbon field with the categories term, you get the same list of calories that WordPress uses as well when you save your post the carbon fields store this relation in its own fields, so Worpress is not notified that you select these categories because to Wordpres is similar to other custom fields.

In summary, the category terms are store and management by WordPress and the association that you create with CarbonFields is management by yourself.