Recently I was tasked with a project that required some filtering of a custom post type based on values stored using the WordPress Metadata API. Due to how popular it is to use metadata for storing additional data for posts (and custom post types) I thought it would be useful to share some of the details of how filtering on this metadata would work. In this article, I will be referring mostly to posts but the theory applies to custom post types as well.
In a WordPress environment, metadata is a way of storing and retrieving additional pieces of information on a specific WordPress object, without needing to register additional tables. The simplest example of this is the ability to add Custom Fields to a WordPress post.
When created, these custom fields are stored in the post metadata table, which (assuming a default table prefix of wp_ ) is stored in the wp_postmeta table. WordPress plugin developers typically use metadata for storing custom field values as it does not require making any changes to the database to function.
To be able to filter by metadata, you first need to meet the WP_Query object.
The WP_Query object deals with the intricacies of entity requests to a WordPress website.