List all taxonomies assigned from a custom post type











up vote
0
down vote

favorite












I am really desperate for help here, been trying do this since yesterday and still no luck, so here is what I want to achieve



Collection = custom post type





  • Material = custom taxonomy assigned to "collection"




    • Mat1 = term

    • Mat2 = term




  • Body = custom taxonomy assigned to "collection"




    • Body1 = term

    • Body2 = term




  • Color = custom taxonomy assigned to "collection"




    • Color1 = term

    • Color2 = term




The structure will be something like this



- Material
- Mat1
- Mat2
- Body
- Body1
- Body2
- Color
- Color1
- Color2


I got this code but it's not working at all



$post_type = 'collection';
$taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type, 'hide_empty' => true ) );

foreach( $taxonomies as $taxonomy ){
echo $taxonomy->name;

$terms = get_terms( $taxonomy );
foreach( $terms as $term ){
echo '<li>';
echo $term->name;
echo '</li>';
}
}









share|improve this question


























    up vote
    0
    down vote

    favorite












    I am really desperate for help here, been trying do this since yesterday and still no luck, so here is what I want to achieve



    Collection = custom post type





    • Material = custom taxonomy assigned to "collection"




      • Mat1 = term

      • Mat2 = term




    • Body = custom taxonomy assigned to "collection"




      • Body1 = term

      • Body2 = term




    • Color = custom taxonomy assigned to "collection"




      • Color1 = term

      • Color2 = term




    The structure will be something like this



    - Material
    - Mat1
    - Mat2
    - Body
    - Body1
    - Body2
    - Color
    - Color1
    - Color2


    I got this code but it's not working at all



    $post_type = 'collection';
    $taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type, 'hide_empty' => true ) );

    foreach( $taxonomies as $taxonomy ){
    echo $taxonomy->name;

    $terms = get_terms( $taxonomy );
    foreach( $terms as $term ){
    echo '<li>';
    echo $term->name;
    echo '</li>';
    }
    }









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am really desperate for help here, been trying do this since yesterday and still no luck, so here is what I want to achieve



      Collection = custom post type





      • Material = custom taxonomy assigned to "collection"




        • Mat1 = term

        • Mat2 = term




      • Body = custom taxonomy assigned to "collection"




        • Body1 = term

        • Body2 = term




      • Color = custom taxonomy assigned to "collection"




        • Color1 = term

        • Color2 = term




      The structure will be something like this



      - Material
      - Mat1
      - Mat2
      - Body
      - Body1
      - Body2
      - Color
      - Color1
      - Color2


      I got this code but it's not working at all



      $post_type = 'collection';
      $taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type, 'hide_empty' => true ) );

      foreach( $taxonomies as $taxonomy ){
      echo $taxonomy->name;

      $terms = get_terms( $taxonomy );
      foreach( $terms as $term ){
      echo '<li>';
      echo $term->name;
      echo '</li>';
      }
      }









      share|improve this question













      I am really desperate for help here, been trying do this since yesterday and still no luck, so here is what I want to achieve



      Collection = custom post type





      • Material = custom taxonomy assigned to "collection"




        • Mat1 = term

        • Mat2 = term




      • Body = custom taxonomy assigned to "collection"




        • Body1 = term

        • Body2 = term




      • Color = custom taxonomy assigned to "collection"




        • Color1 = term

        • Color2 = term




      The structure will be something like this



      - Material
      - Mat1
      - Mat2
      - Body
      - Body1
      - Body2
      - Color
      - Color1
      - Color2


      I got this code but it's not working at all



      $post_type = 'collection';
      $taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type, 'hide_empty' => true ) );

      foreach( $taxonomies as $taxonomy ){
      echo $taxonomy->name;

      $terms = get_terms( $taxonomy );
      foreach( $terms as $term ){
      echo '<li>';
      echo $term->name;
      echo '</li>';
      }
      }






      wordpress custom-post-type custom-taxonomy






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 at 1:34









      Francis Alvin Tan

      2841721




      2841721
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          If you look at the documentation for get_object_taxonomies() you'll notice a few things, namely that for your purposes you'll want to pass the name of a post type as the first argument, and get the objects with the second. It also appears you're conflating the arguments for both get_object_taxonomies() and get_terms().



          Also with the get_terms() function, if you're using WP 4.5 or higher, you'll want to pass the taxonomy in the $args array.



          $taxonomies = get_object_taxonomies( 'collection', 'objects' );

          foreach( $taxonomies as $taxonomy ){
          echo $taxonomy->name;

          $terms = get_terms(array(
          'taxonomy' => $taxonomy->name,
          'hide_empty' => false,
          ));

          foreach( $terms as $term ){
          echo "<li>{$term->name}</li>";
          }
          }





          share|improve this answer





















          • How to I change the taxonomy to its name and not slug, in your code its showing its slug and how do I add links to the terms? that is the only thing I need and all is working fine, thank you
            – Francis Alvin Tan
            Nov 22 at 1:52










          • I think I got it, just change it to echo $taxonomy->label; and added the get_term_link() inside the foreach
            – Francis Alvin Tan
            Nov 22 at 1:58










          • Apologies for the delayed response - but yes that's all you needed. For reference you can view an example taxonomy object here
            – Xhynk
            Nov 22 at 6:48











          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',
          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%2f53422729%2flist-all-taxonomies-assigned-from-a-custom-post-type%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          1
          down vote



          accepted










          If you look at the documentation for get_object_taxonomies() you'll notice a few things, namely that for your purposes you'll want to pass the name of a post type as the first argument, and get the objects with the second. It also appears you're conflating the arguments for both get_object_taxonomies() and get_terms().



          Also with the get_terms() function, if you're using WP 4.5 or higher, you'll want to pass the taxonomy in the $args array.



          $taxonomies = get_object_taxonomies( 'collection', 'objects' );

          foreach( $taxonomies as $taxonomy ){
          echo $taxonomy->name;

          $terms = get_terms(array(
          'taxonomy' => $taxonomy->name,
          'hide_empty' => false,
          ));

          foreach( $terms as $term ){
          echo "<li>{$term->name}</li>";
          }
          }





          share|improve this answer





















          • How to I change the taxonomy to its name and not slug, in your code its showing its slug and how do I add links to the terms? that is the only thing I need and all is working fine, thank you
            – Francis Alvin Tan
            Nov 22 at 1:52










          • I think I got it, just change it to echo $taxonomy->label; and added the get_term_link() inside the foreach
            – Francis Alvin Tan
            Nov 22 at 1:58










          • Apologies for the delayed response - but yes that's all you needed. For reference you can view an example taxonomy object here
            – Xhynk
            Nov 22 at 6:48















          up vote
          1
          down vote



          accepted










          If you look at the documentation for get_object_taxonomies() you'll notice a few things, namely that for your purposes you'll want to pass the name of a post type as the first argument, and get the objects with the second. It also appears you're conflating the arguments for both get_object_taxonomies() and get_terms().



          Also with the get_terms() function, if you're using WP 4.5 or higher, you'll want to pass the taxonomy in the $args array.



          $taxonomies = get_object_taxonomies( 'collection', 'objects' );

          foreach( $taxonomies as $taxonomy ){
          echo $taxonomy->name;

          $terms = get_terms(array(
          'taxonomy' => $taxonomy->name,
          'hide_empty' => false,
          ));

          foreach( $terms as $term ){
          echo "<li>{$term->name}</li>";
          }
          }





          share|improve this answer





















          • How to I change the taxonomy to its name and not slug, in your code its showing its slug and how do I add links to the terms? that is the only thing I need and all is working fine, thank you
            – Francis Alvin Tan
            Nov 22 at 1:52










          • I think I got it, just change it to echo $taxonomy->label; and added the get_term_link() inside the foreach
            – Francis Alvin Tan
            Nov 22 at 1:58










          • Apologies for the delayed response - but yes that's all you needed. For reference you can view an example taxonomy object here
            – Xhynk
            Nov 22 at 6:48













          up vote
          1
          down vote



          accepted







          up vote
          1
          down vote



          accepted






          If you look at the documentation for get_object_taxonomies() you'll notice a few things, namely that for your purposes you'll want to pass the name of a post type as the first argument, and get the objects with the second. It also appears you're conflating the arguments for both get_object_taxonomies() and get_terms().



          Also with the get_terms() function, if you're using WP 4.5 or higher, you'll want to pass the taxonomy in the $args array.



          $taxonomies = get_object_taxonomies( 'collection', 'objects' );

          foreach( $taxonomies as $taxonomy ){
          echo $taxonomy->name;

          $terms = get_terms(array(
          'taxonomy' => $taxonomy->name,
          'hide_empty' => false,
          ));

          foreach( $terms as $term ){
          echo "<li>{$term->name}</li>";
          }
          }





          share|improve this answer












          If you look at the documentation for get_object_taxonomies() you'll notice a few things, namely that for your purposes you'll want to pass the name of a post type as the first argument, and get the objects with the second. It also appears you're conflating the arguments for both get_object_taxonomies() and get_terms().



          Also with the get_terms() function, if you're using WP 4.5 or higher, you'll want to pass the taxonomy in the $args array.



          $taxonomies = get_object_taxonomies( 'collection', 'objects' );

          foreach( $taxonomies as $taxonomy ){
          echo $taxonomy->name;

          $terms = get_terms(array(
          'taxonomy' => $taxonomy->name,
          'hide_empty' => false,
          ));

          foreach( $terms as $term ){
          echo "<li>{$term->name}</li>";
          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 at 1:44









          Xhynk

          7,07862346




          7,07862346












          • How to I change the taxonomy to its name and not slug, in your code its showing its slug and how do I add links to the terms? that is the only thing I need and all is working fine, thank you
            – Francis Alvin Tan
            Nov 22 at 1:52










          • I think I got it, just change it to echo $taxonomy->label; and added the get_term_link() inside the foreach
            – Francis Alvin Tan
            Nov 22 at 1:58










          • Apologies for the delayed response - but yes that's all you needed. For reference you can view an example taxonomy object here
            – Xhynk
            Nov 22 at 6:48


















          • How to I change the taxonomy to its name and not slug, in your code its showing its slug and how do I add links to the terms? that is the only thing I need and all is working fine, thank you
            – Francis Alvin Tan
            Nov 22 at 1:52










          • I think I got it, just change it to echo $taxonomy->label; and added the get_term_link() inside the foreach
            – Francis Alvin Tan
            Nov 22 at 1:58










          • Apologies for the delayed response - but yes that's all you needed. For reference you can view an example taxonomy object here
            – Xhynk
            Nov 22 at 6:48
















          How to I change the taxonomy to its name and not slug, in your code its showing its slug and how do I add links to the terms? that is the only thing I need and all is working fine, thank you
          – Francis Alvin Tan
          Nov 22 at 1:52




          How to I change the taxonomy to its name and not slug, in your code its showing its slug and how do I add links to the terms? that is the only thing I need and all is working fine, thank you
          – Francis Alvin Tan
          Nov 22 at 1:52












          I think I got it, just change it to echo $taxonomy->label; and added the get_term_link() inside the foreach
          – Francis Alvin Tan
          Nov 22 at 1:58




          I think I got it, just change it to echo $taxonomy->label; and added the get_term_link() inside the foreach
          – Francis Alvin Tan
          Nov 22 at 1:58












          Apologies for the delayed response - but yes that's all you needed. For reference you can view an example taxonomy object here
          – Xhynk
          Nov 22 at 6:48




          Apologies for the delayed response - but yes that's all you needed. For reference you can view an example taxonomy object here
          – Xhynk
          Nov 22 at 6:48


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53422729%2flist-all-taxonomies-assigned-from-a-custom-post-type%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

          Berounka

          Different font size/position of beamer's navigation symbols template's content depending on regular/plain...

          Sphinx de Gizeh