One new feature of WordPress 3.1 that will help me with several projects I’m working on is the new custom field query system. This allows you to query multiple custom fields and with more filtering options. For instance, on the TUTV site we display a schedule of upcoming shows. To do this, we add a custom field to posts in a custom post type for schedule items. Previously, there was no easy way to easily organize upcoming showtimes so that only one day appears at a time. With the new meta_query options, I can simplify the schedule query to:
$args = array(
'post_type'=>'events',
// our new custom field query syntax
'meta_query' => array(
// note that the actual query array must be wrapped in
// another array. This allows for multiple queries.
array(
//formerly 'meta_key'
'key' => 'date_value',
//formerly 'meta_value.' This can be a string or an array of values
'value' => array($start_of_schedule_day, $end_of_schedule_day),
// compare can be 'LIKE', 'NOT LIKE', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'
// and the previously used '!=', '>', '>=', '<', or '<='.
'compare' => 'BETWEEN'
)
),
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => '48',
);
query_posts( $args );
The codex documentation for this feature also mentions that a ‘type’ may be specified:
type (string) – Custom field type. Possible values are ‘NUMERIC’, ‘BINARY’, ‘CHAR’, ‘DATE’, ‘DATETIME’, ‘DECIMAL’, ‘SIGNED’, ‘TIME’, ‘UNSIGNED’. Default value is ‘CHAR’.
I’d love to hear if anyone has any other creative uses of this new feature or how to use the ‘type’ attribute.