5

I have a custom post type which needs to be in sync between two different websites housed in different servers. If either site edits, inserts, or deletes, the other database will also have to update its database for the change. The issue I see is, the ID's will be offset between the two databases.

So my rationale is, why can't I create a custom post type to be housed in a different database table and just sync that one table between the two installations?

The issue I see is I need to take into consideration the conflicts such as URL's. Page slug "hello-world" and custom post type slug "hello-world" when accessing the URL site.com/hello-world .

Anyone point me in a direction?


Edit : Looking into it more and thinking about it. How about on save_post, check if it's THE custom post type, connecting to the other database, and doing a last insert ID, store it locally, and then update the external database with the internal ID of the post?

enter image description here

  • I like to help, but maybe you could explain more with a diagram or more details please? – Greeso Jan 24 '17 at 0:28
  • @Greeso added an image/schema. – Darius Jan 26 '17 at 23:40
  • Why cant you have your 2 different Wordpress installations use the same database? Or perhaps come up with a simple api to get the data to and from both sites? Maybe look up replication? This would be a great question to ask a database admin. Perhaps try that stack exchange? Sorry I cannot be of more help. – Radmation Jan 27 '17 at 23:17
  • MySQL Replication for this. Without a doubt. – Michael Ecklund Jan 30 '17 at 23:19
  • I think being able to edit from both sides will end up as a major pain in the ass in any case. Is that absolutely necessary? May it be possible to edit it only on one site and periodically sync in one direction? – kraftner Jan 31 '17 at 17:03
0

Based on the details above, I can think of the following suggestions:

  1. Create a setup in which you have two servers using the same database. This can work if both your setups are identical to each other

  2. Sync the data (option 1). Write a plugin that you install on both servers, and this plugin will periodically read data from the other database and update its own records

  3. Sync the data (option 2). Write functionality that, when you update a record in database 1, it will trigger a REST API call that is sent to the second installation. Then that second installation will receive the updated data from the first installation via that call, and update its own records.

  4. Create a central database that both installation have access to, and this central database will contain shared data for both installations

  5. Have you thought of WordPress network setup (not sure if this works in this environment, you have to decide if this fits your requirements)

Personally, I like options 3, and 4 based on my understanding of the problem, but again, I do not have the full picture as you do.

  • But is there no way to query the other database for custom post type , get the results, and output it? I was able to do REST API methods but wordpress says page 404 not found even though it's outputting the page correctly. Like a pre_connect to database, change database , post get posts and end connection, switch back to regular database. – Darius Feb 3 '17 at 23:36
  • I am working right now on REST API. If you are having an error, then your API is not handling what you need to do correctly. – Greeso Feb 4 '17 at 17:50
  • In regards to another database, yes you can, but you have to write you own connection statements to them, and close them when you are done. This means you have to create a full 3rd database and access it, either directly or via REST API as well. This 3rd database can be part of a special plugin that you install in on both solutions and in which you have access to via that plugin. – Greeso Feb 4 '17 at 17:52
0

If you were to create a separate table to connect both WordPress installs to, you would need to make custom delete, edit and add functions so the data is sent to the right database.

If you are already needing to make these custom functions (many ways to hook into custom post type post, edit, delete, etc), why not just have one site have the 'master DB' and the other site just uses wpdb to connect to that other db and get, edit, post to it. There is no need for complicated checking of consistency as it is all being held within one table.

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.