Title: Using the Post&#8217;s Custom Fields Data In Sidebar
Last modified: August 19, 2016

---

# Using the Post’s Custom Fields Data In Sidebar

 *  Resolved [bluntrizzle](https://wordpress.org/support/users/bluntrizzle/)
 * (@bluntrizzle)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/using-the-posts-custom-fields-data-in-sidebar/)
 * Basically, on my single.php I have my post laid out in the loop as normal.
 * I then include a seperate sidebar.php named sidebar-single.php as it will be 
   different to the main index.php version.
 * In my actual post I set a custom field with some text I use to dynamically include
   a certain html file into the sidebar, depending on the post.
 * Say i set the custom field to “apples”, I want the code in the sidebar to automatically
   query the CURRENT post and look for the custom field and then output like a php
   include(/includes/apples.php)
 * I have done something similar inside the post loop where I can incorporate a 
   banner depending on the custom field set, but I am struggling to get this working
   in the sidebar.
 * This is probably straight forward, but I am not much of a programmer to understand.
 * A simpler way to explain and do this would be to get the current single post 
   title to be displayed in the sidebar outside of the loop.
 * Is this possible? Thanks in advance!

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [dagon-design](https://wordpress.org/support/users/dagon-design/)
 * (@dagon-design)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/using-the-posts-custom-fields-data-in-sidebar/#post-844080)
 * Since you are talking about doing this on single post pages outside the loop,
   you can use this method to get the post ID:
 *     ```
       global $wp_query;
       $post_id = $wp_query->post->ID;
       ```
   
 * Then you can use the get_post_meta function with the post ID:
 * `get_post_meta($post_id, $key, $single);`
 * More info at: [http://codex.wordpress.org/Using_Custom_Fields#Getting_Custom_Fields](http://codex.wordpress.org/Using_Custom_Fields#Getting_Custom_Fields)
 * Hope that helps.
 *  Thread Starter [bluntrizzle](https://wordpress.org/support/users/bluntrizzle/)
 * (@bluntrizzle)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/using-the-posts-custom-fields-data-in-sidebar/#post-844120)
 * Thanks for the response. I’ve tried the above method and echo’d out the post 
   id to see if it works for each single post but it does not.
 *     ```
       <?php
       	global $wp_query;
       	$post_id = $wp_query->post->ID;
       	echo $post_id;
       	?>
       ```
   
 * It gets the post id from one post, which is not even the current post in the 
   browser. If that makes any sense?
 *  [Jeremy Clark](https://wordpress.org/support/users/jeremyclark13/)
 * (@jeremyclark13)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/using-the-posts-custom-fields-data-in-sidebar/#post-844121)
 * Okay I think you got a little confused. This code needs to go within the loop.
 *     ```
       <?php
       	global $wp_query;
       	$post_id = $wp_query->post->ID;
       ?>
       ```
   
 * Then you can use this code outside of the loop.
    `<?php get_post_meta($post_id,
   $key, $single); ?>`
 * One more thing if your sidebar is a seperate file like sidebar.php and it’s called
   from single.php then you’ll need to make sure that you use this code.
 *     ```
       <?php
       	global $wp_query;
       	global $post_id;
       	$post_id = $wp_query->post->ID;
       ?>
       ```
   
 * You also should use this code to wrap your sidebar code.
 *     ```
       <?php (if is_single()){
       get_post_meta($post_id, $key, $single) } ?>
       ```
   
 *  Thread Starter [bluntrizzle](https://wordpress.org/support/users/bluntrizzle/)
 * (@bluntrizzle)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/using-the-posts-custom-fields-data-in-sidebar/#post-844130)
 * Awesome that worked after a little bit more of fiddling around [my fault due 
   to lack of php knowledge I guess]
 * In my sidebar-single.php I had to put this bit of code above the get_post_meta
   part:-
 *     ```
       <?php
       	global $post_id; ?>
       ```
   
 * Thanks a lot guys, I am totally chuffed! Appreciate your help! =)

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Using the Post’s Custom Fields Data In Sidebar’ is closed to new replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [sidebar](https://wordpress.org/support/topic-tag/sidebar/)
 * [single post](https://wordpress.org/support/topic-tag/single-post/)

 * 4 replies
 * 3 participants
 * Last reply from: [bluntrizzle](https://wordpress.org/support/users/bluntrizzle/)
 * Last activity: [17 years, 8 months ago](https://wordpress.org/support/topic/using-the-posts-custom-fields-data-in-sidebar/#post-844130)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
