0

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?

2 Answers 2

3

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

0
1

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
2
  • I just hope an explanation was given by that (-1) down vote.. Commented Jul 1, 2011 at 6:52
  • Fail to see what's wrong with this answer, +1 from me to cancel out the downvote.
    – t31os
    Commented Jul 1, 2011 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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