dil_bert Posted July 5, 2019 Share Posted July 5, 2019 (edited) hello dear experts on phpfreaks, We would like to create a dropdown menu that selects jobs from different sites on a Wordpress-multisite. Scenario: imagine - when on one site, I can select a site from a dropdown menu and have it populate with that other subsite’s job listings. So there would be a central db where all the jobs are stored - i would have three sites jobs.mysite.com free-volunteering.mysite.com work-and-travel@mysite.com is this doable - how to create the subsites - how to work on only one Database with the jobs Any advice on the best way to accomplish this? Regards Edited July 5, 2019 by dil_bert Quote Link to comment Share on other sites More sharing options...
dil_bert Posted July 7, 2019 Author Share Posted July 7, 2019 hello again - here i am back with more ideas. the question is: How to use wpdb on multisite-architecture to pull data from different subsites? Background: i am planning to set up a wp-site where a wordpress plugin (wp-job-manager) runs. i want to connect two other sites to that: well – We would like to create a dropdown menu that selects data-items from different sites on a WordPress-multisite installation. Guess i can do this with the wp-function "connect wpdb" to connect to another database. one method that comes to mind is to create the instance and pass it the database name/username/password. scenario and concrete plan: when on one site, I would like to be able to select a site from a dropdown menu and have it populate with that other subsite’s listings of database-entries from the wordpress plugin (wp-job-manager). Background: So there would be a central db where all the records are stored and which would support the three entities of sites: see how this architecture would look like – i would have three sites: jobs1.mysite.com jobs2.mysite.com jobs3.mysite.com note - you see this could be a combination of three job sites - running the wp-plugin wp-job-manager. Question: is this doable – how to create the subsites – how to work on only one Database with the records? Any advice on the best way to accomplish this? some musings about the setup: First thing that comes up to mind is the question, how to allow one site to get database-data from another site. This i guess can be a little bit tricky, this is not something that is normally done in a wordpress-setup. But there are certainly approaches to accomplish: The easy way would be, if the three sites are all on the same server under the same hosting account, that is helpful. In a case like that, we surely could write a little and tiny custom code-script to access the other site’s database directly from one or the other site. Wouldn´t this be applicable!?`What do you think!? Hmm - i guess that this is possible: we can get a interaction and interoperation between two databasese - if the both were residing on the same server. to spell it out in other words: the wpdb can be instantiated to access any database and query any table. How it would work: The wpdb object should be used to support the access of any database and and support any query of tables. The great benefit of this method is the ability to use all the general wpdb-classes and subsequently also the functions like get_results and others more. well i guess that we can do this probably like so: $mydb = new wpdb('username','password','database','localhost'); $rows = $mydb->get_results("select Name from my_table"); echo "<ul>"; foreach ($rows as $obj) : echo "<li>".$obj->Name."</li>"; endforeach; echo "</ul>"; well - i guess that the connecting to a second wordpress-database should be not that difficult in WordPress. Imagine - we would be able to setup the accessibility and interoperation between the db. That would be just great. i guess that i just would have to create a new instance of the WPDB class and then i should make use it the same way i would use the general standard $wpdb instance. What do you think about this idea?! Well i am assuming the second database has the same login information as the main WP (let us call this the WP-DB-One) then we could even use the predefined-constants that are written down in the wp-config.php to avoid any issues with the hardcoding of the database-login information of the site. i guess that we can work with the following piece of code: /** * we can do the Instantiate of the the wpdb class. This could be done easily: in order to connect to the second database, $database_name */ $second_db = new wpdb(DB_USER, DB_PASSWORD, $database_name, DB_HOST); /** * What do you think abou this approach: here we Use the new database object just like we would use the first one - the $wpdb */ $results = $second_db->get_results($your_query); Well - how about this approach: guess that we can go like this: And thirdly - using a third approach additional this can be done like so: if the additional databases (in other words those we also want to access) has the same db-access-credentials (in other words the user/pass details to access') as our main wordpress database we can use the database name before the table name like this $query = $wpdb->prepare('SELECT * FROM dbname.dbtable WHERE 1'); $result = $wpdb->get_results($query); While these probably will be able to work, one eventually will lose the ability to use the "other" custom features such as the following - eg. get_post_custom and other wordpress queries. The simple solution is $wpdb->select('database_name'); which changes the database system-wide (a mysql select_db). Imagine: this approach is very interesting since the database.table method works if we just want to make a simple query, but if we will want to access another wordpress site note - the architecture looks like so: Quote jobs1.mysite.com jobs2.mysite.com jobs3.mysite.com we can use the select-option. We just need to change it back when we re done. what do you think!? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.