0

I've found several other threads asking this same question, but for some reason I can't seem to make a solution work for me. For clarification, I want to have a custom field that is applied to a post but contains no value so that I can setup a template post (portfolio item in this case) and simply duplicate it around but leave out values where the field does not apply, and of course if there is no value I do not want the related html to show. So, here is what I have:

<?php $fbb_CurrentMetaSet = get_post_meta($post->ID, 'fbb_ProjectData_Process', false);
            if (!empty($fbb_CurrentMetaSet)){?>
            <div id="fbb_ProjectData_Process" class="fbb_ProjectDataSetSection">
                <div class="fbb_Title"><h5>Process:</h5></div>
                <div>
                        <?php foreach($fbb_CurrentMetaSet as $fbb_MetaDataSingle){
                            echo '<div>'.$fbb_MetaDataSingle.'</div>'; 
                        }?> 
                </div>
            </div>
            <?php } ?>

This code works if I simply check whether the custom field exists ( if ($fbb_CurrentMetaSet) ), but for some reason the !empty() method isn't working. Can anyone explain why? FYI I'm using the latest wordpress and the x-theme.

It may be that the larger context yields an explanation so below I've pasted the entire set of related code. My test code can be found in the last section. Once I've got it working I intend to duplicate it to the similar sections above it:

<div id="fbb_ProjectDataWrap">
            <div id="fbb_ProjectData_Client" class="fbb_ProjectDataSetSection">
                <div class="fbb_Title"><h5>Client:</h5></div>
                <ul>
                <?php $fbb_CurrentMetaSet = get_post_meta($post->ID, 'fbb_ProjectData_Client', false);?>
                    <?phpforeach($fbb_CurrentMetaSet as $fbb_MetaDataSingle){
                        echo '<li>'.$fbb_MetaDataSingle.'</li>'; 
                    }
                }?>
                </ul>
            </div>
            <div id="fbb_ProjectData_Tools" class="fbb_ProjectDataSetSection">
                <div class="fbb_Title"><h5>Tools:</h5></div>
                <ul>
                <?php $fbb_CurrentMetaSet = get_post_meta($post->ID, 'fbb_ProjectData_Tools', false); ?>                    
                    <?php foreach($fbb_CurrentMetaSet as $fbb_MetaDataSingle){
                        echo '<li>'.$fbb_MetaDataSingle.'</li>';
                    } ?>
                </ul>
            </div>
            <div id="fbb_ProjectData_About" class="fbb_ProjectDataSetSection">
                <div class="fbb_Title"><h5>About:</h5></div>
                <div>
                <?php $fbb_CurrentMetaSet = get_post_meta($post->ID, 'fbb_ProjectData_AboutTheProject', false); ?>                  
                    <?php foreach($fbb_CurrentMetaSet as $fbb_MetaDataSingle){
                        echo '<div>'.$fbb_MetaDataSingle.'</div>';
                    } ?>
                </div>
            </div>
            <?php $fbb_CurrentMetaSet = get_post_meta($post->ID, 'fbb_ProjectData_Process', false);
            if (!empty($fbb_CurrentMetaSet)){?>
            <div id="fbb_ProjectData_Process" class="fbb_ProjectDataSetSection">
                <div class="fbb_Title"><h5>Process:</h5></div>
                <div>
                        <?php foreach($fbb_CurrentMetaSet as $fbb_MetaDataSingle){
                            echo '<div>'.$fbb_MetaDataSingle.'</div>'; 
                        }?> 
                </div>
            </div>
            <?php } ?>
        </div>
  • use "var_dump" to look the value when "empty" doesn't work as expected – mmm Mar 15 '16 at 22:40
2

Your third parameter of get_post_meta() is set to false. This means it will return an array. Even though you didn't set a value for this custom field, it will still record an array element in the DB - so empty() will return false.

Try switching to true for your third param. That should return an empty string.

  • Thanks. That gave me the information that I was most easily able to implement. In my case the few fields that needed to support multiple entries were also one's that would always have at least one value, while the one's that might not always have a value were also one's that would only have one entry so changing the "false" to "true" created no conflict. The limitation with this approach is that it wont work for a scenario where I want to be able to check a field that might have more than one value. Not my current scenario so I wont worry about it for now. Thanks again. – fbb Mar 17 '16 at 16:20
1

I think we can do this in a bit better and safer. We can get all the post meta in one go (This is just DRY'ing up your code a bit, see this post on custom field performance).

We can try the following: (Code is commented as we go along, and note, all code is untested)

OPTION 1 - Calling get_post_meta() once

$meta   = get_post_meta( get_the_ID() );

// First make sure we have data before continuing
if ( $meta ) :
    // Get your field values and set defaults
    $fields            = [];
    $field_names       = ['Client', 'Tools', 'About', 'Process'];
    foreach ( $field_names as $field_name ) {
        if ( 'About' === $field_name ) {
            $name = 'AboutTheProject';
        } else {
            $name = $field_name;
        }
        $fields[$field_name] = ( isset( $meta['fbb_ProjectData_' . $name] ) )
            ? filter_var( $meta['fbb_ProjectData_' . $name][0], FILTER_SANITIZE_STRING ) 
            : '';
    }

    // Lets make sure we have at least one key/value pair with avalue
    if ( array_filter( $fields ) ) : ?>
        <div id="fbb_ProjectDataWrap">
        <?php // Lets loop through $fields and display them
        foreach ( $fields as $key=>$field ) : 
            // Make sure $field is not empty, if so, continue
            if ( !$field )
                continue;

            // We have values, lets display them
            ?>

            <div id="fbb_ProjectData_<?php echo $key; ?>" class="fbb_ProjectDataSetSection">
                <div class="fbb_Title">
                    <h5><?php echo $key; ?>:</h5>
                </div>
                <ul>
                    <li>
                        <div>
                            <?php echo $field; ?>
                        </div>
                    </li>
                </ul>
            </div>
        <?php endforeach; ?> 
        </div>
    <?php
    endi

f; endif;

OPTION 2 - Calling get_post_meta() for each key

Because custom fields are cached and do not decrease performance if we call multiple instances of get_post_meta() (see the linked post), we can call get_post_meta() for each key. Just remember to set $single to true

// Get your field values and set defaults
$fields            = [];
$field_names       = ['Client', 'Tools', 'About', 'Process'];
foreach ( $field_names as $field_name ) {
    if ( 'About' === $field_name ) {
        $name = 'AboutTheProject';
    } else {
        $name = $field_name;
    }
    $fields[$field_name] = filter_var( 
        get_post_meta( get_the_ID(), 'fbb_ProjectData_' . $name, true ), 
        FILTER_SANITIZE_STRING 
    );
}

// Lets make sure we have at least one key/value pair with avalue
if ( array_filter( $fields ) ) : ?>
    <div id="fbb_ProjectDataWrap">
    <?php // Lets loop through $fields and display them
    foreach ( $fields as $key=>$field ) : 
        // Make sure $field is not empty, if so, continue
        if ( !$field )
            continue;

        // We have values, lets display them
        ?>

        <div id="fbb_ProjectData_<?php echo $key; ?>" class="fbb_ProjectDataSetSection">
            <div class="fbb_Title">
                <h5><?php echo $key; ?>:</h5>
            </div>
            <ul>
                <li>
                    <div>
                        <?php echo $field; ?>
                    </div>
                </li>
            </ul>
        </div>
    <?php endforeach; ?> 
    </div>
<?php
endif;
  • Thanks for this. I think your answer ultimately aimed to solve my problem in the most complete way, however, the other answer answered my question more directly (why the code I was using wasn't working) and I was able to implement that information pretty easily. Unfortunately I was unable to implement your code. I kept getting syntax errors that didn't make sense to me... from what I could tell it seemed to like it should be working, but... perhaps when I'm more experienced I will be able to implement your kind of solution more easily. – fbb Mar 17 '16 at 16:11
  • Errors fixed. You must just make sure that you are using PHP 5.4+. If you're on a lower version of PHP, you will get a lot of fatal errors due to the short array syntax I'm using – Pieter Goosen Mar 17 '16 at 16:14

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.