Need to access the last ACF Flexible Content Field?

May 15, 2017

So, some developers would just list them all and hide all but the last one. That works fine, but if the field volume gets pretty large you will have a heavy page load even though you only want to show a single field. To access the last (usually most recent) ACF Flexible Content Field by itself you have to look at is as an array. The array would start at zero and count to as many flex fields that you have. The tough part is not always knowing how many fields you may have for a particular post/page and if you just want to access the most recent field it could be a guessing game.

My solution is simple. Using basic php you can access the Flex Content section directly as an array. Reverse the array which would then make the most recent flex field the first item. Then use that to get the sub field(s) needed from said flex field.


/* Reverse the Flex Content field array */
$arrayReverse = array_reverse(get_field('flex_content_section'););

/* Pull, the now, first item in the array which will be the most recent flex field */
$lastFlexField = reset($arrayReverse);

/* Grab a sub item from the recent Flex Content Field */
$flexFieldItem = $lastFlexField['sub_field'];

Stay in Touch!

Subscribe to our newsletter.

Solutions Architecture

browse through our blog articles

Blog Archive