skip to Main Content

what im trying to do is to make a query to get all the lastest new products, Im using woocommerce Plugin.
newest products are products that been created in the last 30 days.
i have tried to get this by meta_query, but could not find the right key, or maybe it is the worng way.

$args = array(
            'post_type'      => 'product',
            'posts_per_page' => 7,
            'tax_query' => array(
                array(
                    'taxonomy'      => 'product_cat',
                    'field' => 'term_id',
                    'terms'         => $terms,
                    'operator'      => 'IN'
                )),
                    'meta_query'     => array(
                        array( // Simple products type
                            'key'           => '[ tried a key like '_sale_price' which give you products onsale]',
                            'value'         => 0,
                            'compare'       => '>',
                            'type'          => 'numeric'
                        ),
                    ),
            );
            $loop = new WP_Query( $args );

any suggestion please?

2

Answers


  1. Chosen as BEST ANSWER

    Thanks a lot Alexandr i needed it dynamic so i changed it a bit, this way the date is updated all the time, and its works :)

    $args = array(
                    'post_type'      => 'product',
                    'posts_per_page' => 7,
                    'date_query' => array(
                        array(
                            'after'    => array(
                                'year'  => date("Y"),
                                'month' => date('n')-1,
                                'day'   => date('d'),
                            ),
                            'inclusive' => true,
                        ),
                    ),
                    
                );
    

    this way im getting the latest new products in the past 1 mount


  2. I think you should not use ‘meta query’ because the product create date contains in the ‘post’ table not in the ‘post meta’. You have to try use ‘date_query’. Something like this:

    $args = array(
        'post_type'      => 'product',
        'posts_per_page' => 7,
        'date_query' => array(
            array(
                'after'    => array(
                    'year'  => 2021,
                    'month' => 3,
                    'day'   => 9,
                ),
                'inclusive' => true,
            ),
        ),
        'posts_per_page' => -1,
    );
    $query = new WP_Query( $args );
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search