The WordPress APIs rely on the global $wpdb object, you can define any arbitrary object of type wpdb but that doesn’t mean WordPress will use it.
Instead, something akin to the following might be better:
global $wpdb;
$tempDB = $wpdb;
$wpdb = new wpdb(DB_USER2, DB_PASSWORD2, DB_NAME2, DB_HOST2);
// do your remote SQL stuff
// perhaps a WP_Query loop here?
// we're done, now restore the default and cleanup
$wpdb = $tempDB;
I would advise though that there’s very, very few instances were connecting to a second DB with a WordPress install is the optimum thing to do, there are many, many better ways of doing things. Multisite or RSS feed aggregation would probably be a lot better for you.
Related Posts:
- Most efficient way to get custom database records from 20 buttons and 20 tables?
- How to update BuddyPress xprofile fields programmatically? [closed]
- Best Practice for Syncing Local Development With Staging Development [closed]
- Is it good practice to use wpdb->query() function?
- Check if row exists before inserting
- Remove the deleted users avatar from list
- WordPress shortcode attributes for database SELECT?
- Updating Media Published Date When Parent Post Is Modified in WordPress
- Get User Login Data (date, time… )
- Multisite 404 on pages – rewrite error breaks database
- Accessing two databases wordpress
- Connecting to a 2nd DB gives me no results when querying
- Is There Any Built In WP Functionality For Combining Form Fields To Return Specific Data
- Check if a value exists in database table
- wp_usermeta – Read from database, but save function broken
- How to loop while adding data to database?
- How can I connect to a second database and still be able to use wp functions like get_post_types()?
- Writing a function for WP Cron to run a SQL command daily
- Update wp_postmeta table based on 2 keys
- Button click counter for login user
- Function stops working
- Database SQL Error, Should Work
- Database Query not working
- Function to get the name in database table from the comma separated string
- Functions file mods and CPU
- Bulk set Post Title as Tag where Tag is Empty Function
- Is it ok to use a function to output the text domain name in a wordpress theme
- Issue with wp_get_attachment_image() and SVG file type
- How to override a function when isn’t at functions.php
- deregister scripts on certain page
- contactform7 remove tags with “wpcf7_autop false” from functions.php
- How to count number of functions attached to an action hook?
- ajax live search for post title
- How to get an array of menu items from the toolbar?
- Using get_terms for custom taxonomy in functions.php
- Hide some items from Screen options in dashboard for products
- Editing or filtering the output of the Genesis navigation
- edit role display name and label name without plugins
- is_plugin_active() not properly returning true
- Display gallery on top before content
- List of Body Classes Generated by body_class()
- Converting html to wordpress images not showing
- is_dynamic_sidebar always returns true while using Jetpack visibility
- Replace menu links with # and add name to its li
- Variable if post is sticky in functions.php
- Retrieve tags data in post body
- Change template on the fly based on post parent selection
- Current path on page in functions.php
- How can I recompile js file in dist folder?
- Create a new query in function.php to filter blog posts
- Remove the product category module block from the Woocommerce product page
- Setting youtube size in functions.php
- How to get correct URLs in network wide menu (Multisite)?
- Remove category from url
- ‘Attempt to modify property of non-object’ warning
- Conditional functions.php on page template
- Optimizing a WordPress site
- different style sheet for just one page template
- Problem with comments_number()
- Functions when woocommerce isn’t installed
- How to target post and pages and not all post types in admin?
- Dequeue script to prevent javascript event conflict on wordpress child theme
- While loop with an exception after a count is reached
- Return child theme url
- Passing global variable from header.php to functions.php
- How is $current_page passed in woocommerce_account_orders function?
- Upgraded server now class My_Walker_Nav_Menu extends Walker_Nav_Menu not working
- How to include any template using Shortcode fuction?
- display a div on ervery site but not frontpage?
- Do we need to change our child function.php to require/include child dir files when we add an over-riding file.php into the child theme
- Replace header image on all other pages but home – URL issue
- Child theme functions.php
- How to echo Widget Title in Custom Frontend-Template Box
- Redirect to post after publish or update a published post
- Page Template Won’t Load Correct CSS File
- Getting images from media library and get_the_date() not working
- WordPress show bad the php hour and date
- Using add_image_size in functions.php to get original uploaded file
- show all the posts thumbnails
- the_excerpt function not showing image
- How do I change where “You must be logged in to comment” directs users?
- Login cookies blocked after customizing hashing method
- Order column custom date using pre_get_posts
- query_vars treat as single var from URL
- Running javascript without hooking wp_head
- Remove text after a dot and a colon in Woocommerce product title
- Enable CORS Request for hostname using IP address
- Change default color scheme in twentyfifteen child theme?
- Restrict access if logged out except for homepage
- Add meta data in head from theme
- what is `get_section()` and how to use it?
- DISABLE wordpress upgrade page
- How to echo the translated custom field?
- Moving CDN jQuery to footer does not work?
- Register a widget area when a theme option has been saved?
- Blank child theme – functions.php problem
- Hiding Google Analytics code based on theme options
- How do I make my child theme’s CSS update when I save it?
- Customizer cuistom CSS Priority ordre Issue
- WPDB query suddenly not working