0

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.

0

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

  • Hey, there! Thanks. I want to return the ID of the image attached to that post via ACF, just like get_post_thumbnail_id() returns the ID of the post thumbnail. I have tried both options you've presented before trying to combine both, haha, but none work. – Luiz Cruz Mar 12 at 20:36
  • @LuizCruz it all depends on how your field is defined and what are its settings set to... – Krzysiek Dróżdż Mar 12 at 20:38
  • probably. Isn't an ID set for every attachment? – Luiz Cruz Mar 12 at 20:40
  • @LuizCruz every attachment has an ID. But the ACF image field can have different result format (ID or array). But ACF is third party plugin and as such it’s off topic on this site. So from WP point of view everything should be fine if you’ll get the ID correctly. You should use one of ways from my answer - and read the article on Image field (link at the end of answer). – Krzysiek Dróżdż Mar 12 at 20:45
  • Yes, I see what you mean and the difference between functions get_field_object() and get_field(). I'll try to figure something out. Thanks! – Luiz Cruz Mar 12 at 20:50
0

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
    )
  )
);
  • I'm afraid it won't work, because it isn't even a correct PHP code - it will cause syntax error. – Krzysiek Dróżdż Mar 12 at 20:27
  • Just a sec, I'll fix it. Apparently trying to writing code on mobile isn't a great idea. – Antti Koskinen Mar 12 at 20:29
  • Yeah, writing code on mobile is really hard sometimes... :) – Krzysiek Dróżdż Mar 12 at 20:33
  • Hey, thanks. I also thought this would work but it doesn't. For some reason it won't read the result of $field_object['value'] correctly. – Luiz Cruz Mar 12 at 20:34
  • Interesting, I was just working with the same function earlier today with no problems. Does the get_field_object() return anything at all? Would explicitly setting the post id make it work? – Antti Koskinen Mar 12 at 20:38

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.