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?

1 Like

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!

7 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!

1 Like

@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!

6 Likes
WP query array not quite right
Can I delimit a ACF custom post type with taxonomy choices made in a separate repeater?
Query String - Custom Post Type and Taxonomy ACF
Looper: filter by Woocommerce Featured product
Convert PHP String to JSON String or HTTP query string
Looper Provider - Order By Price
Looper provider wp query string for manual post ids
How to create a CPT archive layout with a simple but mighty text filter function?
Archive Template - Current Page Child and Sub-child
Using F j, Y date format in loopers
Custom Single layout - display event filtered for current item
Dynamically filter looper content with terms buttons
Looper provider sitemap or chart
Using Multi-Select ACF in Query String
Modern Slider with MeCalendar
Term Array Sorting
ACF Query string
Using Looper Provides and Looper Consumers with Custom Post Type and Custom Taxonomies
Custom grid with looper for WooCommerce products
Blog posts plus portfolio items in one library?
MEC Event Data using Dynamic Content (DC)
Loop within currently displayed taxonomie
Archive Page for Custom Post Type
Past Events/Future (Current Events) out of order in Query Builder
Query String question
Woocommerce Linked Product Looper
Another Date Query Question
Looper query string | show recent posts from a category with a current tag name
Layout that Includes Two Custom Post Types and Filters
Change Query / Conditional Logic Sequence?
Popular Posts using Loopers
Using a nested repeater
Looper Provider / Consumer Placeholder
Query String with __not_in
MEC Events Looper Questions
Query string for the same term in a custom taxonomy
Lopping over and returning pages with ACF checkbox selected
Best Practice? Looper with custom meta date
Create Custom Search Results
Looper Query String
Condition Date only before and after
Layouts/Loopers: How to display posts based on an ACF date picker?
Looper - Products in stock
Looper Query String crashing site
Looper provider: Sort by one custom field, filter by another?
Looper and dynamic url?
Looper empty when using multiple terms and types
How to exclude current post from recent posts

@kory,

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

2 Likes

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

2 Likes

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.