lp__meta_query
lp__meta_query is a custom query parameter integrated into the Launchpad plugin to facilitate meta queries for posts. This parameter acts as a flexible container for defining an array of meta query conditions.
Product
How it Works
In WordPress, meta_query is used to filter posts based on custom field values stored in the wp_postmeta table. Launchpad Core extends this capability by introducing a custom table for structured data storage, allowing for more optimized and flexible filtering.
The lp__meta_query parameter enables advanced filtering while remaining compatible with WP_Query. It works alongside WordPress’ default meta_query, ensuring that multiple filtering conditions can be applied efficiently without impacting query performance.
Accepted Arguments
The following arguments can be passed in a key => value paired array.
key(string) – Custom field key.value(string|array) – Custom field value. Accepts an array only ifcompareis set to'IN','NOT IN','ANY IN SET', or'EACH IN SET'.
compare(string) – Comparison operator used to evaluate the field’s value. Possible values are ‘>’, ‘<‘, ‘>=’, ‘<=’, ‘=’, ‘<>’, ‘LIKE’, ‘NOT LIKE’, ‘REGEXP’, ‘NOT REGEXP’, ‘RLIKE’, ‘FIND IN SET’, ‘ANY IN SET’, ‘EACH IN SET’. Default value is ‘=’.type(string) – Custom field type. Possible values are ‘NUMERIC’, ‘BINARY’, ‘CHAR’, ‘DATE’, ‘DATETIME’, ‘DECIMAL’, ‘SIGNED’, ‘TIME’, ‘UNSIGNED’. Default value is ‘CHAR’.relation(string) – Logical relationship between multiple conditions. Possible values are ‘AND’ (default) and ‘OR’.
Usage
This parameter allows you to define a two-dimensional array of meta query conditions, including custom field keys, values, comparison operators, and data types. These conditions help filter and retrieve posts based on specific criteria, enhancing your ability to tailor post queries to your needs.
Example
$args = array(
'post_type' => 'rep_listing', Replace with your actual custom post type
'lp__meta_query' => array(
'relation' => 'AND', // Combine conditions with AND logic
array(
'key' => 'bedrooms',
'value' => 2,
'compare' => '>=',
'type' => 'NUMERIC',
),
array(
'key' => 'property_type',
'value' => 'House',
),
),
);
$query = new WP_Query( $args );
In this example, we’re using the WP_Query to retrieve posts of a custom post type named ‘rep_listing.’ We’ve defined specific conditions using lp__meta_query to filter the results:
- The first condition filters results where the
'bedrooms'field is greater than or equal to2. - The second condition ensures the
'property_type'field has a value of'House'.
These conditions are combined with an ‘AND’ relation, meaning that only listings meeting both criteria will be included in the query results.
Result
The result of this query will be a list of posts of the custom post type ‘rep_listing’ that satisfy both conditions specified in the lp__meta_query. In other words, it will return posts where the ‘bedrooms’ custom field has a value greater than or equal to ‘2’ and the ‘property_type’ custom field is ‘House.’ |