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
- Optimizing AJAX Query with Large Database
- how to get Nearby zipcode
- Getting rid of unwanted nonSQL syntax characters when debugging a query
- How I can change the condition or compare operator for WP_Query in pre_get_posts
- Does WordPress have something like Drupal’s DB API?
- Shortcode for custom query not returning results when attributes are added
- Using Transients
- Should I reset $wp_query?
- Yoast primary category query modification
- How to filter query loop block with a search string from the query parameters
- Update query for wp_posts and wp_postmeta
- SQL query to set posts in bulk based on the post content
- how to write wordpress query for multiple metakeys checking?
- WP_Query: apply an SQL function to meta fileld value
- Wpdb->query result show 1 but is not an integer
- Get specific ACF key and value from all posts – no access to DB
- Why having more than 10 clauses in WP_Query results in some outputs being dropped?
- mysql query order by
- Storing an array of objects related to each user
- Notice thrown when creating numeric pagination on custom query
- Retrieve data from wordpress db via input and put those result in dropdown
- wordpress udpate query not equal to
- WP Query – filtering terms with regex
- Is it possible to retrieve posts depending on meta_key LIKE condition? [duplicate]
- WP_Query with page_ids in arguments without a result
- Write WP Query that selects posts that are part of the same two categories
- Function to retrieve IDs of posts, cache results, and improve wp_query
- WP_User_Query orderby meta_val_num
- WP_Query custom order and pagination
- Issue with data array format
- How to avoid filling up an array each time I run a WP_query?
- Randomly select one of last 3 posts
- Unable to paginate a custom page query
- How to display data from db in select list [closed]
- Include one page/post into query which is already returning posts
- I am officially missing something about transient posts
- Insert images into wordpress post with a query
- Why doesn’t my WP Meta Query return any results?
- WP_Query meta compare must include ALL array values
- Start Query from 2nd Post without offset
- Disable (or limit) queries when certain content (or data) is not needed (or showed)
- 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?
- Prioritising and Ordering Posts By Category Name Using A Custom Loop
- meta_query where the meta value is not the post title
- Post incorrectly excluded when using “category__in”?
- Query multiple post types, but different order for each
- Multi-layered WP_Query
- `offset` WP_Query argument dont work via `pre_get_posts`
- Filter products on category AND tag
- Display posts ONLY from the current logged in user and current week/year/month Elementor posts
- SQL query injection with fifu image
- Passing a SQL query to the WP Query
- Pagination of custom page with custom fields query
- Query pages created by deleted users
- WP_Query() not working as expected
- Why last row deleted when refresh page
- WP Query – grouping posts by same meta key, adding together values from another key
- setting offset to category number in archive page
- Woocommerce Get Orders By Meta Value
- pre_user_query vs pre_get_posts
- Exclude post from wp_query based on custom field boolean
- Job and Employee Performance Tracking with Product Safety
- How to do WP_User_Query for Nested Array value Inside Nested Array field
- Get posts that match defined arrays of tags
- Doing $wpdb->get_results returns NULL, doing the same query in my DB returns correct value
- What is the equivalent WP_Query of a SQL Query?
- Query a database based on form input then output to another page [closed]
- Move some posts to end of sort order, even if there is a sort in the wp_Query already
- How select query is generated in a Custom Post Type?
- wp query search multi terms
- Adding nofollow to all the post links in get_posts with pre_get_posts or WP_Query