Function and usage of has_term_meta() in wordpress


Answers ( 1 )


    The has_term_meta() function in WordPress is used to retrieve all metadata, including the meta IDs, for a given term ID. Here's a detailed explanation:

    Function Signature

    has_term_meta( int $term_id ): array|false

    Function Description

    • Purpose: The primary purpose of has_term_meta() is to fetch all metadata associated with a specific term in the WordPress taxonomy system. This includes categories, tags, or any custom taxonomy terms.
    • How it Works: This function looks up the metadata in the database associated with a particular term ID. Metadata can include additional information that's been stored for terms, like custom fields added via plugins or custom code.


    • $term_id (int, required): This is the unique identifier of the term whose metadata you want to retrieve. The term ID corresponds to a specific term in a taxonomy (e.g., a specific category or tag).

    Return Value

    • Type: array|false
    • Description: The function returns an array of metadata if metadata exists for the given term. Each element in the array is an object containing the meta ID, meta key, and meta value. If there's no metadata for the term or if the metadata table is not installed, it returns false.

    Sample Usage

    $term_id = 123; // Replace 123 with the actual term ID
    $metadata = has_term_meta($term_id);
    if ($metadata) {
        foreach ($metadata as $meta) {
            // Access meta data
            $meta_id = $meta->meta_id;
            $meta_key = $meta->meta_key;
            $meta_value = $meta->meta_value;
            // Do something with the metadata
    } else {
        // No metadata found or meta table not installed

    In this example:

    1. Replace 123 with the term ID for which you want to fetch metadata.
    2. The if statement checks if there is any metadata associated with the term.
    3. If metadata exists, it loops through each meta entry, where you can access the meta ID, key, and value for each metadata entry.
    4. If there's no metadata, the else block can handle that case.


    • This function is particularly useful for developers working with custom taxonomies or needing to access additional information stored in term metadata.
    • It's important to ensure that the term ID provided is valid and corresponds to an existing term in the database.

Leave an answer