Custom Woocommerce Product Query

admin2025-01-08  6

I work on a project that shows some travels (depending on the destination category). I try to sort the travels on the start date (var_confirmed_date) but the result is the same as no sorting.

Here is my query:

`$args = array(
                    'posts_per_page'    => -1,
                    'post_type'         => 'product',
                    'meta_value'        =>  'meta_value',
                    'meta_key'          =>  'var_confirmed_date',
                    'order'             =>  'ASC',
                    'meta_type'         =>  'DATE',
                    'tax_query'         => array(
                        array(
                            'taxonomy'  => 'destination',
                            'field'     => 'id',
                            'terms'     => array($t_id)
                        )
                    ),
                    'meta_query' => array(
                        array(
                            'key'     => 'var_confirmed_date',
                            'value'   => date("Y-m-d",time()),
                            'compare' => '>=',
                            'type'    => 'DATE',
                        ),
                    ),
                );
            $loop = new WP_Query( $args );`

Any idea to make it work correctly?

Thanks.

I work on a project that shows some travels (depending on the destination category). I try to sort the travels on the start date (var_confirmed_date) but the result is the same as no sorting.

Here is my query:

`$args = array(
                    'posts_per_page'    => -1,
                    'post_type'         => 'product',
                    'meta_value'        =>  'meta_value',
                    'meta_key'          =>  'var_confirmed_date',
                    'order'             =>  'ASC',
                    'meta_type'         =>  'DATE',
                    'tax_query'         => array(
                        array(
                            'taxonomy'  => 'destination',
                            'field'     => 'id',
                            'terms'     => array($t_id)
                        )
                    ),
                    'meta_query' => array(
                        array(
                            'key'     => 'var_confirmed_date',
                            'value'   => date("Y-m-d",time()),
                            'compare' => '>=',
                            'type'    => 'DATE',
                        ),
                    ),
                );
            $loop = new WP_Query( $args );`

Any idea to make it work correctly?

Thanks.

Share Improve this question asked Sep 20, 2020 at 14:30 MaximeMaxime 11 bronze badge 1
  • Thanks for your proposal. I do not have anymore result on the query with the code you propose (and mine also). The products I try to load are variable products. The var_confirmed_date are only available for variations and not for the product. Regards. – Maxime Commented Sep 21, 2020 at 4:44
Add a comment  | 

2 Answers 2

Reset to default 0

Try this

$args = array(
                    'posts_per_page'    => -1,
                    'post_type'         => 'product',
                    'meta_key'          =>  'var_confirmed_date',
                    'meta_type'         =>  'DATE',
                    'orderby'           => 'meta_value_date',
                    'order'             =>  'ASC',
                    'tax_query'         => array(
                        array(
                            'taxonomy'  => 'destination',
                            'field'     => 'id',
                            'terms'     => array($t_id)
                        )
                    ),
                    'meta_query' => array(
                        array(
                            'key'     => 'var_confirmed_date',
                            'value'   => date("Y-m-d",time()),
                            'compare' => '>=',
                            'type'    => 'DATE',
                        ),
                    ),
                );
            $loop = new WP_Query( $args );

I have found a solution on StackExchange (WooCommerce: filter by parent product's taxonomy and product variation's meta data)

I use a function in the child function.php then is sort on the $t_id and the ID found with the function

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736267808a1224.html

最新回复(0)