WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am wondering whether this is possible:

  • I would like a widget which displays fields taken from a database table I have created.

Table contains information about the location of the post.

Widget to show the information about the post location based on a custom field in the post e.g

Post Custom Field Location = London

Widget to show text about London (stored in the database)

share|improve this question

I can't find a plugin that does this, but here's an alternative:

You can use code like this to get the post's metadata (custom field) values in the widget sidebar.php:

<?php global $post; ?>
<?php $sidebar = get_post_meta($post->ID, 'sidebarval', $single = true); ?>
<?php if($sidebar != '') { ?>
    <?php $myrows = $wpdb->get_results( "SELECT id, name FROM mytable WHERE col = " . $sidebar );
<?php } ?>
share|improve this answer
Thanks for the tip. DO I replace 'sidebarval' with my custom_field 'location'.. TO to echo the results? – Adam Beaumont Mar 30 '12 at 14:05
Yes, the "sidebarval" would be the variable that goes in the left custom field box (Name) and the $sidebar php variable will be the contents of that custom field (Value). There's a lot more you can do here - this is just a simple example. – GavinR Mar 30 '12 at 14:46
sorry I might be missing the trick here... I have put the following code in a PHP Widget in my side bar: <?php global $post; ?> <?php $sidebar = get_post_meta($post->ID, 'venue', $single = true); ?> <?php if($sidebar != '') { ?> <?php $myrows = $wpdb->get_results( "SELECT name FROM wp_venues WHERE name = " . $sidebar); <?php } ?> <? echo $sidebar; ?> What am I missing? – Adam Beaumont Mar 30 '12 at 15:24
ok fixed that thanks.. <? global $wpdb; $querystr = "SELECT name FROM wp_venues WHERE name ='".the_field('venue')."' "; $info = $wpdb->get_var($querystr); echo $info; ?> – Adam Beaumont Mar 30 '12 at 17:39

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.