wp_update_term() – Updates term based on arguments provided.

You appear to be a bot. Output may be restricted

Description

Updates term based on arguments provided.

The $args will indiscriminately override all values with the same field name. Care must be taken to not override important information need to update or update will fail (or perhaps create a new term, neither would be acceptable). Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not defined in $args already. 'alias_of' will create a term group, if it doesn't already exist, and update it for the `$term`. If the 'slug' argument in $args is missing, then the 'name' will be used. If you set 'slug' and it isn't unique, then a WP_Error is returned. If you don't pass any slug, then a unique one will be created.

Usage

$array|WP_Error = wp_update_term( $term_id, $taxonomy, $args );

Parameters

$term_id
( int ) required – The ID of the term.
$taxonomy
( string ) required – The taxonomy of the term.
$args
( array|string ) optional – { Optional. Array or string of arguments for updating a term.
$alias_of
( string ) optional – Slug of the term to make this term an alias of. Default empty string. Accepts a term slug.
$description
( string ) optional – The term description. Default empty string.
$parent
( int ) optional – The id of the parent term. Default 0.
$slug
( string ) optional – The term slug to use. Default empty string. }

Returns

array|WP_Error An array containing the term_id and `term_taxonomy_id`, WP_Error otherwise.

Source

File name: wordpress/wp-includes/taxonomy.php


Lines:

1 to 100 of 283
function wp_update_term( $term_id, $taxonomy, $args = array() ) {
  global $wpdb;

  if ( ! taxonomy_exists( $taxonomy ) ) {
    return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) );
  }

  $term_id = (int) $term_id;

  // First, get all of the original args.
  $term = get_term( $term_id, $taxonomy );

  if ( is_wp_error( $term ) ) {
    return $term;
  }

  if ( ! $term ) {
    return new WP_Error( 'invalid_term', __( 'Empty Term.' ) );
  }

  $term = (array) $term->data;

  // Escape data pulled from DB.
  $term = wp_slash( $term );

  // Merge old and new args with new args overwriting old ones.
  $args = array_merge( $term, $args );

  $defaults    = array(
    'alias_of'    => '',
    'description' => '',
    'parent'      => 0,
    'slug'        => '',
  );
  $args        = wp_parse_args( $args, $defaults );
  $args        = sanitize_term( $args, $taxonomy, 'db' );
  $parsed_args = $args;

  // expected_slashed ($name)
  $name        = wp_unslash( $args['name'] );
  $description = wp_unslash( $args['description'] );

  $parsed_args['name']        = $name;
  $parsed_args['description'] = $description;

  if ( '' === trim( $name ) ) {
    return new WP_Error( 'empty_term_name', __( 'A name is required for this term.' ) );
  }

  if ( (int) $parsed_args['parent'] > 0 && ! term_exists( (int) $parsed_args['parent'] ) ) {
    return new WP_Error( 'missing_parent', __( 'Parent term does not exist.' ) );
  }

  $empty_slug = false;
  if ( empty( $args['slug'] ) ) {
    $empty_slug = true;
    $slug       = sanitize_title( $name );
  } else {
    $slug = $args['slug'];
  }

  $parsed_args['slug'] = $slug;

  $term_group = isset( $parsed_args['term_group'] ) ? $parsed_args['term_group'] : 0;
  if ( $args['alias_of'] ) {
    $alias = get_term_by( 'slug', $args['alias_of'], $taxonomy );
    if ( ! empty( $alias->term_group ) ) {
      // The alias we want is already in a group, so let's use that one.
      $term_group = $alias->term_group;
    } elseif ( ! empty( $alias->term_id ) ) {
      /*
			 * The alias is not in a group, so we create a new one
			 * and add the alias to it.
			 */
      $term_group = $wpdb->get_var( "SELECT MAX(term_group) FROM $wpdb->terms" ) + 1;

      wp_update_term(
        $alias->term_id,
        $taxonomy,
        array(
          'term_group' => $term_group,
        )
      );
    }

    $parsed_args['term_group'] = $term_group;
  }

  
/**
 * Filters the term parent.
 *
 * Hook to this filter to see if it will cause a hierarchy loop.
 *
 * @since 3.1.0
 *
 * @param int    $parent      ID of the parent term.
 * @param int    $term_id     Term ID.
 * @param string $taxonomy    Taxonomy slug.
 * @param array  $parsed_args An array of potentially altered update arguments for the given term.
 * @param array  $args        An array of update arguments for the given term.

 View on GitHub View on Trac