
Custom fields are definitely very useful and are used on many WordPress-powered blogs. In this recipe, I’m going to show you how to easily get custom fields values outside the loop.

Custom fields are definitely very useful and are used on many WordPress-powered blogs. In this recipe, I’m going to show you how to easily get custom fields values outside the loop.
To display a custom field value outside the loop, simply use the following code. Don't forget to replace customField on line 4 by the name of the custom field you want to display.
<?php global $wp_query; $postid = $wp_query->post->ID; echo get_post_meta($postid, 'customField', true); ?>
Thanks to Paul Whitehead for contributing to WpRecipe with this nice trick! If you'd like to share your WordPress knowledge with the community, don't hesitate to contribute.
47 Responses
Another nice post. Thanks.
yet another great post by WPrecipes.com
Nice guest post Paul. Indeed very useful. Thanks for sharing.
sweet… this is mine….
if you wanta use it in a function then :
function get_my_custom($cf_name = ”, $echo = FALSE) {
global $wp_query;
$content_array = $wp_query->get_queried_object();
$current_id = $content_array->ID;
$output = get_post_meta($current_id, $cf_name, true);
if ($echo === TRUE) :
echo $output;
else :
return $output;
endif;
}
usage:
get_my_custom(‘my_custom_field_name’, TRUE);
will echo the custom field, FALSE will allow it to be assigned to a variable.
Question is… how do you get custom fields to come up in RSS Feeds…? THAT would be an amazing recipe. HINT HINT, WINK WINK
@TJ: Must think about that, but that may be easier than you think
Thanks, this is really WOW tips.
Nice article..thanks
Great and usefull function! How can i give some default value, if custom field is empty?
why do i look here AFTER i spend days trying to do something?
i just put youtube videos on my post’s sidebar using custom fields
but it was a pain in the ass.
here is the exact code i used if anyones wondering. this is the EXACT code, so you might have to hack it slightly.
——————————–
<?php if($Video “” )
>
<object type=”application/x-shockwave-flash” data=”" style=”width: 300px; height: 252px; border: none; padding: 0; margin: 0;” id=”video-block-”>
<param name=”movie” value=”" />
Nicely done here!
how to choose custom field from spesific category?
love this blog!! Thanks again for the great tip!
Thank you very much, you are a life saver. This is exactly what I needed and it worked perfectly on the first try.
Hi, I’ve used this code and for the most part it works great, if used the code before my loop and it works perfectly, but after the loop it doesn’t echo anything. Anybody know why this might be?
Thanks, this worked for me!
Once again, very nice post!
Brilliant. Thank you!
Thanks a lot for this great article and your great book, too! I still have one more recipe request
.
I want to show a alphabetical list of post titles on a static page. For that i have created a custom field for posts in which i put a ‘letter’ for the authors first letter. But i don’t have an idea how to fetch the custom field of the posts in The Loop of the static page and further how to create a query that will return just those posts… Is there any way to do this for me? This would really make me happy.
Thank the lord almighty. And.. um… you. I’ve been working on this problem for days now for a client’s site. You saved me – I was close to scrapping the whole thing. Though I’ve never commented before, this site has helped me out many times. Thanks a million, and keep up the good work!
Thanks for this, I spent a good half of my evening last night trying to work this out!
I was actually searching for a tutorial on this exact topic and stumbled onto your blog. This was incredibly helpful, I will have to try it out. I run a number of websites using Wordpress and have found it to meet my needs for the most part, but I am never finished learning about what it can do.
Yeah, the thing with custom fields in wp by itself ist very cool. But using custom fields outside the loop is very very useful. Thanks for that.
Very Helpfull, thanks! (muy útil)
Greeting from Colombia!
Trackbacks: