You can use wpdb to setup a new connection.
Add something similar to this below in your functions.php and you can then query just like you would normally but using the new connection.
$second_db = new wpdb(DB_USER, DB_PASSWORD, $database_name, DB_HOST);
$results = $second_db->get_results($your_query);
references:
Related Posts:
- How To Get Some Data From WordPress Database Using WordPress $wpdb Query?
- How to make an activities stream mixing posts and comments?
- What is an efficient way to query based on post_meta?
- SQL query equivalent to WP User Query
- how to retrieve specific product attribute value in an sql query?
- WP_Query Performance Issues with meta_query
- Get the post permalink within the loop but without additional DB query
- Filter WordPress posts by between parameter
- How to correctly pass values to wpdb->prepare()?
- List of the years with posts presented
- Optional Meta Query
- Editing the default wordpress search
- WP Query related posts by tags
- Order by summing multiple values
- Include data from custom table in WP_Query
- No results found from a $wpdb->get_results() query when trying to join more than one meta key query
- List of ways to access WordPress database?
- How to set up hierarchical relationships without using plugins / meta query
- Query where ANDing slug values not working
- Slow Query On Search
- How to SQL query posts IDs by categories AND authors?
- Any way to use FETCH_KEY_PAIR with $wpdb?
- How to get meta key list efficiently?
- Translating WP query into to SQL query
- Slow wp_posts and wp_postmeta query using Advance Custom Fields
- Include post_status check within $wpdb query
- wpdb get_results() returns only 2 rows
- WP_Query always returning the last custom post
- When should you use WP_Query vs query_posts() vs get_posts()?
- Pagination with custom SQL query
- How to query for most viewed posts and show top 5
- Using OR conditions in meta_query for query_posts argument
- Add indexing to meta_value in wp_postmeta
- Custom WP_Query order by post_meta and (author) user_meta
- WP_Query for WooCommerce Products
- Get posts by menu ID
- Slow SQL_CALC_FOUND_ROWS Query
- Export wordpress table to excel
- Perform query with meta_value date
- Recommended way to drop a pending query (in pre_get_posts)?
- Change SQL get_results to search for posts with custom term in custom taxonomy
- How to find if a post with custom_field == X exists?
- WP_query category__in not working, only pulls from first category
- Splitting the main query in multiple loops with query_posts and/or pre_get_posts?
- SQL User Query by Multiple Roles using PHP
- Query WordPress database by registered date and role
- What’s faster? One big query, or several smaller ones?
- Meta Query for specific months
- Different string for specifed post type on posts listing at homepage
- if/else on custom query gives 200 OK when condition not met?
- Select from wp_post and multiple meta_value from wp_postmeta
- Order by custom table and multiplication
- Sort users by “birthday” using WP_User_Query
- only delete post within query / for each statement (front end)
- unable to retrive parent page information
- How to detect if query is runned from widget?
- Show all data in database table and make it into an interactive table
- How to get list of posts from permalinks?
- database query with more than a couple meta hangs and doesn’t complete
- Check return value of get_posts
- Transient api Caches confused
- How can I override one post and make it display content for another post?
- Debugging wp_query orderby for taxonomy
- Get a list of posts by specific category
- How to check the array values, what WP_Query has brought to me?
- How to add custom meta to ‘pre_get_terms’?
- Shortcode for custom query not returning results when attributes are added
- Using Transients
- Should I reset $wp_query?
- how to write wordpress query for multiple metakeys checking?
- Get specific ACF key and value from all posts – no access to DB
- Randomly select one of last 3 posts
- How to display data from db in select list [closed]
- I am officially missing something about transient posts
- Start Query from 2nd Post without offset
- Display upcoming events in the next 30 days by querying posts with timestamp in custom meta
- How Can I Always Display A Particular Post First Using WP_Query?
- WP_Query() not working as expected
- How to query users by post count no less than 10
- WP_Query about meta_key and its array value
- Create global array to compare inside a query
- Ignoring ‘a’ when sorting posts
- direct query to post_meta table
- Find by post_meta, then sort by post_meta, then sort by date (wp_posts)
- Filter posts/pages by user_role array
- Optimising amount of calls to custom fields
- WP Query to order posts by multiple meta fields
- add_query_vars does not work
- Retrieve posts in custom post type and specific taxonomies
- WP_Query with several meta_query-statements and order by meta_value
- Custom post types loop on a page template
- Parsing External Table Arguments
- difference between methods of query
- wpdb LIKE request shows all database data
- Query posts by meta value and sort by another meta key
- how to use transient method?
- Custom WP_query in Jnews theme returns thousands of posts
- SQL Query to get post_id from wp_posts and and meta_key(s) from wp_postmeta
- How to use meta_query to retrieve posts from multiple custom post type
- Rewrite URL custom search query