X
X
xstaff2018-10-15 13:36:36
WordPress
xstaff, 2018-10-15 13:36:36

How to display only child products of Woocommerce?

Hello! Can you please tell me how can I display only child elements in a product category without products of subcategories? That is, there are:
- Category 1

    • -Subcategory1
    • -Item3
    • -Item4

  • -Subcategory2
  • -Subcategory3
  • -Item1
  • -Item2

I'm trying with code:
$args = array(
    'posts_per_page' => -1,
    'post_type' => 'product',
    'orderby' => 'title',
    'tax_query'     => array(
        array(
            'taxonomy'  => 'product_cat',
            'field'     => 'slug',
            'terms'     => 'slug-Категория1',
        )
    )
);
$the_query = new WP_Query( $args );

In $the_query all products are Category1 (Product1-4), but only Products1 and 2 are needed. What attribute is needed to take only direct descendants??? Or vice versa filters to exclude Products from subcategories?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
X
xstaff, 2018-10-18
@xstaff

Thank you, but a little not that) or I did not fully understand. category is the categories of posts, and I need the child elements of the woocommerce product category. Figured it out if anyone needs it:

$args = array(
    'posts_per_page' => -1,
    'post_type' => 'product',
    'orderby' => 'title',
    'tax_query'     => array(
        array(
            'taxonomy'  => 'product_cat',
            'field'     => 'slug',
            'terms'     => 'SlugName',
            'include_children' => 0
        )
    )
);
$the_query = new WP_Query( $args );

P
Pychev Anatoly, 2018-10-15
@pton

look here
bzp1qrfhsyyrdbxkq8g-t0w3lzc.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question