Aleš Sýkora / November 28, 2023 / 8 comments
Oxygen Builder: Advanced Custom Query examples
1 min read / WordPress / Share on: Twitter, LinkedIn, Facebook
Post summary: Current taxonomy query Suits good for Taxonomy archives for example. Top selling products in woocommerce category Needed args to get best products: Args to get current taxonomy: And together:
Current taxonomy query
Suits good for Taxonomy archives for example.
$args = array(
'post_type' => 'jobs',
'tax_query' => array(
array(
'taxonomy' => 'job-category',
'field' => 'term_id',
'terms' => get_queried_object_id()
)
),
);
Top selling products in woocommerce category
Needed args to get best products:
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'order' => 'DESC',
);
Args to get current taxonomy:
$args = array(
'post_type' => 'product',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => get_queried_object_id()
)
),
);
And together:
$args = array(
'posts_per_page' => -1,
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => get_queried_object_id()
)
),
);
Fuel my passion for writing with a beer🍺
Your support not only makes me drunk but also greatly motivates me to continue creating content that helps. Cheers to more discoveries and shared success. 🍻
Nick
Is there a way to get only the category name within a repeater loop? I’m working on a product card, & I need each card to display the one category name & that category image. These are created with a custom post type. I tried to use an advanced query, but it didn’t work. I’d like to display all category names except for the empty ones. Here’s the current query I’m using:
‘post_type’ => ‘resources’,
‘tax_query’ => array(
array(
‘taxonomy’ => ‘resource-cat’,
‘field’ => ‘slug’,
‘operator’ => ‘IN’,
),
),
Hashem
Is there any way we can add dynamic post ids to a post__in? Looks like oxygen query builder won’t accept an array returned from a function ?. I was using favourites plugin and it gives you an array that keeps changing based on what users like or unlike.
Nitin
This is great post but the best selling products query isn’t working for me…I’ve added the same query in the repeater but nothing is showing in the front it’s completely blank..I want to create custom layout for best selling products like we create for blog posts…Please let me if this is possible..i was using woocommerce shortcode for best selling, it kinda ugly…I want completely custom layout..
JC
Thank you so much. This is most useful and perhaps most undocumented feature in OxygenBuilder.
I struggle to form query with it. I need to build a repeater that queries taxonomy terms and needs to get some taxonomy meta (defined through MetaBox).
Do I need to query it for custom post type, like in your first example, or just begin with tax_query itself?
Aleš Sýkora
Hello, you need to query the CPT and use code block in the card with get_the_terms function, which will get the category name and image and etc. For example this code will give you first term of the post in query.
Aleš Sýkora
Can you try this in code block? Query is working for me, so it’s propably wrongly constructed in Oxygen Query builder.
Aleš Sýkora
Hello, it should works like in WP Query.
Can you try to print the array values from the function in code block, if it works?
Aleš Sýkora
Hey! So you need to display all assigned terms to the custom post? I don’t think you need a repeater for this but code block and get_terms function.
get_terms – WordPress Codex