Using WordPress ‘get_terms_to_edit()’ PHP function

The get_terms_to_edit() WordPress PHP function retrieves a comma-separated list of terms available to edit for a given post ID.

Usage

get_terms_to_edit($post_id, $taxonomy);

Example:

Input:

echo get_terms_to_edit(42, 'category');

Output:

"category1, category2, category3"

Parameters

  • $post_id (int) – The ID of the post for which to retrieve terms.
  • $taxonomy (string) – The taxonomy for which to retrieve terms. Default is 'post_tag'.

More information

See WordPress Developer Resources: get_terms_to_edit()

Examples

Get the tags for a specific post

$post_id = 12;
$tags = get_terms_to_edit($post_id);
echo "Tags for post {$post_id}: {$tags}";

Get categories for a specific post

$post_id = 42;
$categories = get_terms_to_edit($post_id, 'category');
echo "Categories for post {$post_id}: {$categories}";

Display terms for a custom taxonomy

$post_id = 25;
$custom_taxonomy = 'colors';
$terms = get_terms_to_edit($post_id, $custom_taxonomy);
echo "Terms in '{$custom_taxonomy}' for post {$post_id}: {$terms}";

Add terms to an array for further processing

$post_id = 33;
$categories = get_terms_to_edit($post_id, 'category');
$category_array = explode(', ', $categories);
print_r($category_array);

Check if a specific term exists in the terms list

$post_id = 55;
$tags = get_terms_to_edit($post_id);
$specific_tag = 'nature';
if (strpos($tags, $specific_tag) !== false) {
    echo "The '{$specific_tag}' tag is assigned to post {$post_id}.";
} else {
    echo "The '{$specific_tag}' tag is not assigned to post {$post_id}.";
}