Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function woocommerce_pre_get_posts( $query ) {
- if ( ! is_admin() && is_post_type_archive( 'product' ) && $query->is_main_query() ) {
- // Option 1
- $args = array(
- array(
- 'meta_key' => '_price',
- 'value' => array( 50, 500 ),
- 'compare' => 'BETWEEN'
- )
- );
- // Option 2
- $args = array(
- array(
- 'meta_key' => '_price',
- 'value' => array( 50, 500 ),
- 'compare' => 'BETWEEN',
- 'type' => 'NUMERIC'
- )
- );
- // Option 3
- $args = array(
- 'relation' => 'AND',
- array(
- 'meta_key' => '_price',
- 'value' => 50,
- 'compare' => '>=',
- ),
- array(
- 'meta_key' => '_price',
- 'value' => 500,
- 'compare' => '<=',
- )
- );
- $query->set( 'meta_query', $args );
- }
- }
- add_action( 'pre_get_posts', 'woocommerce_pre_get_posts', 20 );
Add Comment
Please, Sign In to add comment