Opened 14 years ago
Closed 12 years ago
#18736 closed enhancement (fixed)
Add REGEXP to 'compare' values in meta_query
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 3.7 | Priority: | normal |
Severity: | normal | Version: | 3.2.1 |
Component: | Database | Keywords: | has-patch |
Focuses: | Cc: |
Description
I wish to sort some queries using the alphabet.
My current query:
$getPostIds = $wpdb->get_col("
SELECT p.ID
FROM $wpdb->posts p, $wpdb->postmeta pm
WHERE p.ID = pm.post_id
AND pm.meta_key = 'prod-id'
AND pm.meta_value
REGEXP '^" . $wpdb->escape($alpha) . "'
AND p.post_status = 'publish'
AND p.post_type = 'review'
AND p.post_date < NOW()
ORDER BY pm.meta_value ASC"
);
$posts = new WP_Query(array(
'post__in' => $getPostIds,
'post_type' => 'review',
'post_status' => 'publish',
'showposts' => 20,
'paged' => $paged,
'caller_get_posts' => 1, // do not order sticky posts at the top
'orderby' => 'title',
'order' => 'ASC'
));
It would be much simpler if I could use the equivalent with meta_query:
query_posts(array(
'post_type' => 'review',
'post_status' => 'publish',
'showposts' => 20,
'paged' => $paged,
'caller_get_posts' => 1, // do not order sticky posts at the top
'orderby' => 'metal_value',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'prod-id',
'value' => '^'.get_query_var('letter'),
'compare' => 'REGEXP'
)
)
));
I'd love to see it implemented :)
Thanks!
Attachments (1)
Change History (10)
#2
@
13 years ago
To log SQL:
update_post_meta( 1, 'num_as_longtext', 123 ); update_post_meta( 2, 'num_as_longtext', 99 ); add_filter( 'query', function ( $sql ) { error_log( $sql ); return $sql; } ); new WP_Query( array( 'post_type' => 'any', 'meta_type' => 'UNSIGNED', 'meta_compare' => 'REGEXP', 'meta_key' => 'num_as_longtext', 'meta_value' => '^1' ) ); exit();
Note: See
TracTickets for help on using
tickets.
This is really easy to implement - not sure if anything more than $wpdb->prepare is needed to escape RegEx, but the whole meta_compare thing is really "you break it, you bought it" anyways. Attached a patch.