WordPress: How to get custom fields outside the loop

June 10, 2009 at 9:50 am

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.