Accessing values from ACF sub field (flexible content area) in PHP

Question

I’m trying to access the values of a ACF sub field using PHP.

$articles = get_posts(
 array(
  'posts_per_page' => -1,
  'post_status' => 'publish',
  'post_type' => array('venues', 'suppliers'),
 )
);


$locations = array();
foreach ($articles as $article) {

    $field = get_field('info_items', $article->ID, false);

    if( have_rows('info_items') ):
        while( have_rows('parent_field') ): the_row();

        $value = get_sub_field('info_location');

        endwhile;

    endif;
    var_dump($value);
    exit();
}

In the code above if I var_dump the contents of $field I get the array showing all the subfields – including the one I want:

array(3) { [0]=> array(2) { [“acf_fc_layout”]=> string(13)
“info_location” [“field_59ea62cf04adf”]=> string(16) “Manchester” }
[1]=> array(2) { [“acf_fc_layout”]=> string(10) “info_phone”
[“field_59ea634904ae3”]=> string(11) “08009788221” } [2]=> array(3) {
[“acf_fc_layout”]=> string(12) “info_website”
[“field_59ea635c04ae5”]=> string(32) “https://www.stuffandstuff.co.uk
[“field_5c6802013bb34”]=> string(0) “” } } NULL

I’m trying to retrieve the ‘info_location’ value – in this example the string ‘Manchester’.

The have_rows loop is from the ACF website docs for get_sub_field but doesn’t work in this context.

0
, Steve 2 years 2019-12-30T08:38:49-05:00 0 Answers 90 views 0

Leave an answer

Browse
Browse