Post Pic

WordPress: How to get custom fields 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

Jun 10 2009 10:53

Another nice post. Thanks.

Jun 10 2009 16:42

yet another great post by WPrecipes.com :)

Jun 10 2009 16:56

Nice guest post Paul. Indeed very useful. Thanks for sharing.

Jun 10 2009 17:35

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.

:)

Jun 10 2009 17:59

Question is… how do you get custom fields to come up in RSS Feeds…? THAT would be an amazing recipe. HINT HINT, WINK WINK

Jun 10 2009 23:46

@TJ: Must think about that, but that may be easier than you think :)

Jun 11 2009 00:06

Thanks, this is really WOW tips.

Jun 11 2009 09:08

Nice article..thanks

Jun 11 2009 15:30

Great and usefull function! How can i give some default value, if custom field is empty?

Jun 15 2009 19:28

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=”" />

Jun 16 2009 18:35

Nicely done here!

Jun 25 2009 08:20

how to choose custom field from spesific category?

Jul 10 2009 10:28

love this blog!! Thanks again for the great tip!

Jul 10 2009 18:44

Thank you very much, you are a life saver. This is exactly what I needed and it worked perfectly on the first try.

Jul 16 2009 18:04

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?

Aug 11 2009 05:42

Thanks, this worked for me!

Aug 26 2009 22:59

Once again, very nice post!

Sep 11 2009 07:59

Brilliant. Thank you!

Sep 12 2009 07:33

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.

Oct 08 2009 03:58

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!

Nov 05 2009 10:49

Thanks for this, I spent a good half of my evening last night trying to work this out!

Dec 22 2009 05:52

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.

Jan 14 2010 23:10

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.

Feb 02 2010 21:57

Very Helpfull, thanks! (muy útil)

Greeting from Colombia!

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required