get_post_meta always returns an array

I’m using the following code to enable an if/else statement.

<?php
$types = array('.pdf', '.doc', '.xls');
$filename = array(get_post_meta($post->ID, 'mjwlink-url', TRUE));
if(0 < count(array_intersect(array_map('strtolower', $filename), $types))) {
   echo 'One';
} else {
   echo var_dump($filename);
}?>

The problem I have is that get_post_meta always returns an array in the format even when $single is set to true

Read More

array(1) { [0]=> string(34) "http://www.crimeandjustice.org.uk/" }

Any help appreciated.

Related posts

Leave a Reply

1 comment

  1. It always returns an array because you are executing the get_post_meta function within the array language construct. According to the WordPress Codex get_post_meta will not return an array if the third param is set to true. Therefore, swap:

    $filename = array(get_post_meta($post->ID, 'mjwlink-url', true));
    

    to

    $filename = get_post_meta($post->ID, 'mjwlink-url', true);
    

    Just out of curiosity, the logic within the if statement will only work on an array, if you remove the array construct your if statement will fail.