Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Display Custom Fields Outside The Loop in WordPress

Last updated on by
Elegant Themes
How to Display Custom Fields Outside The Loop in WordPress

There will be times that you need to display post-specific content for example a different sidebar for a specific post. Usually post specific contents are added via a custom field which can only be displayed inside a post loop. In this article we will show you how you can display custom fields outside the loop. This tutorial will allow you to expand the possibilities of what you can do with WordPress.

The code will look like this:

<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'Your-Custom-Field', true);
wp_reset_query();
?>

You will need to make sure that you change the part where it says “Your-Custom-Field”.

This code is just the basic code. You can play around with it and use all the query_post arguments to fit your needs. You can even display another post’s custom fields on a single page of a different post.

In this (Example) at Uzzz, we are displaying the custom fields of a specific portfolio post, into the single post of a related testimonial post.

This snippet is very useful for theme developers. If you have any questions, feel free to ask in the comment or via email.


Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »

Comments

  1. Jason C says:

    Hey, I’m a complete php and wordpress newbie and I’m trying to call a custom field on my post by adding this ID, ‘dpta_discounttype’, true); ?> to display a value on my WP product post and its just showing the text.

    I did not at anything to function.php, I only added that line of code to the post content.

    How can I display the field on the UI? Thank you so much.

    • Mervin Abagat says:

      Hi Jason,

      Came across with your problem while I’m searching for some other tutorial that I am working with. Maybe this one can help you. First you must find that div container where you wanted to display that result and when you finally find it place this code.

      ID, ‘dpta_discounttype’, true) ?>

      ID, ‘dpta_discounttype’, true)); ?>

      Both codes are working you can choose between the two but it has a different result depeds on the value that you are putting into the custom fields meta box. If your using a plugin for that one maybe it has a different way on calling that custom fields value or it was on the plugin that you are using that you just need to activate. Try to search for the documentation of the plugin that you are using and if it is a plugin and if it not you just want to call the value of the custom fields. Above code is the easiest way to do it.

      Mervin

  2. Ugur says:

    Is it possible to edit/add new custom field or edit existing custom fileds’ value outside wp-admin? I have lots of posts and i want to edit / add custom fields with values on single page if user logged in with admin account?

    Thanks

  3. Greg says:

    Can I use this code to call custom fields into code? For example, if one custom field holds a URL, can i somehow call it outside the loop, but inside <a href= code?

    • Editorial Staff says:

      Yes you can.

      • greg says:

        haha i meant how can I do it. using an echo call inside an href tag only works if i put the <?php before it. cant do that because id be double calling php.

        • Kevin says:

          Create a variable first and then place the variable in the anchor tag

          Something like:

          $link = echo get_post_meta($postid, ‘Your-Custom-Field’, true);

  4. joel says:

    I’m not getting anywhere with this technique in WP 3.5 with a basic theme. I’m wondering if it’s out of date, or there is missing information.

    • joel says:

      I mean’t to post this on the article about Sidebars and custom fields, it’s missing info and/or out of date. It says it was updated in 2012 but relevant discussion from readers is from 2011 – 09. It’s a somewhat misleading

      • Editorial Staff says:

        The article was originally published on March 5, 2010… However, we do try to keep as many things up to date as possible. This article was last updated on Jun 2012 to make sure that it is all accurate and working. I just tried the code again, and the information here works. So now the last updated date will show March 18th, 2013.

        Syed Balkhi
        Founder of WPBeginner

    • Editorial Staff says:

      This is completely accurate, and the code still works. We are using it on several sites.

  5. Pete says:

    Am I right to say a use of this would be to use it in the sidebar when I’m on a single post so that the custom field will show according to the post that is being displayed in the main body?

  6. efgamercity says:

    Cheers for the code! I needed this for a game-info plugin I was working on and it worked a treat! :D

  7. Rick says:

    This has worked for me and it’s awesome! I just had a question though, because this seems to work on every page except my blog page, (index.php). I think it’s because I’m using a different loop on index.php Anyone else had this issue?

    Rick

  8. fokus says:

    Hi,

    Where do you put the code? – which php section are you editing?

    Thank you

  9. Rick says:

    What about displaying custom fields outside the Loop, with conditional statements? I saw your post on conditional statements as well, but I don’t understand the context well-enough to execute.

    Very useful! Can you help get to this last point?

    Thanks in advance!

    Rick

  10. Clare says:

    Works a treat – thanks! I’m now doing all kinds of crazy things with custom fields in my sidebar :)

  11. Walt Ribeiro says:

    How do I link an mp3 hyperlink (on a page) from a ‘mp3′ custom field I have in a post?

    I’m not sure how close I am, but here’s what I would like to do:

    <a href="ID;
    echo get_post_meta(2437, ‘mp3′, true);
    ?>”>

    the audio player automatically shows up if it sees a hyperlink (I’ve been using absolute http:// values), but all I see with this is code on my webpage.

    I’ve also tried it with HTML 5 audio tag:

    ID;
    echo get_post_meta(2437, ‘mp3′, true);
    ?>” controls=”controls”>

    Any help is appreciated!

    • Editorial Staff says:

      You are not suppose to put the post ID. Let it be dynamic with the $postid variable.

      • Walt Ribeiro says:

        i don’t understand what that means. i’ve tried everything. can you write out the example please?

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.