Support » Plugin: Carbon Fields » Accessing data from complex fields

  • Resolved Sean T.

    (@tinothepro)


    Hi,
    Since version 2 was introduced I am unable to access any of my meta data that was created from my complex field in version 1.

    Using the example from the docs:

    Container::make( ‘post_meta’, ‘Slider Data’ )
    ->where( ‘post_type’, ‘=’, ‘page’ )
    ->add_fields( array(
    Field::make( ‘complex’, ‘slides’ )->add_fields( array(…..

    I was able to just do something like:
    $data = carbon_get_post_meta( get_the_ID(), ‘slides’, ‘complex’ );
    print_r($data) or var_dump($data) to get the array of information and go from there. Now all my complex fields return false. Can someone tell me how to accurately grab all of the info from a complex field? Multiple group complex preferably.

    Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • I’ve also tried my best to use the :

    https://carbonfields.net/docs/advanced-topics-field-name-patterns/?crb_version=2-0-0

    But I still get …./functions.php:256:boolean false from my var_dump() on any combination of pattern I try. I’ve tried all the ones in the documentation. What I’ve tried are things like:

    Container::make( 'post_meta', 'Slider Data' )
        ->where( 'post_type', '=', 'page' )
        ->add_fields( array(
          Field::make( 'complex', 'crb_services' )
        ->add_fields( 'digital', array(
            Field::make( 'text', 'name' ),
        ) )
        ->add_fields( 'physical', array(
            Field::make( 'textarea', 'name' ),
        ) )
        ));

    var_dump(carbon_get_post_meta(get_the_ID(), 'crb_services[0]:digital/name'));

    Plugin Contributor Atanas Angelov

    (@atanasangelovdev)

    Hi @tinothepro ,

    Can you paste all database meta keys (and their values) for the post in question that start with “_crb_services” ?

    Note that advanced features such as queries or fetching nested field values on legacy data is not supported (legacy data is automatically converted to “modern” data when you save it – there is not migration script at this time). Getting the root complex field value is NOT an advanced feature so there may be an underlying issue here.

    Hi @atanasangelovdev,

    Here is a screenshot of the database records:

    https://ibb.co/diH2ra

    I have created 2 digital fields and 1 physical.

    Here’s an example of how the data is stored differently:

    meta key: content_1_text
    meta key: _sections|content_section_text|1|0|value

    By following the documentation I should be able to access the data using complexfield[groupnumber]:namedgroup/field if I understand correctly. There are no examples of retrieving complex data on the site.

    By saying nested field values aren’t able to retrieve legacy data, does that mean all of business I have done using CF version < 2 will not be able to utilize version 2 at all?

    To install carbon fields I have ran the composer script on the directory WordPress is installed. I am able to retrieve values using the get_post_meta() WP function but not able to use any carbon_get_* . Thanks for your help.

    • This reply was modified 2 years, 3 months ago by Sean T..
    Plugin Contributor Atanas Angelov

    (@atanasangelovdev)

    Your database records look OK but I noticed you said your var_dump was in functions.php – is it inside a hook or directly in functions.php? Note that you cannot pull field values before Carbon_Fields::boot() is called so if you call boot() in a hook and then attempt to pull a value before that hook has executed you will not get the results you expect.

    To test if this is the case try calling the carbon_get_post_meta() function in your header.php, for example.

    > … will not be able to utilize version 2 at all?

    CF 2 will pull legacy data but it will not be able to make complex queries based on legacy data, for example. The database records you showed are in 2.0 format so you should not have problems with that.

    @atanasangelovdev ,

    I am currently using the Genesis Framework so I don’t work in multiple templates. Everything is primarily done using hooks so here is the code in my functions.php.

    use Carbon_Fields\Container;
    use Carbon_Fields\Field;
    
    add_action( 'after_setup_theme', 'crb_load' );
    function crb_load() {
        require_once( 'carbon-fields/vendor/autoload.php' );
        \Carbon_Fields\Carbon_Fields::boot();
    }
    
    // Add carbon fields
    add_action('carbon_fields_register_fields', 'crb_register_custom_fields');
    function crb_register_custom_fields() {
    // You can assume these are correctly defined Atanas
      include_once(dirname(__FILE__) . '/includes/hero.php');
    }
    
    // Display hero
    add_action('genesis_header', 'add_hero');
    function add_hero() { ?>
      <div class="hero-content">
        <div class="wrap">
          <?php
          if (carbon_get_post_meta(get_the_ID(), 'hero_headline')) { ?>
            <h1><?php echo carbon_get_post_meta(get_the_ID(), 'hero_headline'); ?></h1>
          <?php }
          if (carbon_get_post_meta(get_the_ID(), 'hero_teaser')) { ?>
            <p><?php echo carbon_get_post_meta(get_the_ID(), 'hero_teaser'); ?></p>
          <?php } ?>
        </div>
      </div>
      <?php }
    Plugin Contributor Atanas Angelov

    (@atanasangelovdev)

    The code you’ve sent uses different fields than the one you mentioned earlier and it does not include the var_dump you also mentioned.

    I can’t really debug remotely based on tickets with bits and pieces of the information – I will need a reproduce case in order to investigate further.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Accessing data from complex fields’ is closed to new replies.