Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I know how to get the value of a custom field, using code like get_post_meta($post->ID, 'company', true);. Is there a way to get the name of the custom field?

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

to get a post custom fields name you can use get_post_custom

eg:

$post_meta = get_post_custom($post->ID);
foreach ( $post_meta as $key => $value )
    echo $key . " => " . $value . "<br />";

this will print a list of custom_field_name => custom_field_value

share|improve this answer
    
+1 for clear answer.:) –  Ramkumar Jul 1 '11 at 6:24
add comment

if you only want to get the name, you can use get_post_custom_keys

<?php

  $custom_field_keys = get_post_custom_keys();
  foreach ( $custom_field_keys as $key => $value ) {
    $valuet = trim($value);
      if ( '_' == $valuet{0} )
      continue;
    echo $key . " => " . $value . "<br />";
  }
?>

prints something like this:

0 => mykey
1 => yourkey
share|improve this answer
    
I just hope an explanation was given by that (-1) down vote.. –  Reigel Jul 1 '11 at 6:52
    
Fail to see what's wrong with this answer, +1 from me to cancel out the downvote. –  Mark Duncan Jul 1 '11 at 10:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.