How can I loop through a custom post type based on a custom taxonomy id?
-
I have a custom post type that uses a custom taxonomy.
Let’s say the custom taxonomy is called ‘equipment’ and the equipment can have sub categories for example:
Parent Category: Camera
– Sub Category: Canon, Nikon, Sony
Parent Category: Lens
– Sub Category: Sigma, Tamron, Canon, NikorNow I want to create a page that first loops through the terms of the custom taxonomy and check which are parents and which are children in the loop (see the code example below).
So far everything is working well, but now I am struggling to loop through all my custom post types that use this custom taxonomy based on the taxonomy child term’s id.
Basically it will then look something like this:
Camera
– Canon
– All posts listed that use the ‘Canon’ term…
– Nikon
– All posts listed that use the ‘Nikon’ term…
– Sony
– All posts listed that use the ‘Sony’ term…
Lens
– Sigma
– All posts listed that use the ‘Sigma’ term…
– Tamron
– All posts listed that use the ‘Tamron’ term…
– Canon
– All posts listed that use the ‘Canon’ term…Here is the code that works…where I need the query to check for taxonomy term id’s…
$args=array( 'orderby' => 'name', 'order' => 'ASC', 'taxonomy' => 'equipment', 'hierarchical' => 1 ); $categories=get_categories($args); foreach($categories as $category) { if ($category->parent == 0) { echo 'Parent: ' . $category->name; $args=array( 'child_of' => $category->term_id, 'orderby' => 'name', 'order' => 'ASC', 'taxonomy' => 'capibility', 'hierarchical' => 1 ); $childcategories=get_categories($args); foreach($childcategories as $childcategory) { echo 'Child: ' . $childcategory->name; /* Insert Query Here to check the custom post type based on the custom taxonomy's id to be used in the loop???? */ } } }
Please help… ??
- The topic ‘How can I loop through a custom post type based on a custom taxonomy id?’ is closed to new replies.