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 haven't seen this done, but I want to return the custom field values outside of the loop in the sidebar.

share|improve this question
add comment

2 Answers

Definitely doable with tons of different ways to accomplish it. Basically, you can use a widget that allows for the execution of PHP or modify your theme files to execute PHP in the sidebar. The main function you will need is get_post_meta. You also need to access the post/page/CPT ID. You can use code like the following to get your post meta displayed.

global $wp_query;
if(is_object($wp_query->queried_object) && $wp_query->queried_object->ID)
{
    echo get_post_meta($wp_query->queried_object->ID, 'my_post_meta_key', true);
}

Obviously, you need to change the "my_post_meta_key" value to a key that corresponds to the meta data that you want. Also, there are other ways to access the current page/post/CPT ID, but since I do not know your specific context, this will work for most situations.

share|improve this answer
    
any chance of returning all meta, and exclude specific fields? –  Jason Aug 23 '11 at 22:49
    
You can use get_post_custom (codex.wordpress.org/Function_Reference/get_post_custom), which will return an array of all of the custom fields. Then, in your foreach loop, you simply withhold the values that you do not want to process. –  tollmanz Aug 24 '11 at 0:26
add comment

If you have the ID, then a call to get_post_meta() passing only the ID will get you an array of all the values:

$meta = get_post_meta( $post_id );
share|improve this answer
1  
How does this answer differ from the other answers? Or in which way is it more verbose than the others? –  kaiser Jul 6 at 2:18
    
The asker asked if there was a way to return all the meta fields.Additionally, I wanted to test the water of SE as a new user and see if I got instantly jumped on for attempting an answer. –  user55199 Jul 6 at 12:16
    
That's cool. Btw, look at get_post_custom() –  kaiser Jul 6 at 15:57
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.