Created
March 23, 2022 10:26
-
-
Save carlaizumibamford/93e2897b85c537910bf325ca830eaa98 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Simple Custom Field Query: | |
Display posts where the custom field key is ‘color’, regardless of the custom field value: | |
$query = new WP_Query( array( 'meta_key' => 'color' ) ); | |
Display posts where the custom field value is ‘blue’, regardless of the custom field key: | |
$query = new WP_Query( array( 'meta_value' => 'blue' ) ); | |
Display page where the custom field value is ‘blue’, regardless of the custom field key: | |
$args = array( | |
'meta_value' => 'blue', | |
'post_type' => 'page' | |
); | |
$query = new WP_Query( $args ); | |
Display posts where the custom field key is ‘color’ and the custom field value is ‘blue’: | |
$args = array( | |
'meta_key' => 'color', | |
'meta_value' => 'blue' | |
); | |
$query = new WP_Query( $args ); | |
Display posts where the custom field key is ‘color’ and the custom field value IS NOT ‘blue’: | |
$args = array( | |
'meta_key' => 'color', | |
'meta_value' => 'blue', | |
'meta_compare' => '!=' | |
); | |
$query = new WP_Query( $args ); | |
Display posts where the custom field key is a set date and the custom field value is now. Displays only posts which date has not passed. | |
$args = array( | |
'post_type' => 'event', | |
'meta_key' => 'event_date', | |
'meta_value' => date( "Ymd" ), // change to how "event date" is stored | |
'meta_compare' => '>', | |
); | |
$query = new WP_Query( $args ); | |
Display ‘product'(s) where the custom field key is ‘price’ and the custom field value that is LESS THAN OR EQUAL TO 22. | |
By using the ‘meta_value’ parameter the value 99 will be considered greater than 100 as the data are stored as ‘strings’, not ‘numbers’. For number comparison use ‘meta_value_num’. | |
$args = array( | |
'meta_key' => 'price', | |
'meta_value' => '22', | |
'meta_compare' => '<=', | |
'post_type' => 'product' | |
); | |
$query = new WP_Query( $args ); | |
Display posts with a custom field value of zero (0), regardless of the custom field key: | |
$args = array( | |
'meta_value' => '_wp_zero_value' | |
); | |
$query = new WP_Query( $args ); | |
Display posts from a single custom field: | |
$args = array( | |
'post_type' => 'product', | |
'meta_query' => array( | |
array( | |
'key' => 'color', | |
'value' => 'blue', | |
'compare' => 'NOT LIKE', | |
), | |
), | |
); | |
$query = new WP_Query( $args ); | |
(Note that meta_query expects nested arrays, even if you only have one query.) | |
Display posts from several custom fields: | |
$args = array( | |
'post_type' => 'product', | |
'meta_query' => array( | |
array( | |
'key' => 'color', | |
'value' => 'blue', | |
'compare' => 'NOT LIKE', | |
), | |
array( | |
'key' => 'price', | |
'value' => array( 20, 100 ), | |
'type' => 'numeric', | |
'compare' => 'BETWEEN', | |
), | |
), | |
); | |
$query = new WP_Query( $args ); | |
Display posts that have meta key ‘color’ NOT LIKE value ‘blue’ OR meta key ‘price’ with values BETWEEN 20 and 100: | |
$args = array( | |
'post_type' => 'product', | |
'meta_query' => array( | |
'relation' => 'OR', | |
array( | |
'key' => 'color', | |
'value' => 'blue', | |
'compare' => 'NOT LIKE', | |
), | |
array( | |
'key' => 'price', | |
'value' => array( 20, 100 ), | |
'type' => 'numeric', | |
'compare' => 'BETWEEN', | |
), | |
), | |
); | |
$query = new WP_Query( $args ); | |
The 'meta_query' clauses can be nested in order to construct complex queries. For example, show productss where color=orange OR color=red&size=small translates to the following: | |
$args = array( | |
'post_type' => 'product', | |
'meta_query' => array( | |
'relation' => 'OR', | |
array( | |
'key' => 'color', | |
'value' => 'orange', | |
'compare' => '=', | |
), | |
array( | |
'relation' => 'AND', | |
array( | |
'key' => 'color', | |
'value' => 'red', | |
'compare' => '=', | |
), | |
array( | |
'key' => 'size', | |
'value' => 'small', | |
'compare' => '=', | |
), | |
), | |
), | |
); | |
$query = new WP_Query( $args ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment