Ask your WordPress questions! Pay money and get answers fast! (more info)

How do I pull only posts from a custom post type into another DB? WordPress

  • REFUNDED

I have two websites on the same server. I am also using WooCommerce which uses a custom post type of 'product'. I need to pull in the products from site A into site B while making use of the styles and pages from site B.

My goal here is to have one central database of products so that I don't have to make changes on multiple sites.

The catch is that I want ONLY the products to be pulled in from the other database, but I want them to make use of the theme on site B.

So in the end Site B should use:
- Posts, Pages, Menus, Theme Options, etc. from Database B
- Products from Database A

So far, I've figured out how to connect site B to site A's database, but I don't know what to change in the WooCommerce plugin to have it pull from site A's database instead of site B's.

Thanks in advance!

****** EDIT *******

I've already figured out how to connect to the database and run a query to pull in the posts I want from the post table. However, I want to use the styles that are included in my theme instead of creating new styles to display the posts. I want all of the woocommerce pages on site B to work normally, just with data from site A. I hope that helps clarify, this.

Answers (4)

2013-07-13

isp_charlie answers:

you should use iframe to load products from site A to site B.

2013-07-13

Arnav Joy answers:

can you share how you manage to connect to other db ?


Matt comments:

$newdb = new wpdb($DB_USER, $DB_PASSWORD, $DB_NAME, $DB_HOST);
$newdb->show_errors();


I found it here:
http://bavotasan.com/2011/access-another-database-in-wordpress/

I can replace $wpdb with $newdb, but it uses the new DB for everything, not just product posts.

2013-07-13

Sabby Sam answers:

If I want to load into another site, I usually create a extra configuration and run from b site.
For eg: see the attache code below :

$host="http://mysql.host.ie"; // Host name
$username="yourusername"; // Mysql username
$password="password"; // Mysql password
$db_name="dbname"; // Database name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");
$fetch_data=mysql_query('SELECT ID,`post_author` , `post_date` , `post_title` , `post_content` , `guid`
FROM `wp_posts`
WHERE `post_type` = "post"
AND `post_status` = "publish"
ORDER BY post_date DESC
LIMIT 3');
while($row = mysql_fetch_array($fetch_data)) {
$featured_image="SELECT p.*
FROM wp_postmeta AS pm
INNER JOIN wp_posts AS p ON pm.meta_value=p.id
WHERE pm.post_id = ".$row['ID']."
AND pm.meta_key = '_thumbnail_id'";
$feature_image_pull=mysql_query($featured_image);
$display_image= mysql_fetch_array($feature_image_pull);
echo "<div class='article-box'>";
echo "<div class='article-image-box'>";
echo "<img src='".$display_image['guid']."' style='width: 100px;float: left;'>";
echo "</div>";
//for Author details
$author_query="SELECT * FROM `wp_users` where ID=".$display_image['post_author'];
$author_detail_pull=mysql_query($author_query);
$author_detail= mysql_fetch_array($author_detail_pull);
echo "<div class='articles-content'>";
echo "<h2 style='font-size: 12px;line-height: 13px;'>".$row['post_title'] . "</h2><p style='padding: 0px;margin: 0px;font-size: 12px;display:none'> On ".$row['post_date'] . " by ".$author_detail['display_name'] ."<p><p style='padding: 0px;margin: 0px;font-size: 12px;'> ".substr($row['post_content'],0,100)."... <a href=".$row['guid'].">read more</a></p></hr>";
echo "</div>";
echo "</div>";
}
?>
</div>


This above code will run as per your requirement but you need to format the css etc.

Alternate you can try this one link
http://snipplr.com/view/8999/manage-multiple-wordpress-sites-with-one-database-and-one-code-base/
[[LINK href="http://snipplr.com/view/8999/manage-multiple-wordpress-sites-with-one-database-and-one-code-base/"]][[/LINK]]

Hope this well help you


Matt comments:

Your code puts me a little further than I was which I like... but I'd like to use the styles from the second theme instead of having to create my own. Any thoughts there?

I looked at doing it the way the link suggests, but I like the idea of keeping the databases separate, especially because I want to keep posts and pages separate between sites.


Sabby Sam comments:

Yes, obviously you can use the same css.
I have one question :-
Does the payment gateway need to implement on both two sites ?
---------------------------------------------------------------
Note: If you implementing on two sites then you must read this blog too ( http://googlewebmastercentral.blogspot.in/2009/12/handling-legitimate-cross-domain.html) Duplicate content.


Sabby Sam comments:

Its simple, you can fetch the style and css from any site like this

<link rel="stylesheet" type="text/css" media="all" href="http://theantijunecleaver.com/wp-content/plugins/ban-user-by-ip/css/style.css" />

and you need to be check or you can copy above style.css and load into your theme.
Basically you will not change the css everytime. You will do only once.


Matt comments:

No, the payment gateway will not be used on the second site for now.

So if I use your code... where do I put it so that all of the theme pages render products using the theme's styles?

Forgive me, I don't fully understand how WP determines which post is being asked for so it can display it...

And thanks for the article!


Sabby Sam comments:

Open your SQL table check which table are being used by the Woo commerce plugin and accordingly you can implement in my code.
If you provide me login details I can easily implement it.


Matt comments:

Sorry for delayed response...

Woocommerce uses the wp_posts and wp_postmeta tables for the actual product data. But how does that help me know where to put the code?

I can maybe provide you details, but it will not be for a few hours.


Sabby Sam comments:

I have sent private message in your inbox.

2013-07-13

Navjot Singh answers:

You can use something like [[LINK href="http://wordpress.stackexchange.com/a/7154/5730"]]this[[/LINK]]. The query shown here can be modified to access only the product post type which will be displayed on your Shopping website.


Matt comments:

Thank you. But using this method, I think I would have to re-code all of the shopping pages in my theme. How do I use this along with the styles, loops, etc. that my theme provides?