• Resolved donderhaas

    (@donderhaas)


    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, Nikor

    Now 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… 🙂

Viewing 2 replies - 1 through 2 (of 2 total)
  • If you put a query in the code where you indicated, you will be running a query inside a loop, inside another loop. That is a recipe for an overloaded server.

    Try this approach instead. Before the first loop, use a custom SQL query to retrieve all posts in the custom type along with their custom taxonomy ids.

    Loop through those posts and create an array keyed on custom taxonomy id where each id is the key to an array of posts associated with that id.

    Where you indicated you wanted a query, use the current custom taxonomy id to get the array of posts and loop through that array.

    Thread Starter donderhaas

    (@donderhaas)

    Thanks for the reply vtxyzzy. I tried a few different options and actually found the perfect solution here…

    View solution that worked

    The code block where it gets mentioned: “I’m using MichaelH code” via Ryan Riatno. Works perfect!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How can I loop through a custom post type based on a custom taxonomy id?’ is closed to new replies.