Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I currently have a simple wp_query search working on my wordpress site. I now want that same search to look for results in it's own database and another one on same server.

I found that I could use new wpdb()and then $mydb->get_results() to get the posts. I managed to pull a title of one of the posts but now I want to integrate this into my wp_query. Is this even possible? Here is a breakdown of the code:

$args = array(
'post_type' => 'post', 
'show_posts' => '10', 
'paged' => $paged
);

$the_query = new WP_Query($args); 

// Testing the new wpdb instance (RESULT: title echoed out. Working)
$mydb = new wpdb('USR','PSWD','DB','localhost');
$rows = $mydb->get_results(" SELECT * FROM wp_posts WHERE id = '5509' ");
foreach ($rows as $obj) :
    echo $obj->post_title;
endforeach;
// So now, how do I use my wp_query to get these results too?

// Loop
if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post();
share|improve this question

1 Answer 1

Out of the box? No

You can do some trickery to try and fool WP_Query into using your new db object but it may have issues with cached objects etc

e.g.

global $wpdb;
// backup wpdb
$thedb = $wpdb;

// pretend mydb is actually wpdb
$wpdb = $mydb;

// do stuff

// reset wpdb back
$wpdb = $thedb;

However if you're trying to pull in data from another site, I would strongly recommend against remote SQL queries, if not for performance reasons but for security reasons.

Have you considered RSS aggregation plugins?

share|improve this answer
    
Hm.. I agree, i'm not keen on remote SQL, but for now i think im trying to find a RAW method and then aim for tidying up later. I want to see how it can be done. It was suggested that having the two searches perform and then use an array_merge.. The mystery continues. I'll check out some RSS agg but I have some further customisation on the posts which could cause it to break down. Thanks for your response. –  Graeme Nov 23 '12 at 11:19
    
For anyone else who was trying this and struggling, I found that after setting $wpdb = $mydb; I also had to set the table prefix for WP_Query to work. $wpdb->prefix('wp_'); in my case. –  Jay Neely Apr 8 '13 at 15:06

Your Answer

 
discard

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.