$wpdb is a PHP global variable that holds the WordPress database object, which is actually an instantiation of the wpdb
class.
WP_Query actually uses wpdb
to query the database.
Use WP_Query
when you manipulate native WordPress tables (the vast majority of common tasks).
Use $wpdb
when you manipulate custom tables.
References
- https://developer.wordpress.org/reference/classes/wpdb
- https://developer.wordpress.org/reference/classes/wp_query/
- Data Validation (must read)
Entrepreneur | Full-stack developer | Founder of MediSign Ltd. I have over 15 years of professional experience designing and developing web applications. I am also very experienced in managing (web) projects.