How to display 'Association' field using Carbon Fields 3

1.1k views Asked by At

I have followed the documentation for Carbon Fields Association field but when I try to use the values, I get absolutely nothing. I have created the field in the back-end and everything seems to work nicely there, but on the front-end I'm using carbon_get_post_meta( $id, $name ); where $id is get_the_ID() and $nameis my field name related_products.

Could anyone please tell me how to utilise this field, or point me to anywhere that might help?

Thank you.

1

There are 1 answers

0
Nilambar Sharma On BEST ANSWER

In association fields, values are obtained in the following format. Example:

Array
(
    [0] => Array
        (
            [value] => post:post:11876
            [type] => post
            [subtype] => post
            [id] => 11876
        )
    [1] => Array
        (
            [value] => post:post:12101
            [type] => post
            [subtype] => post
            [id] => 12101
        )
)

Now you have the data, you can fetch it and display accordingly. Example.

This will fetch ids from the given array.

$ids = wp_list_pluck( $related_products, 'id' );

Now you can use those ids to fetch posts and display as you require.

$related_products_details = get_posts( array(
    'post__in' => $ids,
    ) );

Note: This is just a concept. You need to modify it as per your requirement.