I have two totally separate WP websites setup. Different domains, different databases. I manage both of them and they are both hosted on a dedicated server. I am trying to include some basic content that requires just a tad more than an RSS feed. I need to pull data from SITE-1 and display it on SITE-2, using basic WP formatting from a loop. Everywhere I've looked makes it seem impossible. I've tried calling wp-load.php but can't get it to work, and am not sure if it is even the right way to go. I have access to both sites' root servers, and even the server root if necessary. Is there anyway to do this? Thanks!
2 Answers
Yeah
$wpdb2 = new wpdb('dbuser', 'dbpassword', 'dbname', 'dbhost');
// get 10 posts, assuming the other WordPress db table prefix is "wp_"
$query = "SELECT post_title, guid FROM wp_posts
WHERE post_status = 'publish'
AND post_type = 'post'
ORDER BY post_date DESC LIMIT 10";
$someposts = $wpdb2->get_results($query, OBJECT);
foreach($someposts as $somepost)
echo "<a href=\"{$somepost->guid}\">{$somepost->post_title}</a><br />";
Another way is to use the HTTP api:
Code in your first site, where you want to display the data:
$send = array(
'body' => array(
'action' => 'get_some_posts',
// send other data here, maybe a user/password if you're querying senstive data
),
'user-agent' => 'RodeoRamsey; '.get_bloginfo('url')
);
$response = wp_remote_post('http://yoursiteurl.com/', $send);
if (!is_wp_error($response) && ($response['response']['code'] == 200)) echo $response['body'];
Code in your second site, in the theme's functions.php (or create a plugin):
add_action('template_redirect', 'process_post_request');
function process_post_request(){
if($_POST['action'] == 'get_some_posts'):
$posts = new WP_Query();
$query = array('posts_per_page' => 10);
$posts->query($query);
while ($posts->have_posts()):
$posts->the_post(); // here's the usual loop
?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php the_content(); ?>
<?php
endwhile;
die();
endif;
}
The 2nd method is easier and more flexible from the "formatting" perspective. For example here you could easily echo the post thumbnail as html, while using the database approach it would be very hard for you to get the link to the thumbnail image...
-
Hm, either my comment hadn't posted or vanished.
wp_remote_retrieve_body()
is little more convenient than manual checks and direct access to response fields. And if going this way caching is a must, HTTP requests are major performance killers.– RarstCommented Jan 18, 2011 at 8:39 -
yea, in this case he needs to replace the last two lines with
echo wp_remote_retrieve_body(wp_remote_post('http://yoursiteurl.com/', $send))
Commented Jan 18, 2011 at 9:35 -
THANK YOU SO MUCH! This works great to grab basic info. Do you happen to know how to use it to grab custom fields from each post? That would be great! Thanks! (And sorry it took me so long to work on it.) Commented Feb 3, 2011 at 19:50
-
if you're using the second method, with
get_post_meta()
: codex.wordpress.org/Function_Reference/get_post_meta Commented Feb 4, 2011 at 20:07 -
<?php echo get_post_meta($post->ID, "my_custom_field", true); ?> doesn't work, but <?php $customField = get_post_custom_values("my_custom_field"); echo $customField[0]; ?> does, however, it doesn't work for images. I have images stored with each of the posts, and if I try <?php $customField = get_post_custom_values("images"); echo $customField[0]; ?> (where "images" is my custom field name for the images), it doesn't work. Neither does <?php the_post_thumbnail(); ?>. Commented Feb 5, 2011 at 5:31
I think your question is very similar to this question:
Basically you just need to created the $wpdb
using the security credentials for your other site. Take a look at my answer on that question and let me know if it does or does not answer your question, and if not why so I might be able to provide a better answer.