carbon-fields icon indicating copy to clipboard operation
carbon-fields copied to clipboard

Programatically updating association field values

Open boquiabierto opened this issue 7 years ago • 3 comments

From the docs I’m not able to setup the {key} and {value} for the carbon_set_{container_type} ( {id}, {key}, {value} ); function. When I currently output an entry out off the association field array it looks like this (case for associated term):

array(
    'value' => term:{taxonomy}:{id}
    'type' => {term}
    'subtype' => {taxonomy}
    'id' => {id}
)

Is it possible to update association fields? How would it look like? Thanks!

P.S. I need this to sync associations fields between the associated terms. In version 1.6 it was a unique entry with an array and building and updating was easy.

Version

  • Carbon Fields: 2.2
  • WordPress: 4.9+
  • PHP: 7+

boquiabierto avatar Mar 28 '19 12:03 boquiabierto

You could do something like this:

carbon_set_post_meta(
            ID_OF_THE_POST,
            'seminar',
            array(
                array(
                    'value' => 'post:seminar:' . ID_OF_THE_ASSOCIATION,
                    'id' => ID_OF_THE_ASSOCIATION,
                    'type' => 'post',
                    'subtype' => 'seminar'
                )
            )
        );

In this case seminar is the post-type.

AnagramEngineering avatar May 23 '19 06:05 AnagramEngineering

Thanks @AnagramEngineering, this is what we finally did, forgot to update our research here. It works also with terms: 'value' => 'term:' . ID_TERM_TAXONOMY . ':' . ID_ASSOCIATED_TERM, and users: 'value' => 'user:user:' . ID_ASSOCIATED_USER, setting 'type', 'subtype' and 'id' accordingly.

boquiabierto avatar May 24 '19 07:05 boquiabierto

What if the association is within a complex field. How would it look like then?

jesperkraght avatar Mar 09 '20 09:03 jesperkraght