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
- Return only Count from a wp_query request?
- Whats the difference between post_limits and pre_get_posts?
- Make a WP Query search match exactly the search term
- Query WooCommerce orders where meta data does not exist
- author.php with ACF and CPTs
- Identify which loop you are hooking into; primary or secondary?
- Finding all results from database within 500 miles of the given latitude and longitude [closed]
- Show only oldest post by author
- How to get the posts published in last two days using WP_Query?
- Dynamically Override Fancy Title – Part II
- Transient pagination not working properly
- Can not switch the queried post in pre_get_posts hook
- view queries made?
- Pass the same object to multiple widgets in a template with one query
- List the 5 most recent child pages
- SELECT * FROM $wpdb->posts WHERE ID > 160
- How WordPress attaches its Featured Images with posts?
- Query total number of posts
- Creating a custom search for a specific post type
- Custom query for sidebar isn’t returning results
- Iterating through $wpdb query without using get_results for large query results
- Filter posts in category archive page by year using a dropdown
- Query posts intersecting tags and categories
- How to implement a new row_count method in WordPress?
- WP_Query orderby and tax_query
- How to get a query variable?
- Query Page Content From Theme Options?
- Modify Search Query if original Query gave no results
- Include current post into loop
- Can’t sort custom post type by random
- Make a SQL query with wpdb in WordPress
- Query posts that contain video?
- Specify strict ‘order by’ in WordPress query
- Set front_page programatically after user login via query, while leaving site option alone
- Getting rid of unwanted nonSQL syntax characters when debugging a query
- Does WordPress have something like Drupal’s DB API?
- Why having more than 10 clauses in WP_Query results in some outputs being dropped?
- mysql query order by
- Notice thrown when creating numeric pagination on custom query
- wordpress udpate query not equal to
- WP Query – filtering terms with regex
- Issue with data array format
- meta_query where the meta value is not the post title
- Multi-layered WP_Query
- `offset` WP_Query argument dont work via `pre_get_posts`
- SQL query injection with fifu image
- Pagination of custom page with custom fields query
- Woocommerce Get Orders By Meta Value
- Move some posts to end of sort order, even if there is a sort in the wp_Query already
- wp query search multi terms
- WP_Term_Query->get_terms() very slow query for WordPress filter
- Is my code correct?
- How to search CPTs in draft using get_page_by_title()
- Meta query doesn’t remove placeholder escape before query
- How can I modify standard search query to include also ACF custom fields values?
- Bring a post to the top of the query if it’s in a certain category?
- Website goes slow down after importing long database
- How to get query results for the next page
- Pagination is not working on custom query inside a custom home page template
- Loop through multiple custom fields with increasing number
- How to organize a WP_Query’s list of posts by category and display category title?
- Need help setting up a search form for wordpress [duplicate]
- Custom WP_Query with SQL directives for getting posts by authors and terms
- custom query to get posts
- confirm my booking with phone number
- Get WP_Query query after execution?
- shortcode using multiple WP_Query’s with multiple category names not fully functional
- 294 Queries on Mainpage of WordPress
- SQL query to rewrite all media URLs to end with .webp
- SQL query into to WP query
- Search results stuck on page 1
- Use value from meta key array for use in WP_Query