I have a custom meta field for my posts and I'm able to display the value. But it only displays one row. Since I have more than one value I need to display all of them. I know I probably have to do some kind of for or while loop, but I don't know how to do it.

This is my current code:

$meta_value = get_post_meta( get_the_ID(), 'meta-text', true );

if( !empty( $meta_value ) ) :
    echo $meta_value; 

How do I change that snippet to display all rows?


To get all rows, don't specify the key. Try this:

$meta_values = get_post_meta( get_the_ID() );

var_dump( $meta_values );  // so you can see the structure
| improve this answer | |
  • It works, but is there no way to get an array by key? I can work with this, just wondering. – John Doe Nov 18 '14 at 20:33
  • You mean you want all values for a key in the post_meta table, regardless of post_id? If so, look at codex.wordpress.org/Class_Reference/wpdb#SELECT_a_Column – shanebp Nov 18 '14 at 21:26
  • No, only for the current post. But there are usually more than one result with that key. Anyway I made it work thanks to your answer, just thought it would look better in the code if it was possible to get them all by key. – John Doe Nov 19 '14 at 0:24

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.