• Long story short:

    Several individual pages (no multisite, the pages belong to different companies) are supposed to be able to allow user to search through content of yet another page (posts and events manager events).

    Obvious solution: Link to the central content page, done. But customer would like user to search without leaving the current page – or at least the illusion.

    – iframe is no solution (sadly, ’cause it works fine otherwise), because IPhones6 and below don’t like no Iframes and workarounds don’t work, I assume due to running javascripts

    – sharing the content on a non-multisite setup: So far I could only find one Plugin allowing to share content on not-multisite pages – https://distributorplugin.com/ – but that’s still in development and not yet ready for a life setting. Too many bugs and glitches yet to be solved.

    – final idea: connecting to the central page’s database and search right there, only I’ve not yet really worked with databases before and don’t get it to work so far.

    I am using this script:

    	global $wpdb;
    	$thedb = $wpdb;
    	$mydb = new wpdb('*****MyUserName','*****MyPassword','*****MyDatabase','*****MyHost');
    	$wpdb = $mydb;
    	print_r($wpdb);
    	$result = $wpdb->get_results ( "
        SELECT * 
        FROM  $wpdb->posts
            WHERE post_type = 'post'
    	" );
    	
    	foreach ( $result as $page )
    		{
    		   echo $page->ID.'<br/>';
    		   echo $page->post_title.'<br/>';
    		}
    		
    	$wpdb = $thedb;

    But I don’t get any results. But the print_r($wpdb) tells me: “[has_connected:wpdb:private] => 1”

    – when I change the password (for example) I instead get:

    “[has_connected:wpdb:private] =>
    [error] => WP_Error Object
    (
    [errors] => Array
    (
    [db_connect_fail] => Array
    (
    [0] =><h1>Fehler (….)”

    That tells me that I do indeed get connected to the remote database, but why are there no results? I DO get results if I run the get_results on the local Database, so the script is fine.

    Any ideas? Am I missing something?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Dion

    (@diondesigns)

    I believe that you must call wp_set_wpdb_vars() immediately after the $wpdb = $mydb; line.

    FYI, you can significantly improve the speed and memory usage of your code by changing your query to this:

    "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'post'"
    

    Another approach:
    – Have all of the different company WordPress websites use the SAME database in the same shared hosting account, have each use a different “$table_prefix” setting in their distinct wp-config.php file. This way each site remains independent.
    – Put the shared event data into separate and distinctly named tables.
    – Create shortcodes which invoke functions that query and display the event tables contents, this code is probably identical across all the sites, code could be in a plugin or add to child theme’s functions.php file, each site has its own copy of the shortcode functions. The queries used by the shortcodes name the tables in full.
    – One site may have extra shortcodes which manage/edit the event tables, but could possibly give access to all sites.

    The only drawback is that this design is not great security, the different sites share permissions on all the tables, they could interfere with each other. The also share the same cPanel account.

    • This reply was modified 5 years, 1 month ago by RossMitchell. Reason: clarification, extra details
    Thread Starter kruge

    (@kruge)

    @diondesigns – Thanks for the answer, but that didn’t make any difference – still no output from the script.

    @rossmitchell – Thanks to you too, but anything like that where the pages use the same Database were declared a “no go” option by my customer. I asked for whether this was the way to go right from the beginning, because the whole set of pages all belong to a group of companies that work closely together and none of the pages contains anything like user-data or something similar – they’re basically all just “Hello, we exist”-Pages with some info on them and no user interaction.

    But even though all the pages have the same basic design characteristics and them using all the same events-calendar-page AND only one person will be responsible to administer the pages (me ^^) the decision was: “No way, it’s different companies”.

    Ah well – for now the links are enough and maybe later we still can find a solution.

    I already thought that maybe the provider doesn’t allow external access to the database, but then I should get a connection error instead a no error print_r of $wpdb, right?

    Moderator bcworkz

    (@bcworkz)

    Could it be the other DBs use a different table prefix? Can you access phpMyAdmin for the remote DBs? It would be a good test to run the resulting SQL right in phpMyAdmin’s SQL tab.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Is it possible to search another page’s DB?’ is closed to new replies.