How To Get Custom Fields Outside The Loop In WordPress

Custom fields adds some extra meta-data to our posts and pages. We can only display that meta-data inside the loop, not outside.

WordPress stores all the data into its MySQL database, so we will just made a MySQL query to get the custom field value outside the loop of your post. In short, here’s a nice trick to get custom fields outside the loop of your WordPress posts and pages.

Just add following snippet anywhere in your WordPress theme:

<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'CUSTOM-FIELD-NAME', true);
?>

Just replace the CUSTOM-FIELD-NAME in above code with the name of your custom field. That will do the job!

Leave a Reply

Your email address will not be published. Required fields are marked *