0

I am currently building my own custom advanced search functionality for a WordPress website with Woocommerce. You should be able to search using:

  • Category
  • Min/Max price

My current progress is attached below. This enables you to specify category slugs in the URL parameter. Returned will be posts that matches:

/**
 * Default arguments
 * @var array
 */

    $query = array(
        'post_status' => 'publish',
        'post_type' => 'product',
        'posts_per_page' => 10,
    );

/**
 * Category search
 */

    if(isset($_GET['categories']) && $_GET['categories']) {
        /**
         * Comma seperated --- explode
         * @var [type]
         */

            $categories = explode(',', $_GET['categories']);

        /**
         * Add "or" parameter
         */

            $query['tax_query']['relation'] = 'OR';

        /**
         * Add terms
         */

            foreach($categories as $category) {
                $query['tax_query'][] = array(
                        'taxonomy' => 'product_cat',
                        'field' => 'slug',
                        'terms' => $category,
                    );
            }
    }
/**
 * Fetch
 */

    $wp_query = new WP_Query($query);

Now, while this works great when you're searching for categories it seems it gets way more complicated when you need to search through prices.

In raw SQL, something like below would work:

SELECT DISTINCT ID, post_parent, post_type FROM $wpdb->posts
INNER JOIN $wpdb->postmeta ON ID = post_id
WHERE post_type IN ( 'product', 'product_variation' ) AND post_status = 'publish' AND meta_key = '_price' AND meta_value BETWEEN 200 AND 1000

I have no idea how I could implement this using WP_Query.

  • 1
    Have you seen meta_query argument of WP_Query? Also, you may be interested in pre_get_posts instead of running a secondary query. About the question of WC_Query we can not tell you what is better as it is a class from third party software, which is off-topic here. – cybmeta Feb 6 '15 at 14:41
3

The solution:

$query = array(
    'post_status' => 'publish',
    'post_type' => 'product',
    'posts_per_page' => 10,
    'meta_query' => array(
    array(
        'key' => '_price',
        'value' => array(50, 100),
        'compare' => 'BETWEEN',
        'type' => 'NUMERIC'
        ),
    ),
);

$wpquery = WP_Query($query); // return 10 products within the price range 50 - 100

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.