Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
25 Million+
Websites using our plugins
Years of WordPress experience
WordPress tutorials
by experts

What is: Query

Editorial Note: We earn a commission from partner links on WPBeginner. Commissions do not affect our editors' opinions or evaluations. Learn more about Editorial Process.

A query is a request for information from a database. It is used to describe the act of selecting, inserting, or updating data in a database.

In WordPress, queries are used to access data from your MySQL database. WordPress is written using PHP and MySQL.

Glossary: Query

How Are MySQL Queries Used in WordPress?

Each time you view a WordPress page, MySQL queries are run in the background to fetch the data from the database. This data is then used to dynamically generate HTML for your browser.

When users create, edit, or delete anything from WordPress, there are database queries that convert user input into instructions, which are then executed by running database queries.

How WordPress dynamically generates HTML by querying MySQL database using PHP based on user request

WordPress comes with built-in functions and classes that allow developers and users to query databases. These include WP_Query, WP_User_Query, get_comments(), get_the_terms(), get_posts(), wp_get_recent_posts(), and more.

Here is an example of querying the database for posts within a category using WP_Query class:

$query = new WP_Query('cat=12');

The result will contain all posts within that category, which can then be displayed using a template.

Developers can also query the WordPress database directly by calling in the $wpdb class:

function my_custom_query() {
global $wpdb;
$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users" );
echo "<p>User count is {$user_count}</p>";

Queries can also be used to create new records in the database (e.g., creating a Post) or editing existing records. These are done automatically by WordPress, but plugin developers can also use queries to store their own data in the WordPress database:

global $wpdb;
                DELETE FROM $wpdb->postmeta
		 WHERE post_id = %d
		 AND meta_key = %s
	        13, 'stars'

A WordPress query can look for items based on tags, categories, titles, status, and more. Developers can use this to create custom widgets or custom pages that display a specific set of content.

We hope this article helped you learn more about queries in WordPress. You may also want to see our Additional Reading list below for related articles on useful WordPress tips, tricks, and ideas.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Additional Reading

Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!