WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What I'm trying to do

I am using WP_Query() to get posts based on their range for example:

$args = array(
    'numberposts' => -1,
    'post_type' => 'page',
    'posts_per_page' => -1, 
    'meta_query' => array(
        'relation' => 'AND',
            'key' => 'parameters',
            'value' => 'width',
            'key' => 'value',
            'value' => array( '22mm', '55mm' ), // Can this be done when I'm using NUMERIC and BETWEEN?
            'type' => 'NUMERIC',
            'compare' => 'BETWEEN'

$the_query = new WP_Query( $args );

In the DB

In the databse my values are stored as strings i.e. 22mm, 55mm etc.


How can I tell the query to search a range and ignore the mm in 22mm?

share|improve this question
You couldn't store the units in a seperate meta value? – Zogot Oct 31 '13 at 13:11
@Zogot Believe me I wanted to. But I couldn't at the time and now I have a lot of data which is not always the same :( – hitautodestruct Oct 31 '13 at 13:12
If your values are always "number" followed by "mm" then what you are doing should work. CAST will turn "55mm" into "55". – s_ha_dum Oct 31 '13 at 14:40
@s_ha_dum So, you're saying the code behind the scenes is converting the string into an int? – hitautodestruct Nov 3 '13 at 9:31
Yes. The query will CAST your value as signed-- var_dump($the_query->request). Run SELECT CAST("15mm" as signed) in a MySQL console and you'll see that the letters are dropped leaving just "15" or whatever number you have in there. – s_ha_dum Nov 3 '13 at 14:36
up vote 1 down vote accepted

Intercept the query on the posts_where filter. In there you can do a replacement of the non numeric values:

preg_replace( "/[^0-9,.]/", "", $yourMeta );

Or in a (generic) code piece:

add_action( 'posts_where', 'wpse120784WhereDiameter', 100 );
function wpse120784WhereDiameter( $where )
    // If conditions here - only target the cases where we need it
    # if ( foo ) { etc.

    // Only run once for this single query
    remove_filter( current_filter(), __FUNCTION__ );

    $diameter = get_post_meta( ... );
    $where .= preg_replace( "/[^0-9,.]/", $diameter, $where );

    return $where;

When you manage to target the exact MM diameters that you're searching for, then the Regex Search/Replace call will simply pop off the MM/Inch (whatever unit there is).

Of course you'll need to somehow gain access to the post meta (get_post_meta()) values to target them in the Regex replacement task more accurately, but it should bring you to your goal.

Still I think that removing the MM from your entries would be much easier and better than what you're trying to achieve.

share|improve this answer
Sorry, but what? I'm not sure I understand how to do what you are suggesting. – hitautodestruct Oct 31 '13 at 14:31
@hitautodestruct See update for an example. If this still isn't enough, then you'll find other examples for posts_where when searching the site. – kaiser Oct 31 '13 at 15:18

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.