I have two WordPress installations, one at http://inversekarma.in and the other at http://inversekarma.in/photos. The latter is a photoblog, and its theme uses the standard WP post thumbnails (EDIT: featured images, to be precise!). Is there a way to show the most recent thumbnails from the second site on my first site's sidebar?

  • 1
    Are these two blogs in the same database with different prefixes, are they separate databases, or are the sites on separate servers? Commented Jan 1, 2011 at 8:50
  • @Mike: The two websites are on the same server, but use different databases with different prefixes.
    – GPX
    Commented Jan 1, 2011 at 17:25

2 Answers 2


There are (at least) two (2) ways to approach this:

  1. You can query the second database which will require you to maintain the database credentials in two places, or at least in an include file, or

  2. You could create a simple JSON feed for your photo blog and consume the feed on your main blog and cache it for a short time. This will cause a slight latency on page load when the cache expires but since both are on the same machine it shouldn't be an issue.

Let me know which you'd prefer and if I'll update the answer to so you how.


To connect to another database you can read more about it here:

So here's what I hacked together for you, a function called show_thumbnails_2nd_db() which you can copy into your theme's functions.php file:

function show_thumbnails_2nd_db() {
  global $wpdb;
  global $table_prefix;
  $save_wpdb = $wpdb;
  $save_prefix = $table_prefix;
  include_once(ABSPATH . '/photos/database-credentials.php');
  $wpdb = new wpdb($DB_USER, $DB_PASSWORD, $DB_NAME, $DB_HOST);

  // This is the code for featured images  
  $sql = <<<SQL
SELECT DISTINCT CONCAT('<img src="',attachment.guid,'"/>') AS url
  {$wpdb->posts} attachment
  INNER JOIN {$wpdb->postmeta} ON attachment.ID={$wpdb->postmeta}.meta_value AND {$wpdb->postmeta}.meta_key='_thumbnail_id'
  INNER JOIN {$wpdb->posts} ON {$wpdb->posts}.ID={$wpdb->postmeta}.post_id
WHERE {$wpdb->posts}.post_status='publish'
  AND attachment.post_type='attachment'
ORDER BY attachment.ID DESC
  $post_urls = $wpdb->get_col($sql);
  if (is_array($post_urls))
    echo implode("\n",$post_urls);

// This is the code for post thumbnails  
//   $sql = <<<SQL
// SELECT DISTINCT {$wpdb->posts}.ID
// FROM {$wpdb->posts}
// INNER JOIN {$wpdb->posts} attachment
//    ON {$wpdb->posts}.ID=attachment.post_parent
// WHERE {$wpdb->posts}.post_status='publish'
//   AND attachment.post_type='attachment'
// ORDER BY attachment.ID
// LIMIT 10
// SQL;
//   $post_ids = $wpdb->get_col($sql);
//   foreach($post_ids as $post_id) {
//     $thumbnail = get_the_post_thumbnail($post_id);
//     if ($thumbnail) {
//       echo $thumbnail;
//     }
//   }
  $table_prefix = $save_prefix;
  $wpdb = $save_wpdb;

Note: That the above assumes you have created a database-credentials.php in the root of your photos blog, and it should look like this:

$database_credentials = array(
  'DB_NAME' =>  'your_database',
  'DB_USER' =>  'your_db_user',
  'DB_PASSWORD' =>  'your db password',
  'DB_HOST' =>  'localhost',
  'table_prefix' => 'your_photos_db_prefix_',

Then inside the /wp-config.php for your photos blog you would replace the sections of code that look like this:

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', 'your_database');

/** MySQL database username */
define('DB_USER', 'your_db_user');

/** MySQL database password */
define('DB_PASSWORD', 'your db password');

/** MySQL hostname */
define('DB_HOST', 'localhost');

And this:

$table_prefix  = 'wp_';

With this:

include_once(ABSPATH . '/database-credentials.php');

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
define('DB_NAME', $database_credentials['DB_NAME']);

/** MySQL database username */
define('DB_USER', $database_credentials['DB_USER']);

/** MySQL database password */
define('DB_PASSWORD', $database_credentials['DB_PASSWORD']);

/** MySQL hostname */
define('DB_HOST', $database_credentials['DB_HOST']);

$table_prefix  = $database_credentials['table_prefix'];

If you need more explanation just ask in the comments.

  • @Mike: My guess is, the first approach would be speedier. Am I correct? If we're going with this, can we read the wp-config.php of that installation? Is it safe to include the database password in a file elsewhere? And if we're going for the second approach, I would like a long cache delay, as I won't update my photoblog too frequently. Is there a caching plugin for this? I figure our job would be simpler if there was such a thing. Thanks!
    – GPX
    Commented Jan 2, 2011 at 1:53
  • 1
    @GPX: Yes it is faster to access via the database, but only when the cache is refreshed and it sounds like that could be done once per day? Other than that it'd be a tiny bit slower. But it would be easier to use the database. Commented Jan 3, 2011 at 1:06
  • @GPX: I added an update, let me know what you think. Up votes and answer selects will be appreciated. :) Commented Jan 3, 2011 at 2:17
  • 1
    @GPX - Yes it is safe, updates don't touch /wp-config.php and if they did it wouldn't be hard to fix. The include is just a way to put the code that is normally in one file into two. It's not a big deal. You can't call /wp-config.php because at the end if calls /wp-settings.php to bootstrap WordPress. You could write a parser that would read the file and look for values but I thought that would be slightly slower and if someone used variables to define them somehow it wouldn't work. In order to suggest how to debug I need to know the symptoms. Commented Jan 3, 2011 at 18:06
  • 1
    @GPX - Do you realize that a "featured image" is not the same as a "post thumbnail", right? (And thanks for pointing out the typo, fixed.) Commented Jan 4, 2011 at 9:45

Mike's answer is great, but I would use a different SQL query, since the GUID of the attachment post can change when you change servers or move the blog. My version also gives you access to information of the blog post, which is useful if you want to link to it.

SELECT post.ID, post.post_title, attachment_meta.meta_value AS upload_relative_path
FROM {$wpdb->posts} AS post
    JOIN {$wpdb->postmeta} AS post_meta ON (post_meta.post_id = post.ID AND post_meta.meta_key = '_thumbnail_id')
    JOIN {$wpdb->postmeta} AS attachment_meta ON (attachment_meta.post_id = post_meta.meta_value AND attachment_meta.meta_key = '_wp_attached_file')
WHERE post.post_status = 'publish'
    AND post.post_type = 'post'
ORDER BY post.post_date DESC

Instead of _wp_attached_file you can also get the _wp_attachment_metadata value, which contains extra info about the image and alternative sizes.

  • Can I just replace Mike's SQL query with yours? Because I see that you are selecting 3 columns while his does only 1. If I want only the originally uploaded image's URL, can I select just attachment_meta.meta_value? And how do I select the small or medium-sized thumbnail for the corresponding image?
    – GPX
    Commented Jan 13, 2011 at 13:33
  • @GPX: You can use it instead of Mike's query. get_col() selects just one column, if you want more you should use get_results(). And you can leave off the columns you don't need, it was just an example of getting more info about the post. As I said, you can try it with _wp_attachment_metadata and apply unserialize() to the returned value. This way you get an array with information about all other sizes.
    – Jan Fabry
    Commented Jan 13, 2011 at 13:47
  • @Mike - Your block of code works great! But I have no idea about unserialize(). Can you just show me how to get the thumbnail?
    – GPX
    Commented Jan 13, 2011 at 17:10
  • @GPX: Just try what I said: use _wp_attachment_metadata instead of _wp_attached_file in the query. Return only attachment_meta.meta_value. Apply unserialize() to each result, and var_dump() it to see the contents. The rest is PHP programming, you should be able to do that yourself.
    – Jan Fabry
    Commented Jan 14, 2011 at 7:34
  • Okay so I put together a crude hack based on your idea, as I really couldn't understand the var_dump() of the unserialized array. Check out my piece of code - pastebin.com/xzEbt2NL. It uses hardcoded thumbnail sizes. Can you (pretty) please show me how to do it properly via unserialize? Also, how do I make each thumbnail a hyperlink to its post?
    – GPX
    Commented Jan 14, 2011 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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