0

I'm working with custom post types. In the post type awards I have a relationship field that links each award to a post from the post type project.

I'm using the following code to return the values from this field - it should not return anything if $work_value is empty:

<?php 
$work_value = get_post_meta($post->ID, 'work', false );
// check if the custom field has a value
if( ! empty( $work_value ) ) {
    foreach ( $work_value as $work ) { 
        //get id for related post and put in ID
        $id = $work[ 'ID' ];
        //show the related post name as link
        echo 'for <a href="'.get_permalink($id).'">'.get_the_title( $id ).'</a> ';
    } //end of foreach
} //endif ! empty ( $work_value )
?>

However, if the field is empty (and I checked, it really is empty) it outputs the permalink and title of the current post (from post type awards). How can I solve this? Thanks!

  • What is var_dump($work_value); right before the if conditional? – s_ha_dum Feb 23 '14 at 0:56
  • array(1) { [0]=> bool(false) } // if it has a value it's: array(1) { [0]=> array(23) etc etc – Mia Feb 23 '14 at 16:17
1

get_post_meta() should only return false under a couple of circumstances:

269   function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) {
270           if ( !$meta_type )
271                   return false;
272   
273           if ( !$object_id = absint($object_id) )
274                   return false;

https://core.trac.wordpress.org/browser/tags/3.8.1/src/wp-includes/meta.php#L269

I don't see how $meta_type could be wrong since it is hard-coded into get_post_meta():

1769  function get_post_meta($post_id, $key = '', $single = false) {
1770          return get_metadata('post', $post_id, $key, $single);
1771  }

https://core.trac.wordpress.org/browser/tags/3.8.1/src/wp-includes/post.php#L1769

That leaves $object_id. Something could be passing an bad object ID-- something like this...

$object_id = 'abc';
// or this
// $object_id = -123;
var_dump(!$object_id = absint($object_id));

... will evaluate true causing get_post_meta to return false.

Another possibility is that a filter is returning a problematic value here:

276           $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single );
277           if ( null !== $check ) {
278                   if ( $single && is_array( $check ) )
279                           return $check[0];
280                   else
281                           return $check;
282           }

If some filter sets $check to an array with false as the only element, then null !== $check is true and that array gets returned. For example:

add_filter(
  'get_post_metadata',
  function($meta) {
    return array(false);
  }
);
$t = get_post_meta(1,'_edit_lock_dood',false);
var_dump($t,empty($t));

Note: simply returning false from that filter won't do it. It must be an array with false as the single element, which gets to the technical reason your code doesn't work.

empty(false) is true but empty(array(false)) is false-- the array is not empty. It has an element, even though that element is false. Play with var_dump() a bit and you can demonstrate it for yourself. Like this, for example:

$a = array(false);
var_dump($a);
var_dump(empty($a));

There is not enough context to your question/code for me to narrow it down further.

  • Thanks. Most of this I don't really understand. But I don't think I have anything you mentioned that would interfere like this. I only set up pre_get_posts but no filters. The parent theme uses filters involving get_post_meta but it's set to true. I created my custom post types via Pods. I just asked this in their forum. Maybe it's pods related. – Mia Feb 23 '14 at 21:18
  • "The parent theme uses filters involving get_post_meta but it's set to true." -- that sounds like you do have something that could interfere like this. – s_ha_dum Feb 23 '14 at 21:42
  • I mean it uses it to determine whether to display sidebar or not. Just the regular code setup. I should maybe also say that this is the only instance where this happens. I'm using a ton of get_post_meta fields and none of them give me this problem. Usually like this: $win_value = get_post_meta($post->ID, 'win', true ); – Mia Feb 24 '14 at 7:50
  • Checked again. Found this in parent theme's functions.php pastebin.com/dkEYagxG – Mia Feb 24 '14 at 9:12
  • Sorry, I don't see the connection. Is that code used in relation to the code in your question? If so, how? – s_ha_dum Feb 24 '14 at 14:00
0

I don't know if I'll find out what caused this. Thank you so much for your help! In the meantime I realized I can use this:

<?php 
$work_value = get_post_meta($post->ID, 'work', false );
// check if the custom field has a value
    foreach ( $work_value as $work ) { 
        if(is_array($work)) {
        //get id for related post and put in ID
        $id = $work[ 'ID' ];
        //show the related post name as link
        echo 'for <a href="'.get_permalink($id).'">'.get_the_title( $id ).'</a> ';
        }
    } //end of foreach

?>

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.