WordPress > Get custom taxonomy from a custom post type

11,167

Solution 1

You can do this to get all the terms of a custom taxonomy:

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

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

foreach ( $terms as $term ) {
    $term_link = get_term_link( $term );
}

$term returns an array with the following structure:

array(
[0] => WP_Term Object
(
    [term_id] =>
    [name] =>
    [slug] =>
    [term_group] =>
    [term_taxonomy_id] =>
    [taxonomy] =>
    [description] =>
    [parent] =>
    [count] =>
    [filter] =>
)

$term_link will give you the permalink to your taxonomy term archive.

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


Regarding your other question of how to implement filter tabs: Check out this plugin: https://wordpress.org/plugins/beautiful-taxonomy-filters/

Solution 2

To find the taxonomies associated with a given post type, use the WordPress get_object_taxonomies() function like this:

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

$taxonomies will be an array of WP_Taxonomy objects. Omit the second argument to get an array of taxonomy slugs.

To query by a custom taxonomy, create a new WP_Query something like this:

$args = [
    'posts_per_page' => -1,
    'tax_query' => [
        [
            'taxonomy' => 'categories',
            'field' => 'slug',
            'terms' => ['your-term'],
        ],
    ],
];
$filterQuery = new WP_Query($args);

While taxonomy associations can be declared on post_types using register_post_type, the association is optional and very weak. Internally, WordPress goes the other way and assigns post_types to Taxonomies.

Each Taxonomy has an object_type property which is an array of slugs for the post_types it knows about. Digging into the register_post_type source code, we see it calls register_taxonomy_for_object_type for each item in the taxonomies argument property, then simply adds a slug to the Taxonomy's object_type array. This is the only time the post_type's taxonomy property is used. I prefer to declare post_types when registering the Taxonomy since it's closer to how WordPress works and misunderstanding that association caused me a lot of grief in the past.

Share:
11,167
Chris Tarasovs
Author by

Chris Tarasovs

I design, develop and market awesomeness.

Updated on June 04, 2022

Comments

  • Chris Tarasovs
    Chris Tarasovs almost 2 years
    1. Installed Custom Post Type UI wordpress plugin
    2. Created a custom post type = 'products'
    3. Registered a custom taxonomy categories (different from category) using

    1. How to display all custom post types and have a filter above with categories that works as filter tabs?

    2.How to loop in a custom template through the custom taxonomy 'categories' and display links?

    HTML structure

    Custom Post type URL : /wp-admin/edit-tags.php?taxonomy=categories&post_type=products

    PHP

    <?php get_term( $term, $taxonomy, $output, $filter ) ?>
    
    <?php 
    $args=array(
      'name' => 'categories'
    );
    $output = 'products'; // or names
    $taxonomies=get_taxonomies($args,$output); 
    if  ($taxonomies) {
      foreach ($taxonomies  as $taxonomy ) {
        echo '<p>' . $taxonomy->name . '</p>';
      }
    }  
    ?>
    
    <?php 
    $args = array(
      'public'   => true,
      '_builtin' => false
    
    ); 
    $output = 'names'; // or objects
    $operator = 'and'; // 'and' or 'or'
    $taxonomies = get_taxonomies( $args, $output, $operator ); 
    if ( $taxonomies ) {
      foreach ( $taxonomies  as $taxonomy ) {
        echo '<p>' . $taxonomy . '</p>';
      }
    }
    
  • Chris Tarasovs
    Chris Tarasovs almost 8 years
    I tried this but it still give me nothing also you haven't specified anywhere the custom post type.
  • Quack
    Quack almost 8 years
    You don't need the post type to get the terms of a taxonomy but if your WP Version is older than 4.5.0 you need to make a small change: $terms = get_terms( 'categories', array( 'hide_empty' => false, ) );
  • Fresz
    Fresz over 3 years
    Exactly what I was looking for