SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru
    Join Date
    Jan 2007
    Posts
    971
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    WordPress Meta Query - 750 being evaluated the same as 75

    The meta query below almost works. It isn't comparing numbers correctly.

    In a query where $min=50 and $max=99, it returns a product with a value of 750 (or 751). It doesn't return products with a value of '5'
    In a query where $min=500 and $max=999, it returns a product with a value of 60.

    It seems to only be comparing the first two digits. When I print the values of the keys they look correct. Does anyone have any insight on what's happening here?


    PHP Code:
    <?php
            $args 
    = array(
                
    'post_type' => 'product',
                
    'posts_per_page' => 12,
                
    'meta_query' => array(
                    array(
                        
    'key' => '_regular_price',
                        
    'value' => array($min,$max),
                        
    'meta_type'=>'NUMERIC',
                        
    'compare' => 'BETWEEN'
                    
    )
                )
            );
            
    $loop = new WP_Query$args );
            if ( 
    $loop->have_posts() ) {
                while ( 
    $loop->have_posts() ) : $loop->the_post();
                
                    
    woocommerce_get_template_part'content''product' );
                endwhile;
            } else {
                echo 
    __'No products found' );
            }
            
    wp_reset_postdata();
        
    ?>
    Thank you E

  2. #2
    SitePoint Guru
    Join Date
    Jan 2007
    Posts
    971
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I figured it out. It wasn't picking up the value type. Changing 'meta_type' to 'type' fixed the issue.

    PHP Code:
    $args = array(
                
    'post_type' => 'product',
                
    'posts_per_page' => 12,
                
    'meta_query' => array(
                    array(
                        
    'key' => '_regular_price',
                        
    'value' => array($min,$max),
                        
    'type'=>'NUMERIC',
                        
    'compare' => 'BETWEEN'
                    
    )
                )
            ); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •