-3

I have an array being created within another array in Wordpress and I need to get the ID value from each Array.

This is the code I am using to pull the array

<?php $pages = get_field('products');
get_field('product_id', $pages->ID); ?>

If I use print_r($pages) I get the following output:

Array
(
    [0] => Array
        (
            [product_id] => WP_Post Object
                (
                    [ID] => 35238
                    [post_author] => 13
                    [post_date] => 2014-06-05 15:33:27
                    [post_date_gmt] => 2014-06-05 15:33:27
                    [post_content] => 40G parallel optics transceivers (40GBASE-SR4) ...
                    [post_title] => 40G QSFP+ (MTP/MPO) to 4x10G SFP+ (8xLC) Assembly 
                    [post_excerpt] => 
                    [post_status] => publish
                    [comment_status] => closed
                    [ping_status] => closed
                    [post_password] => 
                    [post_name] => 40g-qsfp-mpomtp-4x10g-sfp-8xlc-assembly
                    [to_ping] => 
                    [pinged] => 
                    [post_modified] => 2014-07-01 08:34:38
                    [post_modified_gmt] => 2014-07-01 08:34:38
                    [post_content_filtered] => 
                    [post_parent] => 36259
                    [guid] => http://www.fibrefab.com/?page_id=35238
                    [menu_order] => 0
                    [post_type] => page
                    [post_mime_type] => 
                    [comment_count] => 0
                    [filter] => raw
                )

        )

    [1] => Array
        (
            [product_id] => WP_Post Object
                (
                    [ID] => 34484
                    [post_author] => 13
                    [post_date] => 2014-06-05 15:16:04
                    [post_date_gmt] => 2014-06-05 15:16:04
                    [post_content] => MTP/MPO QSFP+ trunk/ patchcord assemblies are ...
                    [post_title] => 40G QSFP+ (MTP/MPO) to QSFP+ Assembly
                    [post_excerpt] => 
                    [post_status] => publish
                    [comment_status] => closed
                    [ping_status] => closed
                    [post_password] => 
                    [post_name] => mtpmpo-microcable-qsfp-40gb-trunk-patchcord-assembly
                    [to_ping] => 
                    [pinged] => 
                    [post_modified] => 2014-07-01 08:34:24
                    [post_modified_gmt] => 2014-07-01 08:34:24
                    [post_content_filtered] => 
                    [post_parent] => 36259
                    [guid] => http://www.fibrefab.com/?page_id=34484
                    [menu_order] => 0
                    [post_type] => page
                    [post_mime_type] => 
                    [comment_count] => 0
                    [filter] => raw
                )

        )

)

How I display the [ID] from each of these arrays?

1

It looks like you are using Advanced Custom Fields. Your "Product ID" is storing the object, instead of just the ID. Below shows you how you can access the post ID, otherwise just change your field saving option for Product ID.

It's a simple foreach statement.

foreach( $pages as $page ) {
    echo $page['product_id']->ID;
}
| improve this answer | |

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