0

I want to get a custom post value from post ID

PHP code :

$custom_field = get_post_meta(2055, the_field('titdesc'));
var_dump($custom_field);`

Output :

array(6) {
  ["_edit_last"]=>
  array(1) {
    [0]=>
    string(1) "1"
  }
  ["_edit_lock"]=>
  array(1) {
    [0]=>
    string(12) "1373450528:1"
  }
  ["_wp_page_template"]=>
  array(1) {
    [0]=>
    string(8) "work.php"
  }
  ["titdesc"]=>
  array(1) {
    [0]=>
    string(34) "and some experience, coffee & fun."
  }
  ["_titdesc"]=>
  array(1) {
    [0]=>
    string(19) "field_51baf57155c20"
  }
  ["dsq_thread_id"]=>
  array(1) {
    [0]=>
    string(0) ""
  }
}

I want ["titdesc"] value but not able to get what to write in PHP to get ["titdesc"]'s value.

1
  • As a note, you should be using get_field() instead of the_field().
    – RRikesh
    Commented Jul 10, 2013 at 11:53

2 Answers 2

1

I think you messed up a bit. I'm assuming you're using the ACF plugin because you used the_field().

Have you tried only the_field('titdesc'); if you're in the WordPress loop? Those functions already query values stored in your postmeta table. I'm assuming you have a field called titdesc since you're trying to read it using the_field('titdesc').

If you're not in the loop, you must pass the post ID too, like the_field('titdesc', 2055);.

From what I've noticed, ACF uses two rows per custom field. For you it is:

  • titdesc => and some experience, coffee & fun.
  • _titdesc => field_51baf57155c20

The first pair of values stores the field name titdesc and the field value and some experience, coffee & fun. and the second pair of values is a reference to the field name's unique ID, which is field_51baf57155c20 here.

As a note, get_field() will return a value and the_field() will echo the value.'

If you want to pass the value in a function, you should use get_field() instead. What you got in your var_dump() output is all meta values stored for that particular post ID.

0

You should use

echo "<pre>";
print_r($array_name); //$custom_field in your case
echo "</pre>";

That generates a more understandable array dump.

Answer:

 echo $custom_field['titdesc'][0];

would do in your case.

or you can directly get a single meta key using

$titdesc = get_post_meta(2055, 'titdesc',true);
echo $titdesc;

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.