query

Query

A Query is a term developers are using to describe the act of selecting, inserting, deleting, or updating data in a database also known as CRUD operations. In programming, queries are used to access data from various databases. When talking WordPress the most used databases are MySQL and Postgres and when talking C# the most used is MS SQL.

Each time you are viewing a webpage on the internet there are for sure queries running in the background to fetch or post data to and from the database. This data is used to generate dynamic HTML pages in your browser. When a user in a control panel is creating, deleting or editing content, there are underlying database queries that convert the user’s input to instructions which are then executed by running these so-called database queries.

Queries With WordPress

WordPress has built-in functionality and classes that gives the developer access to implement functionality for the user to query the database. Below is a short list of some examples:

  • WP_Query
  • WP_User_Query
  • get_comments()
  • get_the_terms()
  • get_posts()
  • wp_get_recent_posts()

Below is a very short code-snippet of how you can query the database for posts within a category using the WP_Query class in WordPress.

$posts_query = new WP_Query('cat=32');

The returned data is a set of posts within a specific category. These can now be displayed in the front-end using a template. As a WordPress developer, you can also query the database directly by calling the $wpdb class.

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

A query can also be used to insert new data into a database (this could be when you are creating a new post, uploading a picture etc…) or when editing existing content. This is automatically handled by WordPress, but developers making new extensions (plugins) can also use this to store information from their own data into the WordPress database.

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

In WordPress, a query is capable of retrieving (by default) items based on tags, categories, status, title, etc… As a developer, you can use this to create your own widgets or custom pages to display specific content for the users.

Queries With C# / .Net Core

The application does not care about the type and structure of the original data source (it’s not important). The application will always see the source data as an IEnumerable<T> or IQueryable<T> collection. If you are using LINQ to XML, then your source data will be made visible as anIEnumerable<XElement>.

Below is a code snippet to retrieve a subset of elements to produce a sequence without modifying the individual elements retrieved by the query. Inside the query you are able to sort or group the returned sequence in different ways as you can see below. Please assume that scores is an array of integers (int[])

IEnumerable<int> highScoresQuery =
    from score in scores
    where score > 80
    orderby score descending
    select score;

If you would like to know more about Query Expressions please take a look at the official Microsoft Documentation about Query Expression Basics.

Scroll to Top