Aleš Sýkora / November 28, 2023 / 8 comments

Oxygen Builder: Advanced Custom Query examples

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. 🍻

8 comments

  • 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.

  • 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..

  • 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?

  • A

    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.

  • A

    Can you try this in code block? Query is working for me, so it’s propably wrongly constructed in Oxygen Query builder.

    
    <?php
    $args = array(
        'post_type' => 'product',
        'meta_key' => 'total_sales',
        'orderby' => 'meta_value_num',
        'posts_per_page' => 1,
    );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post(); 
    global $product; ?>
    <div>
    <a href="<?php the_permalink(); ?>" id="id-<?php the_id(); ?>" title="<?php the_title(); ?>">
    
    <?php if (has_post_thumbnail( $loop->post->ID )) 
            echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog'); 
            else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="product placeholder Image" width="65px" height="115px" />'; ?>
    
    <h3><?php the_title(); ?></h3>
    </a>
    </div>
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>
    
  • A

    Hello, it should works like in WP Query.

    
    $args = array(
        'post_type' => array( 'post' ),
        'orderby' => 'ASC',
        'post__in' => $rel
    );
    
    $loop = new WP_Query( $args );

    Can you try to print the array values from the function in code block, if it works?

    echo print_r($rel); // Array ( [0] => 63 [1] => 87 )
  • A

    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

Share Your Thoughts