1

For one of my projects I created a custom post type and taxonomy chained to it. I'm trying to access the values of said taxonomy in each of my custom posts, using this code I get a multidimensional array with said values:

<?php
            $specs = wp_get_post_terms($post->ID, 'specializzazioni', array("fields" => "all"));
            print_r($specs);
 ?>

output for example:

         Array
    (
        [0] => WP_Term Object
            (
                [term_id] => 6
                [name] => Chirurgia Toracica
                [slug] => chirurgia-toracica
                [term_group] => 0
                [term_taxonomy_id] => 6
                [taxonomy] => specializzazioni
                [description] => 
                [parent] => 0
                [count] => 1
                [filter] => raw
            )

    [1] => WP_Term Object
        (
            [term_id] => 7
            [name] => Oculistica Pediatrica
            [slug] => oculistica-pediatrica
            [term_group] => 0
            [term_taxonomy_id] => 7
            [taxonomy] => specializzazioni
            [description] => 
            [parent] => 0
            [count] => 2
            [filter] => raw
        )

)

I'm only trying to access the values of [name] and [slug] but I can't seem to find a way to do that. Using the loop below outputs every value but that's not what I'm looking for

 <?php 
foreach($specs as $row => $value){
    foreach($value as $row2 => $value2)
        echo $value2 . "<br/>";
}
?>

I tried using the name and slug keys on every variable but everytime I get a different type of error.

1 Answers1

2

Notice that the array contains objects. You should be able to access name and slug for each object like this:

foreach ($specs as $object) {
  echo $object->name . ' ' . $object->slug;
}
Cyclonecode
  • 29,115
  • 11
  • 72
  • 93
  • Used this, but for the slug value in the two WP Objects (two event categories chosen in acf multi select) only one gets chosen in the end. – rhand Oct 29 '21 at 05:32