php - woocommerce product attribute, how to filter between two values

252

Lets say i have a list off houses and they have a attribute called "size" now I want to get all houses between size 200 and 300.

I have tried

 $args = array(
  'post_type' => 'product',
   'post_status' => 'publish',
   'posts_per_page' => 2,
  'paged' => $paged,
   'meta_query' => array( 
       array(
        'key' => 'pa_size',
        'value' => array($sizeMin, $sizeMax),
        'compare' => 'BETWEEN',
        'type' => 'NUMERIC'
        )
     );         
  );

Then I tried with tax_query but I couldn't find a way to get a term between two values.

$args = array(
  'post_type' => 'product',
   'post_status' => 'publish',
   'posts_per_page' => 2,
  'paged' => $paged,
   'tax_query' => array( 
       array(
            'taxonomy' => 'pa_size',
            'field' => 'slug',
            'terms' => $sizevalue
        )
     );         
  );

Can't understand if this should not be possible but I think the value has to be a string therefor it cant be between.

for now im sorting them in my foreach loop when im displaying them but then my pagination is not working.

371

Answer

Solution:

My conclusion was that you cant do this with woocommerce product attributes because they a text based, så I made some Advancec custom fields and used them insted like this

$args = array(
         'post_type' => 'product',
         'post_status' => 'publish',
         'posts_per_page' => 2,
         'paged' => $paged,
         'meta_query' => array( 
               'relation' => 'AND,
               array(
                'key' => 'myCutsomField',
                'value' => array($sizeMin, $sizeMax),
                'compare' => 'BETWEEN',
                'type' => 'NUMERIC'
                ),
                array(
                'key' => 'myCutsomField2',
                'value' => array($valueMin, $valueMax),
                'compare' => 'BETWEEN',
                'type' => 'NUMERIC'
                )
             );  

$products = new WP_Query( $args );

People are also looking for solutions to the problem: apache - How in .htaccess do I return the file requested but then run a PHP script?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.