0

I know to get the ACF fields of a post it's self but I'm having trouble getting relationships within those results to return ACF fields as well.

example:

    $args = array(
        'post_type' => 'location',
        'post__in'  => array(67)
    );
    $q = new WP_Query($args);
    $locations = array();
    while($q->have_posts()) : $q->the_post();
        $p = get_post(); // gets the basic post details
        $f = get_fields(); // gets the ACF details
        $m = array_merge((array)$p, (array)$f); // merges all details into one array
        array_push($locations, $m);
    endwhile;

which shows something like

Array
(
    [0] => Array
        (
            [ID] => 67
            [ACF_Field => This is a custom field/value.
            ...
            [ACF_Relationship_Field] => Array
                (
                    [0] => WP_Post Object
                        (
                            [ID] => 133
                            // there should be another [ACF_Fiel]/value here
                            ...
                        )
                )
        )
)

So the first level of array has the ACF Values in it but you can see that the second level does not. I looked at the ACF Reference as that's helped me get to this point but unfortunately I haven't found anything that will pull in a relationships with ACF fields attached.

1

You need to loop for the results and call get_fields() again. So after your code add smt like:

foreach( $locations as $l ) {
   //grab post id
   $pid = $l['ACF_Relationship_Field'][0]->ID;
   // grab fields
   $fields = get_fields($pid);
   // add them back. As ACF_Relationship_Field is an object I will add it next
   // to it, but you can do whatever you prefer
   $l['ACF_Relationship_Field'][] = $fields;

}
  • I was hoping there would be a more seamless way but this works too. – Jacksonkr May 8 '15 at 0:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.