Home Forums Feature Requests Passing multiple field IDs with get_field().


Passing multiple field IDs with get_field().

  • I’ve done a bit of searching on Google with various search terms and haven’t managed to find any mention of this being possible.

    What I’d like to be able to do is pass multiple field IDs to get_field to prevent having to AND/OR them all in sequence.

    Let’s say we only want to show div for social icons if any social networks are configured, and only want to show each icon if that specific network is configured. First we’d check if any of them exist…

    if( get_field('twitter', 'option') || get_field('facebook', 'option') || etc )

    …and if true we start the div and then check each one individually (which is fine, we can just create an array of networks and foreach it).

    The issue comes when there’s a lot of configurable social networks; the line above then becomes a huge statement of ORs. It would be useful if it were possible to provide multiple IDs. For example:

    if( get_field(['twitter','facebook',etc], 'option') )

    or pre PHP5.5:

    if( get_field(array('twitter','facebook',etc), 'option') )

    I understand that it wouldn’t be possible for the_field() though as that skips the step where we decide which value to output if multiple are returned.

  • Hi @chrisdunnbirch

    Thanks for the feature request. I’ll consider adding this in, but for now, you could quickly write something like this:

    $social = array();
    foreach( array('facebook', 'twitter', 'etc') as $k ) {
    	$v = get_field($k, 'option');
    	if( $v ) {
    		$social[ $k ] = $v;
    if( !empty($social) ) {
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.