Retrieving Post/Page Meta Information within a Customized WordPress Query
A quick tip for WordPress users trying to access post meta information within a custom loop and/or customized single page/post.
The Scenario
I’m using a theme-per-post plug to style different pages of my blog with separate themes. This involved creating custom page templates and changing the default WordPress query to a custom query in order to obtain the desired posts. In my case, the custom posts are based on a certain category. This is simple and strait forward enough, but I ran into a problem when attempting to retrieve the post’s meta information when styled with a theme that the themeperpost plugin was utilizing.
Normally, the meta information is retrieved by the WordPress get_meta() method by passing it the global $post object ($post->ID), but since this isn’t available when looping through a custom query custom query I had to find a more creative solution.
The Looping Solution
As I was reading though various blogs and the WordPress codex I found that the ‘best practice’ in my case would be to assign the original WordPress query to a temporary variable, null out the existing query, and then create the desired query. Once this was done, you have the ability to globalize the $post object and reference it throughout the page.
This code would end up looking something like this:
<?php
/*
Custom Query Loop
*/
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=5&cat=511&paged='.$paged);
if($wp_query->have_posts()):
while($wp_query->have_posts()):$wp_query->the_post();
global $post;
// access meta information referencing the global post object
get_post_meta($post->ID, 'Thumbnail', true)
?>
This solution works well for the Main index (index.php) or page (page.php) files, but for some reason it would not retain the reference for the single page (single.php) file. I spent a little time investigating the cause, and though I didn’t come up with an answer as to why the query was behaving this way (it most likely has something to do with the themeperpost plugin and the header manipulation it performs), I did stumble upon a rather simple solution.
The Single Post/Page Solution
WordPress includes a function, get_the_ID(), which does exactly what it sounds like it’ll do. Its sole purpose is to return any given post or page idea within the context of its location — either within a loop or a sinlge post or page.
I changed the first parameter of my get_post_meta() function to the get_the_ID() function, and everything worked as I wanted it to. My call ended up looking like:
<?php /* Updated Get Post Meta Call */ get_post_meta(get_the_ID(), 'Thumbnail', true) ?>











Please could you help me with this I’m trying to do something similar but is not working.
i have a custom field called tagline and works well for the index, the blog and all the pages you create with wordpress but for example you create a page called blog to hold all the posts and create a custom field, the blog shows correctly the custom field but if you click on any of the post will take you to a single page that does not have the tagline.
my question is is it possible to create a get_post_meta on the single page to show the tagline of the blog page?
I’ve been searching for two weeks and haven’t found a solution to this, I’m still learning so please if you have time give me a hand with this problem.
Thank’s and great blog
Hi,
My first impression is that you’re not updating the “single.php” file with the appropriate code to display your custom field. WordPress utilizes two separate pages to display the “index” content (usually index.php) and single “page” or “post” content (usually single.php). If you aren’t already, try adding the get_post_meta function to the “single.php” file and see if that displays your tag line correctly.
If it doesn’t, or you’ve done this already and it still doesn’t work, let me know. I’m more than happy to help you try and figure this out.