0

I can find everywhere infos on how to get the terms for a specific a post, but I only need the taxonomies names. I mean:

tax: Taxonomy1 terms: term1, term2, etc

tax: Taxonomy2 terms: term1, term2, term3, etc

My post is setted as Taxonomy1.term1, Taxonomy2.term3 OK.

What I want is to know that my post is on Taxonomy1 and Taxonomy2, NOT that is on term1 and term3.

How can I do it? (and by the way, I'm speaking of custom taxonomies and custom post types)

1
$t = get_post_type($post);
$taxonomies = get_object_taxonomies($t);

$taxonomies will contain the names of the different taxonomies of your post.

0

I've just found an undocumented function (in wp-includes/taxonomy.php). It's a shortcut that uses get_object_taxonomies(): get_post_taxonomies($post_id). It returns the taxonomy names.

Anyway, I've just coded another symple function:

function get_taxonomies_info($id){
    global $wpdb;
    return $wpdb->get_results($wpdb->prepare("
        select tax.taxonomy as taxonomy, group_concat(tr.name) as term_names, group_concat(tr.term_id) as term_ids
        from " . $wpdb->prefix . "term_taxonomy tax
        INNER JOIN " . $wpdb->prefix . "term_relationships rel ON tax.term_taxonomy_id =rel.term_taxonomy_id
        INNER JOIN " . $wpdb->prefix . "terms tr ON tr.term_id = tax.term_id
        WHERE rel.object_id = %d
        GROUP BY taxonomy
    ",$id));
}

It will return an array of objects like this:

array(2) {
  [0]=>
  object(stdClass)#115 (3) {
    ["taxonomy"]=>
    string(21) "taxonomy name"
    ["term_names"]=>
    string(16) "term name"
    ["term_ids"]=>
    string(1) "4"
  }
  [1]=>
  object(stdClass)#114 (3) {
    ["taxonomy"]=>
    string(17) "soluzioni_manuali"
    ["term_names"]=>
    string(51) "term name 1,term name 2"
    ["term_ids"]=>
    string(3) "2,3"
  }
}

So for every row you'll have 1 taxonomy, a comma separated list of terms of THAT taxonomy, a comma separated list od the terms ids.

Obviously, one can easily change the function, maybe adding some other options (for example a filter-out taxonomy array)

Hope it helps

0

Get all the taxonomy terms of the post, then look at the taxonomy property of them.

See code comments for more details:

global $post;

// get all the taxonomies registered for the post type    
$allcustomtaxes = get_taxonomies(
  array('public'=> true,'_builtin' => false, 'object_type'=> array($post->post_type) )
);

// get the terms assigned to post, in all custom taxonomies
$terms = wp_get_object_terms($post->ID, $allcustomtaxes);

// get the 'taxonomy' property of terms objects, removing duplicates
$belongs = empty($terms) ? array('none'), array_unique(wp_list_pluck($terms, 'taxonomy'));

// example usage:
echo 'Post ' . $post->title . ' belongs to ' . implode(', ') . $belongs;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.