Search Divi Plus Documentation

Search for answers or browse our knowledge base.
Explore Divi Plus's Live Demo

All Topics
Print

Alter Woo Products Query Args

apply_filters('dipl_woo_products_args', array $args)

Filters the query args.


Parameters

$args
    (array) Query args

More Information

This filter is used to alter the query args of the products loop before the products are being retrieved from the database and before it is printed to the screen.

Usage

<?php
/**
* Example usage for dipl_woo_products_args filter.
* Getting products in lowest to highest price.
*/
add_filter( 'dipl_woo_products_args', 'dp_get_products_order_by_price', 1 );
function dp_get_products_order_by_price( $args ) {
$args['orderby'] = 'meta_value_num';
$args['meta_key'] = '_price';
$args['order'] = 'ASC';
return $args;
}

More Examples

<?php
/**
* Getting products in min max price range.
*/
add_filter( 'dipl_woo_products_args', 'dp_get_products_by_min_max_price', 1 );
function dp_get_products_by_min_max_price( $args ) {
$min_price = isset( $_GET['min_price'] ) ? floatval( wp_unslash( $_GET['min_price'] ) ) : 0;
$max_price = isset( $_GET['max_price'] ) ? floatval( wp_unslash( $_GET['max_price'] ) ) : PHP_INT_MAX;
if ( wc_tax_enabled() && 'incl' === get_option( 'woocommerce_tax_display_shop' ) && ! wc_prices_include_tax() ) {
$tax_class = apply_filters( 'woocommerce_price_filter_widget_tax_class', '' ); // Uses standard tax class.
$tax_rates = WC_Tax::get_rates( $tax_class );
if ( $tax_rates ) {
$min_price -= WC_Tax::get_tax_total( WC_Tax::calc_inclusive_tax( $min_price, $tax_rates ) );
$max_price -= WC_Tax::get_tax_total( WC_Tax::calc_inclusive_tax( $max_price, $tax_rates ) );
}
}
$meta_query = array(
'key' => '_price',
'value' => array( $min_price, $max_price ),
'compare' => 'between',
'type' => 'numeric'
);
if ( isset( $args['meta_query'] ) ) {
array_push( $args['meta_query'], $meta_query );
$args['meta_query']['relation'] = 'AND';
} else {
$args['meta_query'] = array(
$meta_query
);
}
return $args;
}
Table of Contents