sorting by product price in wp_query() and keep priceless products

17 Views Asked by At

I want my products to be sorted by "_price" in the category, which in this case does not display the items whose price is equal to 0.

$args = array(
            'post_type'      => 'product',
            'posts_per_page' => 16,
            'order'          => 'DESC',      
            'post_status'    => 'publish',
            'orderby' => 'meta_value_num',
             'meta_key' => '_price',
        );

        if (is_product_category()) {
            $args['tax_query'] = array(
                array(
                    'taxonomy' => 'product_cat',
                    'field' => 'term_id',
                    'terms' => get_queried_object_id(),
                ),
            );
        }
function product_category_load_more()
{
    $category_id = isset($_GET['category_id']) ? intval($_GET['category_id']) : 0;

    $args = array(
        'post_type' => 'product',
        'posts_per_page' => 16,
        'offset' => ((intval($_GET['page']) - 1) * 16),
        'order' => 'DESC',
        'post_status' => 'publish',
        'orderby' => 'meta_value_num',
        'meta_key' => '_price',
    );
...
}

But I want to display the ones whose price is greater than 0 as DESC, then when the products with prices are over, display those whose price is equal to 0 Of course, I have another problem. It is that I have a load more button for loading ajax products.

0

There are 0 best solutions below