Support » Plugin: Relevanssi - A Better Search » WooCommerce wc_get_products

  • Risto Niinemets



    thanks for the great plugin.
    I tried to find if anyone asked the same thing, but didn’t get any results.

    WooCommerce is changing quite a bit and changing the way it searches products (or stores in the database), so the recommended way for search programmatically would be using wc_get_products() function, WC_Product_Query class or via Data_Store class (

    Is there a way to implement Relevanssi to those (or one of) searches?

    I have a custom “live search”, where I don’t want to depend on WP_Query as it’s not futureproof with WooCommerce.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Mikko Saari


    Is there any documentation available for the upcoming changes in the WooCommerce? I don’t really have the time to go hunting for it, but if there’s any information available on what to expect, of course I’m interested in keeping Relevanssi compatible with WooCommerce.

    Based on a quick look that WooCommerce function mostly just solves problems that Relevanssi has solved for WooCommerce users before, ie. looking at custom fields and providing a better search experience.

    Thread Starter Risto Niinemets


    Not sure about specific documentation, but here’s some on two of the mentioned functions/methods:

    The functions are already in use so the future is here 🙂

    wc_get_products and WC_Product_Query provide a standard way of retrieving products that is safe to use and will not break due to database changes in future WooCommerce versions. Building custom WP_Queries or database queries is likely to break your code in future versions of WooCommerce as data moves towards custom tables for better performance. This is the best-practices way for plugin and theme developers to retrieve multiple products. wc_get_products and WC_Product_Query are similar to WordPress get_posts and WP_Query. Just like those, you pass in an array of arguments defining the criteria for the search.

    So far they’ve added a product_meta_lookup table to store stock status and some other product related core data.

    Plugin Author Mikko Saari


    Ah, I see, you don’t know how Relevanssi works. Relevanssi doesn’t query posts directly using WP_Query or get_posts(); instead, Relevanssi builds its own index and queries that.

    As long as WooCommerce products are posts in the wp_posts database table with the metadata stored in wp_postmeta and taxonomies, Relevanssi will continue to work with WooCommerce without major issue.

    However, if at some point WooCommerce products stop being posts, Relevanssi won’t work with WooCommerce anymore, because Relevanssi can only work with WordPress posts. At that point I’ll have to see what WooCommerce does – it may be Relevanssi can be compatible with WooCommerce, but it may be it won’t.

    In any case, that’s such a breaking change it won’t happen until WooCommerce 5, and that won’t happen without a lot of advance notice, as it’ll utterly break down the whole ecosystem, so I’ll see how that works when release candidates are available; until I know how they end up building it, I can’t tell. Those interfaces they’re providing now aren’t helpful, as they don’t tell how things are beneath them, and Relevanssi won’t likely be using them in any case.

    Thread Starter Risto Niinemets


    Thanks for the explanation.

    So basically as long as WooCommerce product are stores as posts, I should be able to pull search results from Relevanssi instead of built-in search, even if using
    wc_get_products(['s' => $keyword, 'stock_status' => 'instock', 'limit' => 3]) or
    new WC_Product_Query(['s' => $keyword, 'stock_status' => 'instock', 'limit' => 3])->get_products() or
    \WC_Data_Store::load('product')->search_products($keyword, '', false, false, 3)

    But that doesn’t happen.

    First and second calls are same, first one is just a wrapper for the second one. Both use WP_Query at the moment as products are in posts table, but problem is that the query itself is never returned so it could not be passed on to relevanssi_do_query.

    As for the third option, currently I don’t see how Relevanssi interferes with the results, WooCommerce makes a direct database call to it’s lookup tables. There is an option though

    I just might be missing something…

    Plugin Author Mikko Saari


    I’m sure that won’t invoke Relevanssi. Relevanssi interacts with the WP search by taking over the results from the_posts filter hook, and those do not fire that.

    Just create a WP_Query object with the parameters you want and pass that to Relevanssi:

    $query = new WP_Query();
    $query->parse_query( array(
        's' => $keyword,
        'posts_per_page' => 3,
        'tax_query' => array(
                'product_visibility' => 'instock',
    ) );
    relevanssi_do_query( $query );

    Now you have the results in $query->posts. (I’m not 100% sure of how to pass the instock parameter correctly, but I think that’s about right.)

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.