I want to exclude two items (using their IDs) from an array that's retrieving images attached to the post: the post's thumbnail (featured image) and an image attached via Advanced Custom Fields.
I'm getting the first one's ID using get_post_thumbnail_id()
and this last one's ID using get_field_object('icon')
, where 'icon'
is the $selector
of the custom field (essentially an image, that's being used as an icon). If you're not familiar with ACF's Documentation the relevant part about this is here.
I've scrapped everything irrelevant for demonstration purposes. This is what I have and where [I believe] my problem is at:
$var = get_children(array(
'exclude' => array(
get_post_thumbnail_id(),
get_field_object('icon')['ID'])
));
I'm thinking the result of get_post_thumbnail_id
is an integer, so it works perfectly, but the result of get_field_object['ID']
is a string and that's why it doesn't work. If I could use something like echo
it would probably work.
I would love to understand where it's wrong and how to make this work, please.
I want to exclude two items (using their IDs) from an array that's retrieving images attached to the post: the post's thumbnail (featured image) and an image attached via Advanced Custom Fields.
I'm getting the first one's ID using get_post_thumbnail_id()
and this last one's ID using get_field_object('icon')
, where 'icon'
is the $selector
of the custom field (essentially an image, that's being used as an icon). If you're not familiar with ACF's Documentation the relevant part about this is here.
I've scrapped everything irrelevant for demonstration purposes. This is what I have and where [I believe] my problem is at:
$var = get_children(array(
'exclude' => array(
get_post_thumbnail_id(),
get_field_object('icon')['ID'])
));
I'm thinking the result of get_post_thumbnail_id
is an integer, so it works perfectly, but the result of get_field_object['ID']
is a string and that's why it doesn't work. If I could use something like echo
it would probably work.
I would love to understand where it's wrong and how to make this work, please.
The problem is that you use wrong ACF function.
get_field_object
returns the settings of a specific field. Each field contains many settings such as a label, name and type. This function can be used to load these settings as an array along with the field’s value.
So get_field_object('icon')['ID'])
doesn't return the ID of image attachment, but ID of that ACF field.
What you want to use is:
$var = get_children(array(
'exclude' => array(
get_post_thumbnail_id(),
get_field('icon')
)
));
If your field returns the ID of selected image, or
$image = get_field('icon');
$var = get_children(array(
'exclude' => array(
get_post_thumbnail_id(),
$image['ID']
)
));
if it returns an array.
More on get_field
for Image field
The ACF function returns an array, so would this work?
$field_object = get_field_object('icon'); // add post id as 2nd param if needed
$var = get_children(
array(
'exclude' => array(
get_post_thumbnail_id(),
$field_object['value'] // the saved value of the field, type cast to int if needed
)
)
);