0

This is really confusing me. Hopefully you can help me. What i want to do, is search for a number and display anything that is either minus 1 or plus one of the search result. The numbers that it will be searching for will have decimals, for example 12.5. So if i searched for 12.5 it should display 11.5, 11.6 all the way up to 13.5. I have the search part of it done its just displaying the posts is my issue. The code i have so far is:

<?php 
    $search_query = get_search_query();
    $searchplusone = ++$search_query ;
    $search_query1 = get_search_query();
    $searchnegone = --$search_query1 ;

$args = (array( 
    'post_type' => 'product',
    'numberposts' => -1,

'meta_query' => array(  
 array(     
    'value' => array( $searchnegone, $searchplusone), 
    'key' => 'kilowatt',
    'type' => 'decimal',
    'compare' => 'BETWEEN',

)),
 ) ); 
 $the_query = new WP_Query( $args );
?>
<?php echo $search_query; echo $searchnegone; echo $searchplusone;

print_r ($args);

?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <li>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    </li>
<?php endwhile; ?>
</ul>
<?php endif; ?>

<?php wp_reset_query(); 
?>

It seems to work when you search for 12.5, but when you search for 11.5 or even 11.9 it doesnt display the posts, but if you search for 14 it will display the posts which has the value 12.5 even though its not meant to because it should be comparing and displaying the posts between 13 and 15. It should, however display 11.9 because it is in range of 12.5.

EDIT: After further investigation, i discovered that if the number in the post is set to say 20 and you search for 19.1, 19.2 etc it will display the correct post. It just seems to be when the post has a number set that is a decimal it causes the problem.

3
  • are you sure the field which you are eval'ing is being properly treated as a decimal? This sounds like internally its being compared as a string. what happens when you query for the field and var dump? What type is it reporting? Commented Oct 8, 2014 at 14:48
  • It is being treated as a float. array(3) { ["post_type"]=> string(7) "product" ["numberposts"]=> int(-1) ["meta_query"]=> array(1) { [0]=> array(4) { ["value"]=> array(2) { [0]=> float(13.1) [1]=> float(15.1) } ["key"]=> string(8) "kilowatt" ["type"]=> string(7) "decimal" ["compare"]=> string(7) "BETWEEN" } } }
    – Daz
    Commented Oct 8, 2014 at 14:52
  • 1
    It will treat the decimal as a float but it will treat a full number like 10 as an int.
    – Daz
    Commented Oct 8, 2014 at 15:03

1 Answer 1

1

Turns out the answer was staring me in the face the whole time, what i did wrong was compare the posts by "DECIMAL" when it should have been "DECIMALS". I also added a (float) to both of my variables inside 'value' so it looked like this:

   'value' => array ((float) $searchnegone, (float) $searchplusone), 

Although im not sure if that would even make a difference.

Hope this helps with anyone who is having a problem. Fell free to send me a message if you need help!

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.