0

My goal is to return posts based on a specific price range using meta_query. The price in the admin that the user enters is a custom field, created using WPAlchemy. In the example below, the query would return any posts with a price value between 1000 and 1500:

        'meta_query' => array(
            array(
                'key' => '_my_price',
                'value' => array( 1000, 1500 ),
                'type' => 'numerical',
                'compare' => 'BETWEEN',
            )),

It works fine if a price value was entered as 1200 (and stored as 1200 in the DB), but how could I get this working for prices that are stored in the database that have dollar signs and commas such as $1,200?

Ideally I would like all price values on the website to be consistent, so I'm using a Javascript currency formatter that converts the prices, which is why I'm asking. If a user enters 1200, it converts it to $1,200, which is then stored in the database. The meta_query doesn't think this is a number and therefore returns no results.

Or maybe there's a better method. Would it be better to ditch the JS formatter on the input fields in the admin and instead add the dollar sign and comma separation on the front end? This seems more logical, but then it raises another question of how to prevent a user entering a dollar sign or comma formatting in the admin.

1 Answer 1

1

Would it be better to ditch the JS formatter on the input fields in the admin and instead add the dollar sign and comma separation on the front end?

Yes, definitely.

This seems more logical, but then it raises another question of how to prevent a user entering a dollar sign or comma formatting in the admin.

Sanitize the input before storing it, using absint().

4
  • Thanks scribu. I've now done some reading and from what I've learnt, santizing the input will prevent it from posting unsafe data to the database (never knew this before!). And absint() is the WP function which will convert the value entered in my input field to a non negative value before it is stored in the DB? I'm unsure of how to use the absint() function with my input field. Do I just pass the text fields name or ID into it?
    – Andrew
    Jul 28, 2011 at 0:30
  • Here's what I have so far: <?php $mb->the_field('price'); absint( 'price' ); ?> <input class="text" type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>" />
    – Andrew
    Jul 28, 2011 at 0:31
  • getting closer :) I realise I have to put it within the actual input field. This is not working properly, adding an 0 onto the price, but it's doing something at least. <input class="text" type="text" name="<?php $mb->the_name(); ?>" value="<?php print absint( $mb->the_value() ); ?>" />
    – Andrew
    Jul 28, 2011 at 1:30
  • I'm going to open up another question regarding how to use absint and validate the custom field. Thanks for pointing me in the right direction. Sanitizing the field on save and doing the formatting on the front end it a much better solution than what I was trying to do in this question.
    – Andrew
    Jul 28, 2011 at 2:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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