I want to get list of 5 recent posts from a subdomain (not multisite) to show on our main domain. Following is the code am trying to execute, but is giving the error - Invalid arguments supplied in foreach()

global $wpdb;
$second_db = new wpdb('user','pass','db','localhost');
$rows = $second_db->get_results('SELECT post_title FROM wp_posts WHERE post_type="post" and post_status="publish" ORDER BY post_date DESC LIMIT 5');

echo "<ul>";
  foreach ($rows as $obj) :
    echo "<li>".$obj->post_title."</li>";
echo "</ul>";

I even tried to supply OBJECT, ARRAY_N etc within get_results and also tried to use $wpdb->prepare but nothing worked. Any ideas?

PS. The other database is also on the same server.


via $wpdb->print_error(); it shows -

WordPress database error: [] SELECT option_value FROM wp_options WHERE
option_name = '_transient_feed_mod_eec437511a934edf3e159e1cc8a5844b'

via $second_db->print_error(); it shows -

Warning: mysql_error() expects parameter 1 to be resource, boolean given


Finally the following code is working, I've realized the mistake and have mentioned it in comment. Now, when everything is running fine, I am receiving following text (error, if you may) before the output.

WordPress database error: [] 

Code -

global $wpdb;
        $second_db = new wpdb('user','pass','db','localhost');
        $rows = $second_db->get_results('SELECT * FROM wp_posts WHERE post_type="post" and post_status="publish" ORDER BY post_date DESC LIMIT 5');
        echo "<ul>";
        foreach ($rows as $obj) :
            echo "<li>";
                echo '<a href="' . $obj->guid . '">' . $obj->post_title . '</a>';
            echo "</li>";
        echo "</ul>";

Also, is there a better way to get permalink of the post instead of using $obj->guid?

  • Have you reviewed for any errors? Please take a look at codex.wordpress.org/Class_Reference/… for details on how you can enable the display of SQL errors in wpdb; and, when you have done so, would you mind sharing the information you receive? – Justin Bell Oct 13 '14 at 14:33
  • I updated the question. Please have a look. – vajrasar Oct 13 '14 at 14:43
  • Are you using $wpdb->print_error() or $second_db->print_error() to get the error? You should use the latter, since that's the one you're trying to get data from. – Pat J Oct 13 '14 at 15:13
  • 1
    Are you running the print_error() method right after you execute the query? It's hard to get a full picture from your code snippets. Also: Why aren't you using the build in methods from $wpdb to access the data, but adding a raw query with static table names? – kaiser Oct 13 '14 at 16:32
  • 2
    Are you sure your connection credentials are correct? The ones you use to create the new object? – s_ha_dum Oct 13 '14 at 16:33

I'm looking at wp-db.php and see:

1384                            if ( $this->dbh->connect_errno ) {
1385                                    $this->dbh = null;

This is in the class function db_connect(), which is called by the wpdb constructor. In the latest error you describe:

Warning: mysql_error() expects parameter 1 to be resource, boolean given

we can see that mysql_error() is not receiving the appropriate argument. This argument comes into play here:

1211            public function print_error( $str = '' ) {
1212                    global $EZSQL_ERROR;
1214                    if ( !$str ) {
1215                            if ( $this->use_mysqli ) {
1216                                    $str = mysqli_error( $this->dbh );
1217                            } else {
1218                                    $str = mysql_error( $this->dbh );
1219                            }
1220                    }

Note lines 1216 and 1218 (it differs based on whether you use mysqli extension). The argument it is passing in is your MySQL connection resource, which as seen on 1384-1385 is not defined correctly if there is a MySQL connection issue. This will make it difficult to trace the error at the point you're doing so from, but at least now you know it's an issue with the connection.

As @s_ha_dum indicated, you should verify your connection information to ensure that you're able to connect. If you have WP_DEBUG set to false, the mysql_connect() (lines 1409, 1411) and mysqli_real_connect() (lines 1379, 1381) are run with the @ prefix that suppresses errors. Enable WP_DEBUG and you should receive error text for these lines, if that is indeed where the problem lies.

  • Thanks @justin. I enabled WP-Debug and that pointed out the mistake. The problem was indeed with the connection settings. Thanks for the awesome explanation. I though, have updated the question (EDIT 2) can you please have a look? – vajrasar Oct 14 '14 at 6:42

The only neat way to get a full permalink is to make use of the get_permalink() function, @see http://codex.wordpress.org/Function_Reference/get_permalink

  • No, I tried this. This assumes that the $obj->ID resides in the current database (which in this case is main domain db) instead of going to subdomain's db and using get_permalink function there. – vajrasar Oct 14 '14 at 11:41
  • @vajrasar The GUID is a unique ID string, nothing else. It's no reliable source from where you can fetch permalinks. For e.g.: If you export/import data, the GUID does not change and will point to completely unrelated posts. What Ramon showed you is the only way to go, no matter how you retrieve the ID. – kaiser Oct 14 '14 at 12:03
  • 1
    Allright .. I see your problem as the current (main) WPCore WPDB database data source != the database data source you are using. – Ramon Fincken Oct 14 '14 at 12:14
  • Is there no AJAX/bootstrap method you can use on your secondary source to do queries, get_permalink() and return you data using JSON/XML ? Next to AJAX, you might like this one: [wordpress][snipplet][plugin] Standalone cron.php to call WordPress functions Permalink: ramonfincken.com/permalink/topic187.html – Ramon Fincken Oct 14 '14 at 12:21

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.