How to manipulate, create, update and delete WordPress Terms with PHP

Marc Wag­ner

Febru­ary 6, 2022

2 min read|

Terms can be crea­ted, queried and edi­ted via PHP. Word­Press pro­vi­des func­tions for this pur­po­se.

Query a WordPress term with PHP #

To retrie­ve a term you can use the func­tion get_term($term_id, $taxo­no­my). For this you only need the ID and the taxo­no­my.

$term_id = 10;
$taxonomy = "post_tag";

/**
 * @var \WP_Term $Term
 */
$Term = get_term($term_id, $taxonomy);

Query a WordPress term by slug, name or term ID #

Via get_term_by($field, $value, $taxo­no­my, $out­put = OBJECT, $fil­ter = ‘raw’) you can also query a term by its slug, name or ID.

/**
 * Query WordPress Term with the Slug
 * @var \WP_Term|array|false $Term
 */
$Term = get_term_by("slug", "i-am-a-slug", "post_tag");

/**
 * Query WordPress Term with the Name
 * @var \WP_Term|array|false $Term
 */
$Term = get_term_by("name", "i am a name", "post_tag");

/**
 * Query WordPress Term with the Term ID
 * @var \WP_Term|array|false $Term
 */
$Term = get_term_by("term_id", 10, "post_tag");

Query all WordPress terms from one taxonomy #

Using get_terms($args) you can query all terms of a taxo­no­my.

$args = array(
   'taxonomy' => 'post_tag',
   'hide_empty' => false
);

/**
 * @var array<\WP_Term> $Terms
 */
$Terms = get_terms($args);

Inserting a new WordPress term at a taxonomy #

New terms can be added via wp_insert_term($term, $taxo­no­my, $args = array()). If the term alre­a­dy exists, it will be updated.

/**
 * Arguments can be specified optionally
 */
$args = array(
   'description' => 'Description',
   'parent' => 0,
   'slug' => ''
);

/**
 * Create new term in the taxonomy post_tag
 */
$term_id = wp_insert_term('I am a new term', 'post_tag', $args);

Delete a term on WordPress via PHP #

Terms can also be remo­ved quick­ly and easi­ly via PHP. For this pur­po­se, the func­tion wp_delete_term($term_id,$taxonomy) exists.

/**
 * Deletes the term with term ID 10 in the taxonomy post_tag
 */
$result = wp_delete_term(10, 'post_tag');

Query meta data of a term #

Like posts, terms can also con­tain meta­da­ta. The­se can be retrie­ved via get_term_meta($term_id, $meta_key, $sin­gle).

/**
 * Returns a single value
 * @var string|int|mixed $my_meta_data
 */
$my_meta_data = get_term_meta(10,'my_meta_data', true);

/**
 * Returns an array
 * @var array $meine_meta_daten
 */
$my_meta_data = get_term_meta(10,'my_meta_data');

Deleting metadata of a term in WordPress #

Meta­da­ta of terms can be dele­ted in Word­Press via delete_term_meta($term_id, $meta_key, $meta_value = ”).

$result = delete_term_meta(10, 'my_meta_data');

Update/create metadata of a term via PHP #

To update alre­a­dy exis­ting meta­da­ta of a term the func­tion update_term_meta($term_id, $meta_key, $meta_value, $prev_value = ”) is used. If the data does not exist yet, it will be crea­ted.

$result = update_term_meta(10, 'my_meta_data', 'this will be outputed');
88e86fcb816eff22bc917094df2862d8dd5c0e978b333e6dd5f36f808990c261 96

Arti­kel von:

Marc Wag­ner

Hi Marc here. I’m the foun­der of Forge12 Inter­ac­ti­ve and have been pas­sio­na­te about buil­ding web­sites, online stores, appli­ca­ti­ons and SaaS solu­ti­ons for busi­nesses for over 20 years. Befo­re foun­ding the com­pa­ny, I alre­a­dy work­ed in publicly lis­ted com­pa­nies and acqui­red all kinds of know­ledge. Now I want to pass this know­ledge on to my cus­to­mers.

Hast du eine Fra­ge? Hin­ter­lass bit­te einen Kom­men­tar
  1. blank
    Aman Pote August 9, 2024 at 10:00 — Rep­ly

    The blog on Word­Press terms with PHP pro­vi­des a clear and con­cise expl­ana­ti­on of key con­cepts and func­tions used in Word­Press deve­lo­p­ment. The focus on how the­se terms rela­te to PHP helps bridge the gap bet­ween Word­Press and backend pro­gramming. The prac­ti­cal examp­les and defi­ni­ti­ons make it an excel­lent resour­ce for deve­lo­pers loo­king to deepen their under­stan­ding of Word­Press and PHP inte­gra­ti­on. Gre­at job on making com­plex topics more acces­si­ble and under­stan­da­ble!

  2. blank
    Aman Pote August 9, 2024 at 10:01 — Rep­ly

    The blog on Word­Press terms with PHP pro­vi­des a clear expl­ana­ti­on of key con­cepts and func­tions used in Word­Press deve­lo­p­ment. It effec­tively bridges the gap bet­ween PHP pro­gramming and Word­­Press-spe­ci­­fic ter­mi­no­lo­gy, making it easier for deve­lo­pers to under­stand and app­ly the­se terms in their pro­jects. The prac­ti­cal examp­les and defi­ni­ti­ons are valuable for anyo­ne loo­king to deepen their know­ledge of Word­Press deve­lo­p­ment. Gre­at job on demys­ti­fy­ing the­se important con­cepts!

  3. blank
    Aman Pote August 9, 2024 at 10:02 — Rep­ly

    The blog on Word­Press terms with PHP pro­vi­des a hel­pful over­view of key PHP con­cepts and how they rela­te to Word­Press deve­lo­p­ment. The expl­ana­ti­ons of terms and code examp­les are valuable for both novice and expe­ri­en­ced deve­lo­pers loo­king to deepen their under­stan­ding of Word­Press cus­to­miza­ti­on and func­tion­a­li­ty. This post ser­ves as a useful refe­rence for anyo­ne working with Word­Press and PHP. Gre­at job on brea­king down com­plex topics into acces­si­ble and prac­ti­cal infor­ma­ti­on!

  4. blank
    Aman Pote August 9, 2024 at 10:02 — Rep­ly

    The blog on Word­Press terms with PHP offers a clear and con­cise expl­ana­ti­on of PHP con­cepts in the con­text of Word­Press deve­lo­p­ment. The detail­ed break­down of terms and their prac­ti­cal appli­ca­ti­ons is excel­lent for both new and expe­ri­en­ced deve­lo­pers. It pro­vi­des valuable insights into how PHP is used to extend Word­Press func­tion­a­li­ties. This gui­de is a gre­at resour­ce for impro­ving your Word­Press coding skills and under­stan­ding the under­ly­ing PHP code. Thanks for the infor­ma­ti­ve and well-orga­­ni­­zed post!