0

I'm having trouble getting the meta keys of custom categories. I tried dumping all the meta keys and values of a post by using get_post_meta() as such:

$myvals = get_post_meta($post->ID);
var_dump($myvals);

However, the output doesn't include custom categories (not even the regular category). I made sure that my post does contain values in these categories so that they are not empty (in hopes that var_dump doesn't skip empty values). Some of the fields that are being printed are: custom fields keys, thumbnail key ("_thumbnail_id"), and some Yoast field keys that were included through a plugin.

1

It sounds like you're trying to get the category terms for a post. Category terms are not stored in post meta.

You can get a post's terms using get_the_terms(). In this example, $terms will hold an array of term objects for the category taxonomy associated with the current post. You can replace category with your custom taxonomy name.

$terms = get_the_terms( get_the_ID(), 'category' );
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
    echo '<ul>';
    foreach ( $terms as $term ) {
        echo '<li>' . $term->name . '</li>';
    }
    echo '</ul>';
}

Each term object will look like this as we iterate over the foreach loop in the above code:

WP_Term Object
(
    [term_id] => 2
    [name] => Alignment
    [slug] => alignment
    [term_group] => 0
    [term_taxonomy_id] => 2
    [taxonomy] => category
    [description] => Posts in this category test image and text alignment.
    [parent] => 0
    [count] => 4
    [filter] => raw
)
  • Thanks for your reply. Ah so categories aren't in post meta. I've already seen the term names for my categories, however I'll accept your post as the answer because it demystified why I wasn't able to see my categories as meta keys/values. – Paul Sim Oct 5 '16 at 0:58
  • 1
    Glad to help! Yes, taxonomies have their own database table and API. WordPress adds the category and tag taxonomies by default, and as you know, you can add custom taxonomies. Terms are the names of items within a taxonomy. Taxonomy terms have the ability to store metadata now too, which is different from post meta. – Dave Romsey Oct 5 '16 at 1:07

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.