Search code examples
wordpresscategoriestaxonomycustom-taxonomy

Using WP_Term_Query to get Custom Taxonomies but show just the taxonomy from post


I am showing all taxonomies with this code:

    'orderby'   => 'ASC',
    'post_type' => 'integrations_cpt',
    'taxonomy' => 'categories_integration',
    'hide_empty'             => false,
    );
    $the_query = new WP_Query( $args );
    $categories = new WP_Term_Query( $args );
<?php foreach ( $categories->terms as $category): ?>
                <div class="row pl-3 p-1 sib-integrations-check fil-cat" data-rel="<?php echo $category->name?>">
                    <label class="sib-integrations-label"><?php echo $category->name?>
                        <input type="checkbox" class="chb"> <!-- hidden input -->
                        <span class="sib-integrations-checkmark" ></span>
                    </label>
                </div>

And is working fine outside the loop. But I want to show now, inside the loop and just the taxonomy from the post, not all of them... All I've tried is not working... Someone have any idea?

My CPT:

function create_integrations_custom_post_type()
{
    register_post_type('integrations_cpt',
        array(
            'labels'            => array(
                'name'          => __('Integrations'),
                'singular_name' => __('Integration')
            ),
            'public'        => true,
            'menu_icon'     => 'dashicons-share',
            'query_var'     => true,
            'has_archive'   => true,
            'taxonomies'          => array('categories_integration'),
            'supports'      => array('custom-fields','title'),
        )
    );

    register_taxonomy( 'categories_integration', 'integrations_cpt', array(
        'hierarchical'          => true, 
        'label'                 => 'Categories',
        'query_var'             => true,
        'exclude_from_search'   =>false,
        'singular_label'        => 'Category', 
        'has_archive'           => true,
        )
    );

    register_taxonomy_for_object_type( 'categories_integration', 'integrations_cpt' );
}
add_action('init', 'create_integrations_custom_post_type');

Solution

  • Use get_terms() function for finding the all categories.

    $terms = get_terms('categories_integration');
    foreach ($terms as $cat) {
        echo $cat->name;
    }