Display subcategories of custom categories - WordPress





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















This is what I have:



    $args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'directory-category',
'pad_counts' => false
);


Which gets me the categories.



What I want is to get the child categories of this directory-category taxonomy.



Any ideas on how to do that?




I'm not asking for a solution, just an advice or someone to show me the road.



Googling didn't help :/




Here is a screenshot HERE










share|improve this question































    2















    This is what I have:



        $args = array(
    'type' => 'post',
    'child_of' => 0,
    'parent' => '',
    'orderby' => 'name',
    'order' => 'ASC',
    'hide_empty' => 1,
    'hierarchical' => 1,
    'exclude' => '',
    'include' => '',
    'number' => '',
    'taxonomy' => 'directory-category',
    'pad_counts' => false
    );


    Which gets me the categories.



    What I want is to get the child categories of this directory-category taxonomy.



    Any ideas on how to do that?




    I'm not asking for a solution, just an advice or someone to show me the road.



    Googling didn't help :/




    Here is a screenshot HERE










    share|improve this question



























      2












      2








      2


      1






      This is what I have:



          $args = array(
      'type' => 'post',
      'child_of' => 0,
      'parent' => '',
      'orderby' => 'name',
      'order' => 'ASC',
      'hide_empty' => 1,
      'hierarchical' => 1,
      'exclude' => '',
      'include' => '',
      'number' => '',
      'taxonomy' => 'directory-category',
      'pad_counts' => false
      );


      Which gets me the categories.



      What I want is to get the child categories of this directory-category taxonomy.



      Any ideas on how to do that?




      I'm not asking for a solution, just an advice or someone to show me the road.



      Googling didn't help :/




      Here is a screenshot HERE










      share|improve this question
















      This is what I have:



          $args = array(
      'type' => 'post',
      'child_of' => 0,
      'parent' => '',
      'orderby' => 'name',
      'order' => 'ASC',
      'hide_empty' => 1,
      'hierarchical' => 1,
      'exclude' => '',
      'include' => '',
      'number' => '',
      'taxonomy' => 'directory-category',
      'pad_counts' => false
      );


      Which gets me the categories.



      What I want is to get the child categories of this directory-category taxonomy.



      Any ideas on how to do that?




      I'm not asking for a solution, just an advice or someone to show me the road.



      Googling didn't help :/




      Here is a screenshot HERE







      wordpress field parent-child categories






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 9 '16 at 8:25







      johnchar

















      asked Oct 8 '16 at 22:13









      johncharjohnchar

      421720




      421720
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You said you didn't want a direct answer, but essentially you want to use get_terms found here:



          https://developer.wordpress.org/reference/functions/get_terms/



          SELECT * from prod_term_taxonomy WHERE parent = 0;



          UPDATE:



          // Using your specific parent taxonomy id of 214 the query is below
          global $wpdb;
          $results = $wpdb->get_results("SELECT * from prod_term_taxonomy WHERE taxonomy = 'directory-category'");

          // then you can use WordPress get_term to query each object to get it's name based on it's term_id. $results will be an array of objects so you will use a foreach loop to loop through to get each $result like this...

          $child_cat_array = array();

          foreach ($results as $result) {
          $term = get_term( $result->term_id, $taxonomy );
          $name = $term->name;
          // the slug will be used for querying for posts
          $slug = $term->slug;
          // this will push the slug of the child category into the array for querying posts later
          array_push($child_cat_array, $slug);
          }


          You can then modify your get_posts query like this:



          $args = array(
          'tax_query' => array(
          array(
          'taxonomy' => 'directory-category',
          'field' => 'slug',
          'terms' => $child_cat_array
          )
          )
          );

          $postslist = get_posts( $args );





          share|improve this answer


























          • Are you looking to get the posts associated with those child categories at all?

            – Alex Rindone
            Oct 9 '16 at 3:13











          • Thanks for your suggestion. I have already checked get_terms() but I was looking for something, just a bit more specific, lol

            – johnchar
            Oct 9 '16 at 8:24











          • You may have to query the WordPress database with a global $wpdb. I can give you the answer if you'd like :) are you familiar with SQL? Because you would query for the exact table where they live in your WordPress database. There isn't a built in WordPress function for retrieving them unfortunately.

            – Alex Rindone
            Oct 9 '16 at 12:19











          • But again, are you trying to dynamically get all of the child tags or the posts associated with them, or both?

            – Alex Rindone
            Oct 9 '16 at 12:21











          • Well yeah, I am trying to get both. It would be very lovely if you could do that! Please do give me the answer if you can. Thank you

            – johnchar
            Oct 9 '16 at 15:35





















          0














          If you want to display a list of Subcategories and the related posts based on a single category provided by you, you can use the following code. Make sure to use your own names of taxonomy, post_type and terms:



          function ow_subcategories_with_posts_by_category( $taxonomy, $post_type, $term ) {
          $category = get_term_by( 'slug', $term, $taxonomy );
          $cat_id = $category->term_id;

          // Get all subcategories related to the provided $category ($term)
          $subcategories = get_terms(
          array(
          'taxonomy' => $taxonomy,
          'parent' => $cat_id,
          'orderby' => 'term_id',
          'hide_empty' => true
          )
          );
          ?>
          <div>
          <?php
          // Iterate through all subcategories to display each individual subcategory
          foreach ( $subcategories as $subcategory ) {

          $subcat_name = $subcategory->name;
          $subcat_id = $subcategory->term_id;
          $subcat_slug = $subcategory->slug;

          // Display the name of each individual subcategory with ID and Slug
          echo '<h4>Subcategory: ' . $subcat_name . ' - ID: ' . $subcat_id . ' - Slug: ' . $subcat_slug . '</h4>';

          // Get all posts that belong to this specific subcategory
          $posts = new WP_Query(
          array(
          'post_type' => $post_type,
          'posts_per_page' => -1, // <-- Show all posts
          'hide_empty' => true,
          'order' => 'ASC',
          'tax_query' => array(
          array(
          'taxonomy' => $taxonomy,
          'terms' => $subcat_id,
          'field' => 'id'
          )
          )
          )
          );

          // If there are posts available within this subcategory
          if ( $posts->have_posts() ):
          ?>
          <ul>
          <?php
          while ( $posts->have_posts() ): $posts->the_post();
          //Show the title of each post with the Post ID
          ?>
          <li>Post: <?php the_title(); ?> - ID: <?php the_ID(); ?></li>
          <?php
          endwhile;
          ?>
          </ul>
          <?php
          else:
          echo 'No posts found';
          endif;

          wp_reset_query();
          }
          ?>
          </div>
          <?php
          }
          ow_subcategories_with_posts_by_category( 'name-of-your-taxonomy', 'name-of-your-post-type', 'name-of-your-specific-term' );


          'name-of-your-taxonomy' is the name of the main taxonomy. e.g.: 'victual_category'



          'name-of-your-post-type' is the name of your post type. e.g.: 'victual'



          'name-of-your-specific-term' is the name of the specific category that you want to use so that subcategories that belong to that category can be displayed. e.g.: 'food'



          So if I call the function:



          ow_subcategories_with_posts_by_category( 'victual_category', 'victual', 'food' );


          That would display all the subcategories with their respective Posts that belongs to Food of the Victual-Category taxonomy:



          Subcategory: Appetizers - ID: 35 - Slug: appetizers




          • Post: Chips & Salsa - ID: 464

          • Post: Queso - ID: 465


          Subcategory: Tacos - ID: 36 - Slug: tacos




          • Post: Al Pastor - ID: 466

          • Post: Fish Al Pastor - ID: 467






          share|improve this answer


























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39937889%2fdisplay-subcategories-of-custom-categories-wordpress%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            You said you didn't want a direct answer, but essentially you want to use get_terms found here:



            https://developer.wordpress.org/reference/functions/get_terms/



            SELECT * from prod_term_taxonomy WHERE parent = 0;



            UPDATE:



            // Using your specific parent taxonomy id of 214 the query is below
            global $wpdb;
            $results = $wpdb->get_results("SELECT * from prod_term_taxonomy WHERE taxonomy = 'directory-category'");

            // then you can use WordPress get_term to query each object to get it's name based on it's term_id. $results will be an array of objects so you will use a foreach loop to loop through to get each $result like this...

            $child_cat_array = array();

            foreach ($results as $result) {
            $term = get_term( $result->term_id, $taxonomy );
            $name = $term->name;
            // the slug will be used for querying for posts
            $slug = $term->slug;
            // this will push the slug of the child category into the array for querying posts later
            array_push($child_cat_array, $slug);
            }


            You can then modify your get_posts query like this:



            $args = array(
            'tax_query' => array(
            array(
            'taxonomy' => 'directory-category',
            'field' => 'slug',
            'terms' => $child_cat_array
            )
            )
            );

            $postslist = get_posts( $args );





            share|improve this answer


























            • Are you looking to get the posts associated with those child categories at all?

              – Alex Rindone
              Oct 9 '16 at 3:13











            • Thanks for your suggestion. I have already checked get_terms() but I was looking for something, just a bit more specific, lol

              – johnchar
              Oct 9 '16 at 8:24











            • You may have to query the WordPress database with a global $wpdb. I can give you the answer if you'd like :) are you familiar with SQL? Because you would query for the exact table where they live in your WordPress database. There isn't a built in WordPress function for retrieving them unfortunately.

              – Alex Rindone
              Oct 9 '16 at 12:19











            • But again, are you trying to dynamically get all of the child tags or the posts associated with them, or both?

              – Alex Rindone
              Oct 9 '16 at 12:21











            • Well yeah, I am trying to get both. It would be very lovely if you could do that! Please do give me the answer if you can. Thank you

              – johnchar
              Oct 9 '16 at 15:35


















            1














            You said you didn't want a direct answer, but essentially you want to use get_terms found here:



            https://developer.wordpress.org/reference/functions/get_terms/



            SELECT * from prod_term_taxonomy WHERE parent = 0;



            UPDATE:



            // Using your specific parent taxonomy id of 214 the query is below
            global $wpdb;
            $results = $wpdb->get_results("SELECT * from prod_term_taxonomy WHERE taxonomy = 'directory-category'");

            // then you can use WordPress get_term to query each object to get it's name based on it's term_id. $results will be an array of objects so you will use a foreach loop to loop through to get each $result like this...

            $child_cat_array = array();

            foreach ($results as $result) {
            $term = get_term( $result->term_id, $taxonomy );
            $name = $term->name;
            // the slug will be used for querying for posts
            $slug = $term->slug;
            // this will push the slug of the child category into the array for querying posts later
            array_push($child_cat_array, $slug);
            }


            You can then modify your get_posts query like this:



            $args = array(
            'tax_query' => array(
            array(
            'taxonomy' => 'directory-category',
            'field' => 'slug',
            'terms' => $child_cat_array
            )
            )
            );

            $postslist = get_posts( $args );





            share|improve this answer


























            • Are you looking to get the posts associated with those child categories at all?

              – Alex Rindone
              Oct 9 '16 at 3:13











            • Thanks for your suggestion. I have already checked get_terms() but I was looking for something, just a bit more specific, lol

              – johnchar
              Oct 9 '16 at 8:24











            • You may have to query the WordPress database with a global $wpdb. I can give you the answer if you'd like :) are you familiar with SQL? Because you would query for the exact table where they live in your WordPress database. There isn't a built in WordPress function for retrieving them unfortunately.

              – Alex Rindone
              Oct 9 '16 at 12:19











            • But again, are you trying to dynamically get all of the child tags or the posts associated with them, or both?

              – Alex Rindone
              Oct 9 '16 at 12:21











            • Well yeah, I am trying to get both. It would be very lovely if you could do that! Please do give me the answer if you can. Thank you

              – johnchar
              Oct 9 '16 at 15:35
















            1












            1








            1







            You said you didn't want a direct answer, but essentially you want to use get_terms found here:



            https://developer.wordpress.org/reference/functions/get_terms/



            SELECT * from prod_term_taxonomy WHERE parent = 0;



            UPDATE:



            // Using your specific parent taxonomy id of 214 the query is below
            global $wpdb;
            $results = $wpdb->get_results("SELECT * from prod_term_taxonomy WHERE taxonomy = 'directory-category'");

            // then you can use WordPress get_term to query each object to get it's name based on it's term_id. $results will be an array of objects so you will use a foreach loop to loop through to get each $result like this...

            $child_cat_array = array();

            foreach ($results as $result) {
            $term = get_term( $result->term_id, $taxonomy );
            $name = $term->name;
            // the slug will be used for querying for posts
            $slug = $term->slug;
            // this will push the slug of the child category into the array for querying posts later
            array_push($child_cat_array, $slug);
            }


            You can then modify your get_posts query like this:



            $args = array(
            'tax_query' => array(
            array(
            'taxonomy' => 'directory-category',
            'field' => 'slug',
            'terms' => $child_cat_array
            )
            )
            );

            $postslist = get_posts( $args );





            share|improve this answer















            You said you didn't want a direct answer, but essentially you want to use get_terms found here:



            https://developer.wordpress.org/reference/functions/get_terms/



            SELECT * from prod_term_taxonomy WHERE parent = 0;



            UPDATE:



            // Using your specific parent taxonomy id of 214 the query is below
            global $wpdb;
            $results = $wpdb->get_results("SELECT * from prod_term_taxonomy WHERE taxonomy = 'directory-category'");

            // then you can use WordPress get_term to query each object to get it's name based on it's term_id. $results will be an array of objects so you will use a foreach loop to loop through to get each $result like this...

            $child_cat_array = array();

            foreach ($results as $result) {
            $term = get_term( $result->term_id, $taxonomy );
            $name = $term->name;
            // the slug will be used for querying for posts
            $slug = $term->slug;
            // this will push the slug of the child category into the array for querying posts later
            array_push($child_cat_array, $slug);
            }


            You can then modify your get_posts query like this:



            $args = array(
            'tax_query' => array(
            array(
            'taxonomy' => 'directory-category',
            'field' => 'slug',
            'terms' => $child_cat_array
            )
            )
            );

            $postslist = get_posts( $args );






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 9 '16 at 16:28

























            answered Oct 9 '16 at 3:11









            Alex RindoneAlex Rindone

            24429




            24429













            • Are you looking to get the posts associated with those child categories at all?

              – Alex Rindone
              Oct 9 '16 at 3:13











            • Thanks for your suggestion. I have already checked get_terms() but I was looking for something, just a bit more specific, lol

              – johnchar
              Oct 9 '16 at 8:24











            • You may have to query the WordPress database with a global $wpdb. I can give you the answer if you'd like :) are you familiar with SQL? Because you would query for the exact table where they live in your WordPress database. There isn't a built in WordPress function for retrieving them unfortunately.

              – Alex Rindone
              Oct 9 '16 at 12:19











            • But again, are you trying to dynamically get all of the child tags or the posts associated with them, or both?

              – Alex Rindone
              Oct 9 '16 at 12:21











            • Well yeah, I am trying to get both. It would be very lovely if you could do that! Please do give me the answer if you can. Thank you

              – johnchar
              Oct 9 '16 at 15:35





















            • Are you looking to get the posts associated with those child categories at all?

              – Alex Rindone
              Oct 9 '16 at 3:13











            • Thanks for your suggestion. I have already checked get_terms() but I was looking for something, just a bit more specific, lol

              – johnchar
              Oct 9 '16 at 8:24











            • You may have to query the WordPress database with a global $wpdb. I can give you the answer if you'd like :) are you familiar with SQL? Because you would query for the exact table where they live in your WordPress database. There isn't a built in WordPress function for retrieving them unfortunately.

              – Alex Rindone
              Oct 9 '16 at 12:19











            • But again, are you trying to dynamically get all of the child tags or the posts associated with them, or both?

              – Alex Rindone
              Oct 9 '16 at 12:21











            • Well yeah, I am trying to get both. It would be very lovely if you could do that! Please do give me the answer if you can. Thank you

              – johnchar
              Oct 9 '16 at 15:35



















            Are you looking to get the posts associated with those child categories at all?

            – Alex Rindone
            Oct 9 '16 at 3:13





            Are you looking to get the posts associated with those child categories at all?

            – Alex Rindone
            Oct 9 '16 at 3:13













            Thanks for your suggestion. I have already checked get_terms() but I was looking for something, just a bit more specific, lol

            – johnchar
            Oct 9 '16 at 8:24





            Thanks for your suggestion. I have already checked get_terms() but I was looking for something, just a bit more specific, lol

            – johnchar
            Oct 9 '16 at 8:24













            You may have to query the WordPress database with a global $wpdb. I can give you the answer if you'd like :) are you familiar with SQL? Because you would query for the exact table where they live in your WordPress database. There isn't a built in WordPress function for retrieving them unfortunately.

            – Alex Rindone
            Oct 9 '16 at 12:19





            You may have to query the WordPress database with a global $wpdb. I can give you the answer if you'd like :) are you familiar with SQL? Because you would query for the exact table where they live in your WordPress database. There isn't a built in WordPress function for retrieving them unfortunately.

            – Alex Rindone
            Oct 9 '16 at 12:19













            But again, are you trying to dynamically get all of the child tags or the posts associated with them, or both?

            – Alex Rindone
            Oct 9 '16 at 12:21





            But again, are you trying to dynamically get all of the child tags or the posts associated with them, or both?

            – Alex Rindone
            Oct 9 '16 at 12:21













            Well yeah, I am trying to get both. It would be very lovely if you could do that! Please do give me the answer if you can. Thank you

            – johnchar
            Oct 9 '16 at 15:35







            Well yeah, I am trying to get both. It would be very lovely if you could do that! Please do give me the answer if you can. Thank you

            – johnchar
            Oct 9 '16 at 15:35















            0














            If you want to display a list of Subcategories and the related posts based on a single category provided by you, you can use the following code. Make sure to use your own names of taxonomy, post_type and terms:



            function ow_subcategories_with_posts_by_category( $taxonomy, $post_type, $term ) {
            $category = get_term_by( 'slug', $term, $taxonomy );
            $cat_id = $category->term_id;

            // Get all subcategories related to the provided $category ($term)
            $subcategories = get_terms(
            array(
            'taxonomy' => $taxonomy,
            'parent' => $cat_id,
            'orderby' => 'term_id',
            'hide_empty' => true
            )
            );
            ?>
            <div>
            <?php
            // Iterate through all subcategories to display each individual subcategory
            foreach ( $subcategories as $subcategory ) {

            $subcat_name = $subcategory->name;
            $subcat_id = $subcategory->term_id;
            $subcat_slug = $subcategory->slug;

            // Display the name of each individual subcategory with ID and Slug
            echo '<h4>Subcategory: ' . $subcat_name . ' - ID: ' . $subcat_id . ' - Slug: ' . $subcat_slug . '</h4>';

            // Get all posts that belong to this specific subcategory
            $posts = new WP_Query(
            array(
            'post_type' => $post_type,
            'posts_per_page' => -1, // <-- Show all posts
            'hide_empty' => true,
            'order' => 'ASC',
            'tax_query' => array(
            array(
            'taxonomy' => $taxonomy,
            'terms' => $subcat_id,
            'field' => 'id'
            )
            )
            )
            );

            // If there are posts available within this subcategory
            if ( $posts->have_posts() ):
            ?>
            <ul>
            <?php
            while ( $posts->have_posts() ): $posts->the_post();
            //Show the title of each post with the Post ID
            ?>
            <li>Post: <?php the_title(); ?> - ID: <?php the_ID(); ?></li>
            <?php
            endwhile;
            ?>
            </ul>
            <?php
            else:
            echo 'No posts found';
            endif;

            wp_reset_query();
            }
            ?>
            </div>
            <?php
            }
            ow_subcategories_with_posts_by_category( 'name-of-your-taxonomy', 'name-of-your-post-type', 'name-of-your-specific-term' );


            'name-of-your-taxonomy' is the name of the main taxonomy. e.g.: 'victual_category'



            'name-of-your-post-type' is the name of your post type. e.g.: 'victual'



            'name-of-your-specific-term' is the name of the specific category that you want to use so that subcategories that belong to that category can be displayed. e.g.: 'food'



            So if I call the function:



            ow_subcategories_with_posts_by_category( 'victual_category', 'victual', 'food' );


            That would display all the subcategories with their respective Posts that belongs to Food of the Victual-Category taxonomy:



            Subcategory: Appetizers - ID: 35 - Slug: appetizers




            • Post: Chips & Salsa - ID: 464

            • Post: Queso - ID: 465


            Subcategory: Tacos - ID: 36 - Slug: tacos




            • Post: Al Pastor - ID: 466

            • Post: Fish Al Pastor - ID: 467






            share|improve this answer






























              0














              If you want to display a list of Subcategories and the related posts based on a single category provided by you, you can use the following code. Make sure to use your own names of taxonomy, post_type and terms:



              function ow_subcategories_with_posts_by_category( $taxonomy, $post_type, $term ) {
              $category = get_term_by( 'slug', $term, $taxonomy );
              $cat_id = $category->term_id;

              // Get all subcategories related to the provided $category ($term)
              $subcategories = get_terms(
              array(
              'taxonomy' => $taxonomy,
              'parent' => $cat_id,
              'orderby' => 'term_id',
              'hide_empty' => true
              )
              );
              ?>
              <div>
              <?php
              // Iterate through all subcategories to display each individual subcategory
              foreach ( $subcategories as $subcategory ) {

              $subcat_name = $subcategory->name;
              $subcat_id = $subcategory->term_id;
              $subcat_slug = $subcategory->slug;

              // Display the name of each individual subcategory with ID and Slug
              echo '<h4>Subcategory: ' . $subcat_name . ' - ID: ' . $subcat_id . ' - Slug: ' . $subcat_slug . '</h4>';

              // Get all posts that belong to this specific subcategory
              $posts = new WP_Query(
              array(
              'post_type' => $post_type,
              'posts_per_page' => -1, // <-- Show all posts
              'hide_empty' => true,
              'order' => 'ASC',
              'tax_query' => array(
              array(
              'taxonomy' => $taxonomy,
              'terms' => $subcat_id,
              'field' => 'id'
              )
              )
              )
              );

              // If there are posts available within this subcategory
              if ( $posts->have_posts() ):
              ?>
              <ul>
              <?php
              while ( $posts->have_posts() ): $posts->the_post();
              //Show the title of each post with the Post ID
              ?>
              <li>Post: <?php the_title(); ?> - ID: <?php the_ID(); ?></li>
              <?php
              endwhile;
              ?>
              </ul>
              <?php
              else:
              echo 'No posts found';
              endif;

              wp_reset_query();
              }
              ?>
              </div>
              <?php
              }
              ow_subcategories_with_posts_by_category( 'name-of-your-taxonomy', 'name-of-your-post-type', 'name-of-your-specific-term' );


              'name-of-your-taxonomy' is the name of the main taxonomy. e.g.: 'victual_category'



              'name-of-your-post-type' is the name of your post type. e.g.: 'victual'



              'name-of-your-specific-term' is the name of the specific category that you want to use so that subcategories that belong to that category can be displayed. e.g.: 'food'



              So if I call the function:



              ow_subcategories_with_posts_by_category( 'victual_category', 'victual', 'food' );


              That would display all the subcategories with their respective Posts that belongs to Food of the Victual-Category taxonomy:



              Subcategory: Appetizers - ID: 35 - Slug: appetizers




              • Post: Chips & Salsa - ID: 464

              • Post: Queso - ID: 465


              Subcategory: Tacos - ID: 36 - Slug: tacos




              • Post: Al Pastor - ID: 466

              • Post: Fish Al Pastor - ID: 467






              share|improve this answer




























                0












                0








                0







                If you want to display a list of Subcategories and the related posts based on a single category provided by you, you can use the following code. Make sure to use your own names of taxonomy, post_type and terms:



                function ow_subcategories_with_posts_by_category( $taxonomy, $post_type, $term ) {
                $category = get_term_by( 'slug', $term, $taxonomy );
                $cat_id = $category->term_id;

                // Get all subcategories related to the provided $category ($term)
                $subcategories = get_terms(
                array(
                'taxonomy' => $taxonomy,
                'parent' => $cat_id,
                'orderby' => 'term_id',
                'hide_empty' => true
                )
                );
                ?>
                <div>
                <?php
                // Iterate through all subcategories to display each individual subcategory
                foreach ( $subcategories as $subcategory ) {

                $subcat_name = $subcategory->name;
                $subcat_id = $subcategory->term_id;
                $subcat_slug = $subcategory->slug;

                // Display the name of each individual subcategory with ID and Slug
                echo '<h4>Subcategory: ' . $subcat_name . ' - ID: ' . $subcat_id . ' - Slug: ' . $subcat_slug . '</h4>';

                // Get all posts that belong to this specific subcategory
                $posts = new WP_Query(
                array(
                'post_type' => $post_type,
                'posts_per_page' => -1, // <-- Show all posts
                'hide_empty' => true,
                'order' => 'ASC',
                'tax_query' => array(
                array(
                'taxonomy' => $taxonomy,
                'terms' => $subcat_id,
                'field' => 'id'
                )
                )
                )
                );

                // If there are posts available within this subcategory
                if ( $posts->have_posts() ):
                ?>
                <ul>
                <?php
                while ( $posts->have_posts() ): $posts->the_post();
                //Show the title of each post with the Post ID
                ?>
                <li>Post: <?php the_title(); ?> - ID: <?php the_ID(); ?></li>
                <?php
                endwhile;
                ?>
                </ul>
                <?php
                else:
                echo 'No posts found';
                endif;

                wp_reset_query();
                }
                ?>
                </div>
                <?php
                }
                ow_subcategories_with_posts_by_category( 'name-of-your-taxonomy', 'name-of-your-post-type', 'name-of-your-specific-term' );


                'name-of-your-taxonomy' is the name of the main taxonomy. e.g.: 'victual_category'



                'name-of-your-post-type' is the name of your post type. e.g.: 'victual'



                'name-of-your-specific-term' is the name of the specific category that you want to use so that subcategories that belong to that category can be displayed. e.g.: 'food'



                So if I call the function:



                ow_subcategories_with_posts_by_category( 'victual_category', 'victual', 'food' );


                That would display all the subcategories with their respective Posts that belongs to Food of the Victual-Category taxonomy:



                Subcategory: Appetizers - ID: 35 - Slug: appetizers




                • Post: Chips & Salsa - ID: 464

                • Post: Queso - ID: 465


                Subcategory: Tacos - ID: 36 - Slug: tacos




                • Post: Al Pastor - ID: 466

                • Post: Fish Al Pastor - ID: 467






                share|improve this answer















                If you want to display a list of Subcategories and the related posts based on a single category provided by you, you can use the following code. Make sure to use your own names of taxonomy, post_type and terms:



                function ow_subcategories_with_posts_by_category( $taxonomy, $post_type, $term ) {
                $category = get_term_by( 'slug', $term, $taxonomy );
                $cat_id = $category->term_id;

                // Get all subcategories related to the provided $category ($term)
                $subcategories = get_terms(
                array(
                'taxonomy' => $taxonomy,
                'parent' => $cat_id,
                'orderby' => 'term_id',
                'hide_empty' => true
                )
                );
                ?>
                <div>
                <?php
                // Iterate through all subcategories to display each individual subcategory
                foreach ( $subcategories as $subcategory ) {

                $subcat_name = $subcategory->name;
                $subcat_id = $subcategory->term_id;
                $subcat_slug = $subcategory->slug;

                // Display the name of each individual subcategory with ID and Slug
                echo '<h4>Subcategory: ' . $subcat_name . ' - ID: ' . $subcat_id . ' - Slug: ' . $subcat_slug . '</h4>';

                // Get all posts that belong to this specific subcategory
                $posts = new WP_Query(
                array(
                'post_type' => $post_type,
                'posts_per_page' => -1, // <-- Show all posts
                'hide_empty' => true,
                'order' => 'ASC',
                'tax_query' => array(
                array(
                'taxonomy' => $taxonomy,
                'terms' => $subcat_id,
                'field' => 'id'
                )
                )
                )
                );

                // If there are posts available within this subcategory
                if ( $posts->have_posts() ):
                ?>
                <ul>
                <?php
                while ( $posts->have_posts() ): $posts->the_post();
                //Show the title of each post with the Post ID
                ?>
                <li>Post: <?php the_title(); ?> - ID: <?php the_ID(); ?></li>
                <?php
                endwhile;
                ?>
                </ul>
                <?php
                else:
                echo 'No posts found';
                endif;

                wp_reset_query();
                }
                ?>
                </div>
                <?php
                }
                ow_subcategories_with_posts_by_category( 'name-of-your-taxonomy', 'name-of-your-post-type', 'name-of-your-specific-term' );


                'name-of-your-taxonomy' is the name of the main taxonomy. e.g.: 'victual_category'



                'name-of-your-post-type' is the name of your post type. e.g.: 'victual'



                'name-of-your-specific-term' is the name of the specific category that you want to use so that subcategories that belong to that category can be displayed. e.g.: 'food'



                So if I call the function:



                ow_subcategories_with_posts_by_category( 'victual_category', 'victual', 'food' );


                That would display all the subcategories with their respective Posts that belongs to Food of the Victual-Category taxonomy:



                Subcategory: Appetizers - ID: 35 - Slug: appetizers




                • Post: Chips & Salsa - ID: 464

                • Post: Queso - ID: 465


                Subcategory: Tacos - ID: 36 - Slug: tacos




                • Post: Al Pastor - ID: 466

                • Post: Fish Al Pastor - ID: 467







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Jan 3 at 4:45

























                answered Jan 3 at 4:24









                drjorgepolancodrjorgepolanco

                3,0232629




                3,0232629






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f39937889%2fdisplay-subcategories-of-custom-categories-wordpress%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    MongoDB - Not Authorized To Execute Command

                    in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

                    Npm cannot find a required file even through it is in the searched directory