WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] How to get all custom fields (name value)?

[Resolved] How to get all custom fields (name value)?

  • Does anyone know how to show all custom fields (name AND value) for one post?

    I would like to create a FAQ, and just put the Q&A:s into the custom fields of one post:

    CUSTOM FIELDS
    NAME…………………………………VALUE
    Question 1………………………….Answer 1
    Question 2………………………….Answer 2
    Question 3………………………….Answer 3

    ______________________________________________

    I am using a jquery faq plugin, so I want to have full control of the data. Please help..

Viewing 8 replies - 1 through 8 (of 8 total)
  • esmi

    @esmi

    Forum Moderator

    Emsi,

    That doesn´t give me both fields. Only one of them + the ID 🙁

    Justin Tadlock

    @greenshady

    WordPress God

    It works perfectly fine. get_post_custom() returns a multidimensional array of ALL custom fields (with keys and values) for a post.

    Greenshady,

    OK, I´m sorry. But I can´t get it to work.
    Could you please give me the code for echoing “name” and “value”?

    Someone please help me.

    I have my custom fields like this:

    Custom field “name” Custom field “value”
    Q: How do I contact you? A: You can use our contact form here
    Q: Which way is the……. A: Try this one……..

    And so on. Everything I am able to get is:

    0 => You can use our contact form here
    1 => Try this one……..

    ID and the value field. Not the “name” field.

    Justin Tadlock

    @greenshady

    WordPress God

    This bit of code should work:

    <?php
    $custom_fields = get_post_custom();
    
    foreach ( $custom_fields as $field_key => $field_values ) {
    	foreach ( $field_values as $key => $value )
    		echo $field_key . ' - ' . $value . '<br />';
    }
    ?>

    Thank you so much, greenshady!

    But I get this as well, in the beginning of the custom fields:

    _edit_lock - 1274798334
    _edit_last - 1

    Is it possible to filter that away somehow?

    I got it, thanks to a friend:

    $custom_fields = get_post_custom(9);
    
    foreach ( $custom_fields as $field_key => $field_values ) {
    	if(!isset($field_values[0])) continue;
    	if(in_array($field_key,array("_edit_lock","_edit_last"))) continue;
    echo $field_key . '=>' . $field_values[0];
    }
Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘[Resolved] How to get all custom fields (name value)?’ is closed to new replies.
Skip to toolbar