开发者

multiple wordpress installs, getting data from wp-2 while inside wp-1

开发者 https://www.devze.com 2022-12-20 09:06 出处:网络
I have a site with 4 wordpress installs, (no need for wp-MU) x4 databases, with each a unique username and pass...

I have a site with 4 wordpress installs, (no need for wp-MU) x4 databases, with each a unique username and pass...

i will call these wp1=mainsite wp2=childsite wp3=childsite wp开发者_JAVA百科4=childsite

In wp1, there is a page, with tabs, which should load recent posts from the wp2, wp3, wp4 installs, this page is using a custom template to provide the page with php code which should connect to each database and return the data for each...

code:

$db_wp2_user = 'db_user'
$db_wp2_pwd = 'db_pass';
$db_wp2_db = 'db_name';
$db_wp2_host = 'localhost';
$wpdb_wp2 = new wpdb($db_wp2_user, $db_wp2_pwd, $db_wp2_db, $db_wp2_host);

//query
$wp2_latestposts = $wpdb_wp2->get_results("SELECT ID, post_title FROM $wpdb_wp2->posts WHERE post_status = 'publish' AND post_type = 'post' ");

foreach ($wp2_latestposts as $latestpost) {
  echo $latestpost->post_title."<br />";
}


$db_wp3_user = 'db_user'
$db_wp3_pwd = 'db_pass';
$db_wp3_db = 'db_name';
$db_wp3_host = 'localhost';
$wpdb_wp3 = new wpdb($db_wp3_user, $db_wp3_pwd, $db_wp3_db, $db_wp3_host);

//query
$wp3_latestposts = $wpdb_wp3->get_results("SELECT ID, post_title FROM $wpdb_wp3->posts WHERE post_status = 'publish' AND post_type = 'post' ");

foreach ($wp3_latestposts as $latestpost) {
  echo $latestpost->post_title."<br />";
}

$db_wp4_user = 'db_user'
$db_wp4_pwd = 'db_pass';
$db_wp4_db = 'db_name';
$db_wp4_host = 'localhost';
$wpdb_wp4 = new wpdb($db_wp4_user, $db_wp4_pwd, $db_wp4_db, $db_wp4_host);


//query
$wp4_latestposts = $wpdb_wp4->get_results("SELECT ID, post_title FROM $wpdb_wp4->posts WHERE post_status = 'publish' AND post_type = 'post' ");

foreach ($wp4_latestposts as $latestpost) {
  echo $latestpost->post_title."<br />";
}

each call, to each database should display the latest posts from each database.. on that single page... loaded into each tab...

when the code is run the page comes back with all styles and the footer, but were the information should load its blank, with no error messages,

can you tell me if this is the proper way to perform this action, or if there is an easier way to populate an array if the data is stored in another database, outside your main wp install?


During the install you can put in a prefix, so you can install as many WP installs as you want in the same database. This might be easier for what you're trying to do.

Wordpress Codex

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号