This is the set up: Two separate wordpress installs on two separate domains (not multi-site)

This is what I want to do Query the posts from database one based on custom field and display them on the other Wordpress install

This is what I have done: Created a metabox in the WP 1 install for custom field Created a template in WP 2 install with the following query

$hbldb = new wpdb('username','password','dbasename','localhost');

$results = $hbldb->get_results( query_posts('meta_key=dbt_ffhpost&meta_value=on') );


The result: 404 - what you are looking for isn't here.

I know the issue is in the query - just don't know what. Do I need to query the specific table, row, field etc?

Any help greatly appreciated.

  • Did you test the values $hbldb and $results before attempting to use them? You don't actually show us your code, so it's anyone's guess about what caused a 404. If nothing else, stick in a print_r($hbldb) and a print_r($results) in there and see what you get. – Peter Rowell Feb 12 '11 at 18:43
  • Thanks for the tip - I am obviously not a php programmer;-) I put that in and it is connecting to the database - this is the result plantpurefoods.com/bluebird/blog-articles-page – Ann Feb 12 '11 at 18:54
  • Looks like I need to query the specific table(s) to get the post I am looking for? – Ann Feb 12 '11 at 18:55

The query_posts() inside your database call is being evaluated first, on the current site, and returning an array - which is probably empty, since its not the database you were expecting to be querying. Then that array of posts is being passed to the outer $hbldb->get_results() call, which is giving you your error, because it isn't a valid MySQL query syntax.

If you want to deal with the raw SQL query on your external database, you would have to write that entire query within your call to get_results()... ie, something like this:

$results = $hbldb->get_results( "SELECT * FROM 
                                {$hbldb->posts} posts 
                                JOIN {$hbldb->postmeta} meta
                                ON meta.post_id = posts.ID
                                WHERE meta.meta_key ='dbt_ffhpost'
                                AND meta.meta_value = 'on'",
                                ARRAY_A );

*(edited: added ARRAY_A argument to make this query return an array.)*

I think there must be a better way of doing this, but I can't quite wrap my head around how to to create a new WP_Query object that relates to a secondary wpdb object, or even if that's possible, so the best help I can give is the raw SQL.

And, once you have these results, you can loop through them like this:


if ($results) : 

foreach ($results as $post) :

echo '<h2>'.apply_filters( 'the_title', $post->post_title ).'</h2>';
echo '<h4>'.apply_filters( 'the_author', $post->post_author ).'</h4>';
echo apply_filters( 'the_content', $post->post_content );

endforeach; endif;


Note that getting more advanced data from this second WP install is much harder using this method. I didn't even deal with getting the permalinks, etc. If you need to get more data out of the other WP install, you might be better off setting up an RSS feed from that site and displaying items from that RSS feed on the first site using a plugin or basic fetch_feed() code.

  • The above code with the following revisions is REALLY close - it returns the result but not in the loop? – Ann Feb 13 '11 at 13:46
  • $hbldb = new wpdb('plantpur_wrd13','7PCXqFiUr5','plantpur_wrd13','localhost'); $results = $hbldb->get_results( "SELECT * FROM {$hbldb->wp_posts} wp_posts JOIN {$hbldb->wp_postmeta} wp_postmeta ON wp_postmeta.post_id = wp_posts.ID WHERE wp_postmeta.meta_key ='dbt_ffh_post' AND wp_postmeta.meta_value = 'on'" ); $pageposts = $wpdb->get_results($querystr, OBJECT); – Ann Feb 13 '11 at 13:47
  • and yes I am using revised loop $pageposts = $hbldb->get_results($querystr, OBJECT);<?php if ($pageposts): ?> <?php global $post; ?> <?php foreach ($pageposts as $post): ?> <?php setup_postdata($post); ?> – Ann Feb 13 '11 at 13:52
  • Ah, looks like the query is returning an object. Add ARRAY_A to the get_results call to make it return an array instead. I'll update my answer. – goldenapples Feb 14 '11 at 4:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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