0

When I do get_post_meta($post->ID, "company_wp_box_g", false) I am getting back an array of key/values pairs.

array (size=1)
      0 => 
        array (size=7)
          'f_name' => string 'John Doe' (length=14)
          'f_company_name' => string 'Something Ltd' (length=0)
          'f_description' => string '' (length=0)
          'f_phone' => string '0208 992 7222' (length=13)
          'f_cell_phone' => string '07809775200' (length=0)
          'f_email' => string 'jonh.doe@gmail.com' (length=0)
          'f_web' => string 'doe.com' (length=0)

How to get a single value for example f_name?

just FYI everything is taking place inside foreach loop:

$args = array(
    'numberposts' => -1,
    'post_type' => 'company'
);

$posts = get_posts($args);  


foreach ($posts as $post){ 

    // do stuff

}
5

If every meta value has the key f_name

$meta = get_post_meta($post->ID, "company_wp_box_g", true);
echo $meta['fname'];

If you're not sure of the contents & want to search for the first one available

$name = false;
$meta = get_post_meta($post->ID, "company_wp_box_g", false);
foreach($meta as $array) {
    if(isset($array['f_name'])) {
        $name = $array['f_name'];
        break;
    }
}
echo $name;
  • 1
    is it possible to do this without using variable? get_post_meta($post->ID, "company_wp_box_g", true)->key ?? – Iladarsda Oct 25 '12 at 19:33
  • 2
    You can't use get_post_meta($post->ID, "company_wp_box_g", true)->key since the return value from the function is not an object but an array but you can probably typecast it to object & then use it ((object)get_post_meta($post->ID, "company_wp_box_g", true))->key – Mridul Aggarwal Oct 25 '12 at 19:36

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.