get post types by taxonomies in wordpress

How to get all the post types(not the posts) grouped by taxonomies?
Is there any standard wordpress functions?

I want something like this

taxonomy_1 -> post_type_11, post_type_12, post_type_13,  ....
taxonomy_2 -> post_type_21, post_type_22, ....

Related posts

Leave a Reply

2 comments

  1. I have written a query to get the taxonomies and the attached post types as arrays

    $query = "
    SELECT taxonomy, GROUP_CONCAT( DISTINCT `post_type` SEPARATOR ',') AS `post_types`
    FROM wp_term_taxonomy 
    JOIN wp_term_relationships ON wp_term_relationships.`term_taxonomy_id` =  wp_term_taxonomy.`term_taxonomy_id`
    JOIN wp_posts ON wp_term_relationships.`object_id` = wp_posts.`ID` 
    /* you can add other conditions here like - AND post_status = 'published' */
    GROUP BY taxonomy
    ";
    

    then I call this query

    $global wpdb;
    $post_types_by_taxonomies = $wpdb->get_results( $query, OBJECT_K );
    

    now we can loop through the result array

    foreach($post_types_by_taxonomies as $taxonomy => $post_types_as_string){
        $post_types = explode(',', $post_types_as_string->post_types);
        echo '<hr />';
        echo $taxonomy;
        echo '<br />';
        echo '<br />';
        foreach($post_types as $post_type){
            echo $post_type;
            echo '<br />';
        }
    }
    
  2. Please check this one i hope this will work for you.
    
    <?php
    
    // get taxonomies terms links
    
    function custom_taxonomies_terms_links(){
    
      // get post by post id
    
      $post = get_post( $post->ID );
    
    
    
      // get post type by post
    
      $post_type = $post->post_type;
    
    
    
      // get post type taxonomies
    
      $taxonomies = get_object_taxonomies( $post_type, 'objects' );
    
    
    
      $out = array();
    
      foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){
    
    
    
        // get the terms related to post
    
        $terms = get_the_terms( $post->ID, $taxonomy_slug );
    
    
    
        if ( !empty( $terms ) ) {
    
          $out[] = "<h2>" . $taxonomy->label . "</h2>n<ul>";
    
          foreach ( $terms as $term ) {
    
            $out[] =
    
              '  <li><a href="'
    
            .    get_term_link( $term->slug, $taxonomy_slug ) .'">'
    
            .    $term->name
    
            . "</a></li>n";
    
          }
    
          $out[] = "</ul>n";
    
        }
    
      }
    
    
    
      return implode('', $out );
    
    }
    
    ?>