I’m trying to retrieve some data from a remote WordPress site (to another WordPress site), the code below returns an empty array, if I use the local database (just comment out the first line of code) it returns posts. Preferably I’d like to be able to run a WP_Query
on the remote database, but that’s just a bonus.
$wpdb = new wpdb( $dbusername, $dbpassword, $dbname, $dbhost );
$wpdb->set_prefix('wp_');
$wpdb->show_errors();
$psts = $wpdb->get_row(
"SELECT * FROM $wpdb->posts"
);
EDIT:
I’ve discovered that if I change the query to "SELECT * FROM wp_posts"
I get a result. So I guess it’s a PHP version thing (I run 5.4.4 and server runs 5.3.3)? Still can’t run a WP-Query though.
EDIT 2:
I just realized I’ve left out one important piece of information: the site I’m running the Query from is a multi site install! I’m sure this could affect they how the querys are formed!
Check if:
If you owner of source wordpress website please follow the instruction:
Create a php file inside source wordpress root directory named remotequery.php or you like best
load wordpress library including “required (‘wp-load.php’)” at the top of file
So it will something like this
<?php
define( 'WP_USE_THEMES', false ); // Don't load theme support functionality
require( 'wp-load.php' );
?>
Then you can add any wordpress query, you no need add database correction. you can add wordpress style database query to retrieve data from wordpress database.
See this plugin…
If both the sites are yours, and you want data from other site than you can get data in
json format
with this plugin…This plugin is quite help full…
Hope this will help…
You can also used the PHP CURL library for the remote connection.
is the one way
or we also used Ajax using the data type jsonp
that can connect to remove server using Ajax.
Please used below URL:
http://www.asp.net/ajaxlibrary/jquery_webforms_jsonp_retrieve_data.ashx
or
https://api.jquery.com/jQuery.ajax/