1

I want to use Advanced Custom Fields to determine from which category products will be shown. The ACF field gives the ID of the category and from that I can get the slug. I can't get it to work in the array though, and so it shows all products on the page. Does anyone have suggestions or ideas why it's not working? Thanks in advance!

$term_id = get_field('kies_product_categorie'); //Get category id
$term = get_term_by('id', $term_id, 'product_cat'); //Get terms from given category

$args = array( 
    'post_type' => 'product', 
    'posts_per_page' => 9, 
    'orderby' => 'date',
    'tax_query' => array(
      array(
        'taxonomy' => 'product_cat',
        'terms' => $term->slug //Slug of given category
      )
    )
);
1
  • What does $term return? Commented Jun 4, 2020 at 18:18

1 Answer 1

1

Looking at the documentation here, you appear to be missing the field attribute within the tax_query array.

So your $args should look like:

$args = array( 
    'post_type' => 'product', 
    'posts_per_page' => 9, 
    'orderby' => 'date',
    'tax_query' => array(
      array(
        'taxonomy' => 'product_cat',
        'terms' => $term->slug, //Slug of given category
        'field' => 'slug'
      )
    )
);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.