0

I'm trying to get ID from a relationship field within a flexible content and push it in a array

if (have_rows('studio_blocs')) {
    while(have_rows('studio_blocs')) {
        the_row();
        $news = get_sub_field('studio_bio_list');

        if ($news) {
            foreach($news as $item) {
                $array[] = $item;
            }
        }
    }
    print_r($array);
}

For the moment I got this output, can I have some help to get the ID and push it in my array please ?

Array
(
    [0] => 
    [1] => WP_Post Object
        (
            [ID] => 88
            [post_author] => 1
            [post_date] => 2019-11-07 14:52:40
            [post_date_gmt] => 2019-11-07 13:52:40
            [post_content] => 
            [post_title] => Sébastien Arkange
            [post_excerpt] => 
            [post_status] => publish
            [comment_status] => closed
            [ping_status] => closed
            [post_password] => 
            [post_name] => sebastien-arkange
            [to_ping] => 
            [pinged] => 
            [post_modified] => 2019-11-07 14:53:27
            [post_modified_gmt] => 2019-11-07 13:53:27
            [post_content_filtered] => 
            [post_parent] => 0
            [guid] => http://dev.mywebsite.com/?post_type=collaborateurs&p=88
            [menu_order] => 8
            [post_type] => collaborateurs
            [post_mime_type] => 
            [comment_count] => 0
            [filter] => raw
        )

)
0

Editing the custom field itself would be the easiest way. In the field, under "Return Format," you can change it from Post Object to Post ID. That way, when you call for the data, you'll already have an array of just the IDs.

If you're not able to change the field for some reason, you could alternatively code it something like

<?php
if (have_rows('studio_blocs')) {
    while(have_rows('studio_blocs')) {
        the_row();
        $news = get_sub_field('studio_bio_list');
        if ($news) {
            foreach($news as $item) {
                $array[] = $item->ID;
            }
        }
    }
    print_r($array);
}
?>

but you're adding a little processing overhead, so changing the field is the better solution.

  • That's work, thank you for your help – Jandon Nov 8 at 19:04

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.