Get all products under a category

I’ve recently encountered a project that needs to get all products under a category. Creating my own product filter using ajax. So now I’m going to share to you a simple script on how to filter WooCommerce wp_query loop and retrieve all products under the category you have chosen.

You have to make sure that whenever you’re dealing with taxonomies you have to use tax_query

Tax query has these parameters needed to work but could be more once you started using multiple tax_query or nested tax_query. more info

taxonomy – The custom post type category you’re using. mine is product_cat which is the WooCommerce taxonomy.

field – Whether you’re gonna use slug or post id.

terms – The container array of your selected field.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.