Looper Query String: Featured Products

Hello there, I know that according to this post, there isn’t a way to use the “Query Builder” to return featured products from Woocommerce. However, with the addition of the “Query String” options in Pro 4.1, I wonder if there is.

I’ve tried to scour the internet for a solution to this, but as of yet, the closest I’ve come is this Stack Overflow answer. However, the answer provided is not a query string to be parsed by the WP-Query and I’m not exactly sure how to transfer the information there into the Query String field.

This is the closest I’ve come up with that makes sense to me: post_type=product&tax_query=array(taxomony=product_visibility&field=name&terms=featured&operator=IN), but I’m not sure the syntax to put the tax_query array in the string. What am I doing wrong?

Hey, @bobbybosler! Thanks for writing in…longterm we definitely are wanting to get in either some new Provider Types for Loopers or add a way for WooCommerce-specific queries in the Query Builder to access certain features like Featured Products, Cross sells, Upsells, et cetera. That being said, we do not have an exact timeline on when we will get this in, so I wanted to take a moment to see if I could get a query string together for you, and I believe I’ve got something working! Check out the following:

post_type=product&post_status=publish&ignore_sticky_posts=1&posts_per_page=2&orderby=rand&order=asc&tax_query%5B0%5D%5Btaxonomy%5D=product_visibility&tax_query%5B0%5D%5Bfield%5D=name&tax_query%5B0%5D%5Bterms%5D=featured&tax_query%5B0%5D%5Boperator%5D=IN

So there’s a few points of note here: definitely make sure to update the posts_per_page, orderby, and order query strings to the appropriate values you want. I’ve currently just set these to 2, rand, and asc by default, but you may want to have a larger count or have them ordered alphabetically by title, et cetera.

The main tricky bit was getting the tax_query working, as it’s a nested array, which really doesn’t translate well (or readably, haha) to query strings. That being said, you shouldn’t really need to update anything related to the multiple tax_query pieces, so you can mostly ignore that bit.

I tested this locally and it seems to be working great for me, so let me know how that works…hopefully that helps to point you in the right direction!

4 Likes

@kory, you da man!! Thank you so much! I knew the problem was nesting the arrays, I just had no idea how to go about doing it from a syntax standpoint. Works like a charm!

@bobbybosler, no problem…happy to help. :slight_smile: For a little more context on how I got that result, you might find this helpful if you need to create additional complex queries in the future…

First, you’ll need a PHP environment to work in. If you’re developing locally, you can just throw any of this in your child theme’s functions.php file to ultimately get some output to work with. Start by writing out a query using the standard WP_Query syntax like so:

$tax_query[] = array(
    'taxonomy' => 'product_visibility',
    'field'    => 'name',
    'terms'    => 'featured',
    'operator' => 'IN',
);

$query = array(
    'post_type'           => 'product',
    'post_status'         => 'publish',
    'ignore_sticky_posts' => 1,
    'posts_per_page'      => 2,
    'orderby'             => 'rand',
    'order'               => 'asc',
    'tax_query'           => $tax_query
);

Then, output that somehow utilizing http_build_query(), which will format our string accordingly:

var_dump( http_build_query( $query ) );

Ultimately, that will give you the nasty string you see above, haha. You can rinse and repeat as necessary for anything else that you can’t (for now) access via the given Looper Providers.

Hopefully that helps you or anyone else trying to do something this way get a result more quickly. Cheers!

5 Likes

@kory,

Wow! Thank you not only for giving the answer, but for teaching how to get there in the process!

1 Like

No problem, @bobbybosler! Hopefully it helps you / others moving forward. Have a wonderful weekend! :slight_smile:

1 Like

Hi,

I tried using the above query string to populate upsells but it didn’t work. I tested it for featured products and it works - so my loop is working. What I did is, I swapped the term ‘featured’ in the string with ‘upsell’, ‘up-sell’, and ‘upsells’, but none of them worked. Am I using the wrong taxonomy term for upsells or do I need to change some other terms in this is query string to make it work?

Thanks in advance for the support!

Hi @gunesinan,

WooCommerce upsells work a bit differently. Instead of associating individual products with a “featured” term, WooCommerce tracks direct relationships of products by storing their IDs in post meta. Regretfully, we don’t have a similar workaround for this one. Both featured and upsells are planned features, so we will be finding a way to support them officially.

1 Like

Hi @alexander,
Thanks for the response. In @kory’s original response, I understood we can use the same string for upsells, but that’s fine.
Thanks again!

No problem! We were just trying to communicate earlier that it’s a priority to solve those use cases natively (without customization). We just happened to be lucky that a query string could be substituted for the featured products query.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.