0

I have submitted an integer data via a form to my custom search page. I can get values properly if the input is single char (e.g. 1 ) but if the input is multi char (e.g. 100) it still compares against "1".

IF the user input is 1 I get all the posts with custom field value More than 1. but If the input is 100 the result is same.

Why is this happening??

Here is my code::

$i_rate = $_GET["interest_rate"];

                $bc_args = array(

                    'post_type' => 'data',
                    'data-category'=>'insurance-rate',
                    'order' => 'DESC',
                    'meta_key' => 'interest_rate',
                    'meta_value' =>$i_rate,
                    'meta_compare' => '>=',
                    'orderby' => 'meta_value_num',

                );

                 $sortedPosts = new WP_Query($bc_args);
  • Should not 'meta_value' =>$i_rate, be 'meta_value_num' =>$i_rate, – user27457 Apr 1 '14 at 9:55
  • @magenta I tried that too...but it doesn't work that way – rafsuntaskin Apr 1 '14 at 11:15
  • Was going to suggest casting to INT on your value, but looks like the marked answer has you covered.. :) – t31os Apr 1 '14 at 11:30
7

You could try this instead:

$bc_args = array(
    'post_type'      => 'data',
    'data-category'  =>'insurance-rate',
    'order'          => 'DESC',
    'orderby'        => 'meta_value_num',
    'meta_query' => array(
        array(
            'key'     => 'interest_rate',
            'value'   => (int) $i_rate,
            'type'    => 'numeric',
            'compare' => '>='
        ),
    )
);

$sortedPosts = new WP_Query( $bc_args );

where we use the numeric type.

You should also consider using filter_input instead of $_GET:

$i_rate = filter_input( INPUT_GET, 'i_rate', FILTER_SANITIZE_NUMBER_INT );

or the native add_query_var / get_query_var way.

Ps: data-category is not a native parameter in the WP_Query() class.

| improve this answer | |
  • Thank you so much.... it worked just I needed...I knew there was some type of casting problem just didn't know how to solve it. Thanks – rafsuntaskin Apr 1 '14 at 11:04
  • Glad to hear it worked for you. You can always check the generated SQL with echo $sortedPosts->request; to make sure it's what you want. – birgire Apr 1 '14 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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