'show_in_nav_menus' => current_theme_supports( 'post-formats' ),
) );
}
-add_action( 'init', 'create_initial_taxonomies', 0 ); // highest priority
/**
* Get a list of registered taxonomy objects.
* * If not set, the default is inherited from public.
* - show_tagcloud - Whether to list the taxonomy in the Tag Cloud Widget.
* * If not set, the default is inherited from show_ui.
+ * - show_in_quick_edit - Whether to show the taxonomy in the quick/bulk edit panel.
+ * * It not set, the default is inherited from show_ui.
* - show_admin_column - Whether to display a column for the taxonomy on its post type listing screens.
* * Defaults to false.
* - meta_box_cb - Provide a callback function for the meta box display.
* - _builtin - true if this taxonomy is a native or "built-in" taxonomy. THIS IS FOR INTERNAL USE ONLY!
*
* @since 2.3.0
- * @uses $wp_taxonomies Inserts new taxonomy object into the list
- * @uses $wp Adds query vars
+ * @since 4.2.0 Introduced `show_in_quick_edit` argument.
+ *
+ * @global array $wp_taxonomies Registered taxonomies.
+ * @global WP $wp WP instance.
*
* @param string $taxonomy Taxonomy key, must not exceed 32 characters.
* @param array|string $object_type Name of the object type for the taxonomy object.
'show_in_menu' => null,
'show_in_nav_menus' => null,
'show_tagcloud' => null,
+ 'show_in_quick_edit' => null,
'show_admin_column' => false,
'meta_box_cb' => null,
'capabilities' => array(),
);
$args = wp_parse_args( $args, $defaults );
- if ( strlen( $taxonomy ) > 32 ) {
- _doing_it_wrong( __FUNCTION__, __( 'Taxonomies cannot exceed 32 characters in length' ), '4.0' );
- return new WP_Error( 'taxonomy_too_long', __( 'Taxonomies cannot exceed 32 characters in length' ) );
+ if ( empty( $taxonomy ) || strlen( $taxonomy ) > 32 ) {
+ _doing_it_wrong( __FUNCTION__, __( 'Taxonomy names must be between 1 and 32 characters in length.' ), '4.2' );
+ return new WP_Error( 'taxonomy_length_invalid', __( 'Taxonomy names must be between 1 and 32 characters in length.' ) );
}
if ( false !== $args['query_var'] && ! empty( $wp ) ) {
if ( null === $args['show_tagcloud'] )
$args['show_tagcloud'] = $args['show_ui'];
+ // If not set, default to the setting for show_ui.
+ if ( null === $args['show_in_quick_edit'] ) {
+ $args['show_in_quick_edit'] = $args['show_ui'];
+ }
+
$default_caps = array(
'manage_terms' => 'manage_categories',
'edit_terms' => 'manage_categories',
* - separate_items_with_commas - This string isn't used on hierarchical taxonomies. Default is "Separate tags with commas", used in the meta box.
* - add_or_remove_items - This string isn't used on hierarchical taxonomies. Default is "Add or remove tags", used in the meta box when JavaScript is disabled.
* - choose_from_most_used - This string isn't used on hierarchical taxonomies. Default is "Choose from the most used tags", used in the meta box.
- * - not_found - This string isn't used on hierarchical taxonomies. Default is "No tags found", used in the meta box.
+ * - not_found - Default is "No tags found"/"No categories found", used in the meta box and taxonomy list table.
*
* Above, the first default value is for non-hierarchical taxonomies (like tags) and the second one is for hierarchical taxonomies (like categories).
*
'separate_items_with_commas' => array( __( 'Separate tags with commas' ), null ),
'add_or_remove_items' => array( __( 'Add or remove tags' ), null ),
'choose_from_most_used' => array( __( 'Choose from the most used tags' ), null ),
- 'not_found' => array( __( 'No tags found.' ), null ),
+ 'not_found' => array( __( 'No tags found.' ), __( 'No categories found.' ) ),
);
$nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
$cleaned_query['relation'] = $this->sanitize_relation( $query );
// First-order clause.
- } else if ( self::is_first_order_clause( $query ) ) {
+ } elseif ( self::is_first_order_clause( $query ) ) {
$cleaned_clause = array_merge( $defaults, $query );
$cleaned_clause['terms'] = (array) $cleaned_clause['terms'];
}
// Otherwise, it's a nested query, so we recurse.
- } else if ( is_array( $query ) ) {
+ } elseif ( is_array( $query ) ) {
$cleaned_subquery = $this->sanitize_query( $query );
if ( ! empty( $cleaned_subquery ) ) {
foreach ( $query as $key => &$clause ) {
if ( 'relation' === $key ) {
$relation = $query['relation'];
- } else if ( is_array( $clause ) ) {
+ } elseif ( is_array( $clause ) ) {
// This is a first-order clause.
if ( $this->is_first_order_clause( $clause ) ) {
$where_count = count( $clause_sql['where'] );
if ( ! $where_count ) {
$sql_chunks['where'][] = '';
- } else if ( 1 === $where_count ) {
+ } elseif ( 1 === $where_count ) {
$sql_chunks['where'][] = $clause_sql['where'][0];
} else {
$sql_chunks['where'][] = '( ' . implode( ' AND ', $clause_sql['where'] ) . ' )';
switch ( $query['field'] ) {
case 'slug':
case 'name':
- $terms = "'" . implode( "','", array_map( 'sanitize_title_for_query', $query['terms'] ) ) . "'";
+ foreach ( $query['terms'] as &$term ) {
+ /*
+ * 0 is the $term_id parameter. We don't have a term ID yet, but it doesn't
+ * matter because `sanitize_term_field()` ignores the $term_id param when the
+ * context is 'db'.
+ */
+ $term = "'" . esc_sql( sanitize_term_field( $query['field'], $term, 0, $query['taxonomy'], 'db' ) ) . "'";
+ }
+
+ $terms = implode( ",", $query['terms'] );
+
$terms = $wpdb->get_col( "
SELECT $wpdb->term_taxonomy.$resulting_field
FROM $wpdb->term_taxonomy
$value = sanitize_title($value);
if ( empty($value) )
return false;
- } else if ( 'name' == $field ) {
+ } elseif ( 'name' == $field ) {
// Assume already escaped
$value = wp_unslash($value);
$field = 't.name';
- } else if ( 'term_taxonomy_id' == $field ) {
+ } elseif ( 'term_taxonomy_id' == $field ) {
$value = (int) $value;
$field = 'tt.term_taxonomy_id';
} else {
* The 'get_terms_orderby' filter passes the ORDER BY clause for the query
* along with the $args array.
*
- * The 'get_terms_fields' filter passes the fields for the SELECT query
- * along with the $args array.
- *
* @since 2.3.0
+ * @since 4.2.0 Introduced 'name' and 'childless' parameters.
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param array|string $args {
* Optional. Array or string of arguments to get terms.
*
- * @type string $orderby Field(s) to order terms by. Accepts term fields ('name', 'slug',
- * 'term_group', 'term_id', 'id'), 'count' for term taxonomy count,
- * 'include' to match the 'order' of the $include param, or 'none'
- * to skip ORDER BY. Defaults to 'name'.
- * @type string $order Whether to order terms in ascending or descending order.
+ * @type string $orderby Field(s) to order terms by. Accepts term fields ('name', 'slug',
+ * 'term_group', 'term_id', 'id', 'description'), 'count' for term
+ * taxonomy count, 'include' to match the 'order' of the $include param,
+ * or 'none' to skip ORDER BY. Defaults to 'name'.
+ * @type string $order Whether to order terms in ascending or descending order.
* Accepts 'ASC' (ascending) or 'DESC' (descending).
* Default 'ASC'.
* @type bool|int $hide_empty Whether to hide terms not assigned to any posts. Accepts
* @type array|string $exclude_tree Array or comma/space-separated string of term ids to exclude
* along with all of their descendant terms. If $include is
* non-empty, $exclude_tree is ignored. Default empty array.
- * @type int $number Maximum number of terms to return. Accepts 1+ or -1 (all).
- * Default -1.
+ * @type int|string $number Maximum number of terms to return. Accepts ''|0 (all) or any
+ * positive number. Default ''|0 (all).
* @type int $offset The number by which to offset the terms query. Default empty.
* @type string $fields Term fields to query for. Accepts 'all' (returns an array of
* term objects), 'ids' or 'names' (returns an array of integers
* or strings, respectively. Default 'all'.
+ * @type string|array $name Optional. Name or array of names to return term(s) for. Default empty.
* @type string|array $slug Optional. Slug or array of slugs to return term(s) for. Default empty.
* @type bool $hierarchical Whether to include terms that have non-empty descendants (even
* if $hide_empty is set to true). Default true.
* @type int $child_of Term ID to retrieve child terms of. If multiple taxonomies
* are passed, $child_of is ignored. Default 0.
* @type int|string $parent Parent term ID to retrieve direct-child terms of. Default empty.
+ * @type bool $childless True to limit results to terms that have no children. This parameter has
+ * no effect on non-hierarchical taxonomies. Default false.
* @type string $cache_domain Unique cache key to be produced when this query is stored in an
* object cache. Default is 'core'.
* }
$defaults = array('orderby' => 'name', 'order' => 'ASC',
'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(),
- 'number' => '', 'fields' => 'all', 'slug' => '', 'parent' => '',
+ 'number' => '', 'fields' => 'all', 'name' => '', 'slug' => '', 'parent' => '', 'childless' => false,
'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', 'description__like' => '',
'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core' );
$args = wp_parse_args( $args, $defaults );
$args['offset'] = absint( $args['offset'] );
// Save queries by not crawling the tree in the case of multiple taxes or a flat tax.
- if ( ! $single_taxonomy || ! is_taxonomy_hierarchical( reset( $taxonomies ) ) ) {
+ $has_hierarchical_tax = false;
+ foreach ( $taxonomies as $_tax ) {
+ if ( is_taxonomy_hierarchical( $_tax ) ) {
+ $has_hierarchical_tax = true;
+ }
+ }
+
+ if ( ! $has_hierarchical_tax ) {
$args['hierarchical'] = false;
$args['pad_counts'] = false;
}
}
if ( 'all' == $args['get'] ) {
+ $args['childless'] = false;
$args['child_of'] = 0;
$args['hide_empty'] = 0;
$args['hierarchical'] = false;
*
* @since 3.1.0
*
- * @param array $args An array of arguments.
- * @param string|array $taxonomies A taxonomy or array of taxonomies.
+ * @param array $args An array of arguments.
+ * @param array $taxonomies An array of taxonomies.
*/
$args = apply_filters( 'get_terms_args', $args, $taxonomies );
+ // Avoid the query if the queried parent/child_of term has no descendants.
$child_of = $args['child_of'];
+ $parent = $args['parent'];
+
if ( $child_of ) {
- $hierarchy = _get_term_hierarchy( reset( $taxonomies ) );
- if ( ! isset( $hierarchy[ $child_of ] ) ) {
- return $empty_array;
- }
+ $_parent = $child_of;
+ } elseif ( $parent ) {
+ $_parent = $parent;
+ } else {
+ $_parent = false;
}
- $parent = $args['parent'];
- if ( $parent ) {
- $hierarchy = _get_term_hierarchy( reset( $taxonomies ) );
- if ( ! isset( $hierarchy[ $parent ] ) ) {
+ if ( $_parent ) {
+ $in_hierarchy = false;
+ foreach ( $taxonomies as $_tax ) {
+ $hierarchy = _get_term_hierarchy( $_tax );
+
+ if ( isset( $hierarchy[ $_parent ] ) ) {
+ $in_hierarchy = true;
+ }
+ }
+
+ if ( ! $in_hierarchy ) {
return $empty_array;
}
}
*
* @since 2.3.0
*
- * @param array $cache Cached array of terms for the given taxonomy.
- * @param string|array $taxonomies A taxonomy or array of taxonomies.
- * @param array $args An array of arguments to get terms.
+ * @param array $cache Cached array of terms for the given taxonomy.
+ * @param array $taxonomies An array of taxonomies.
+ * @param array $args An array of arguments to get terms.
*/
$cache = apply_filters( 'get_terms', $cache, $taxonomies, $args );
return $cache;
$_orderby = strtolower( $args['orderby'] );
if ( 'count' == $_orderby ) {
$orderby = 'tt.count';
- } else if ( 'name' == $_orderby ) {
+ } elseif ( 'name' == $_orderby ) {
$orderby = 't.name';
- } else if ( 'slug' == $_orderby ) {
+ } elseif ( 'slug' == $_orderby ) {
$orderby = 't.slug';
- } else if ( 'include' == $_orderby && ! empty( $args['include'] ) ) {
+ } elseif ( 'include' == $_orderby && ! empty( $args['include'] ) ) {
$include = implode( ',', array_map( 'absint', $args['include'] ) );
$orderby = "FIELD( t.term_id, $include )";
- } else if ( 'term_group' == $_orderby ) {
+ } elseif ( 'term_group' == $_orderby ) {
$orderby = 't.term_group';
- } else if ( 'none' == $_orderby ) {
+ } elseif ( 'description' == $_orderby ) {
+ $orderby = 'tt.description';
+ } elseif ( 'none' == $_orderby ) {
$orderby = '';
} elseif ( empty($_orderby) || 'id' == $_orderby ) {
$orderby = 't.term_id';
*
* @since 2.8.0
*
- * @param string $orderby ORDERBY clause of the terms query.
- * @param array $args An array of terms query arguments.
- * @param string|array $taxonomies A taxonomy or array of taxonomies.
+ * @param string $orderby ORDERBY clause of the terms query.
+ * @param array $args An array of terms query arguments.
+ * @param array $taxonomies An array of taxonomies.
*/
$orderby = apply_filters( 'get_terms_orderby', $orderby, $args, $taxonomies );
$where .= $inclusions;
}
+ $exclusions = array();
if ( ! empty( $exclude_tree ) ) {
$exclude_tree = wp_parse_id_list( $exclude_tree );
$excluded_children = $exclude_tree;
(array) get_terms( $taxonomies[0], array( 'child_of' => intval( $extrunk ), 'fields' => 'ids', 'hide_empty' => 0 ) )
);
}
- $exclusions = implode( ',', array_map( 'intval', $excluded_children ) );
- } else {
- $exclusions = '';
+ $exclusions = array_merge( $excluded_children, $exclusions );
}
if ( ! empty( $exclude ) ) {
- $exterms = wp_parse_id_list( $exclude );
- if ( empty( $exclusions ) ) {
- $exclusions = implode( ',', $exterms );
- } else {
- $exclusions .= ', ' . implode( ',', $exterms );
+ $exclusions = array_merge( wp_parse_id_list( $exclude ), $exclusions );
+ }
+
+ // 'childless' terms are those without an entry in the flattened term hierarchy.
+ $childless = (bool) $args['childless'];
+ if ( $childless ) {
+ foreach ( $taxonomies as $_tax ) {
+ $term_hierarchy = _get_term_hierarchy( $_tax );
+ $exclusions = array_merge( array_keys( $term_hierarchy ), $exclusions );
}
}
if ( ! empty( $exclusions ) ) {
- $exclusions = ' AND t.term_id NOT IN (' . $exclusions . ')';
+ $exclusions = ' AND t.term_id NOT IN (' . implode( ',', array_map( 'intval', $exclusions ) ) . ')';
+ } else {
+ $exclusions = '';
}
/**
*
* @since 2.3.0
*
- * @param string $exclusions NOT IN clause of the terms query.
- * @param array $args An array of terms query arguments.
- * @param string|array $taxonomies A taxonomy or array of taxonomies.
+ * @param string $exclusions NOT IN clause of the terms query.
+ * @param array $args An array of terms query arguments.
+ * @param array $taxonomies An array of taxonomies.
*/
$exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args, $taxonomies );
$where .= $exclusions;
}
+ if ( ! empty( $args['name'] ) ) {
+ if ( is_array( $args['name'] ) ) {
+ $name = array_map( 'sanitize_text_field', $args['name'] );
+ $where .= " AND t.name IN ('" . implode( "', '", array_map( 'esc_sql', $name ) ) . "')";
+ } else {
+ $name = sanitize_text_field( $args['name'] );
+ $where .= $wpdb->prepare( " AND t.name = %s", $name );
+ }
+ }
+
if ( ! empty( $args['slug'] ) ) {
if ( is_array( $args['slug'] ) ) {
$slug = array_map( 'sanitize_title', $args['slug'] );
break;
case 'ids':
case 'id=>parent':
- $selects = array( 't.term_id', 'tt.parent', 'tt.count' );
+ $selects = array( 't.term_id', 'tt.parent', 'tt.count', 'tt.taxonomy' );
break;
case 'names':
- $selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name' );
+ $selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name', 'tt.taxonomy' );
break;
case 'count':
$orderby = '';
$selects = array( 'COUNT(*)' );
break;
case 'id=>name':
- $selects = array( 't.term_id', 't.name', 'tt.count' );
+ $selects = array( 't.term_id', 't.name', 'tt.count', 'tt.taxonomy' );
break;
case 'id=>slug':
- $selects = array( 't.term_id', 't.slug', 'tt.count' );
+ $selects = array( 't.term_id', 't.slug', 'tt.count', 'tt.taxonomy' );
break;
}
/**
* Filter the fields to select in the terms query.
*
+ * Field lists modified using this filter will only modify the term fields returned
+ * by the function when the `$fields` parameter set to 'count' or 'all'. In all other
+ * cases, the term fields in the results array will be determined by the `$fields`
+ * parameter alone.
+ *
+ * Use of this filter can result in unpredictable behavior, and is not recommended.
+ *
* @since 2.8.0
*
- * @param array $selects An array of fields to select for the terms query.
- * @param array $args An array of term query arguments.
- * @param string|array $taxonomies A taxonomy or array of taxonomies.
+ * @param array $selects An array of fields to select for the terms query.
+ * @param array $args An array of term query arguments.
+ * @param array $taxonomies An array of taxonomies.
*/
$fields = implode( ', ', apply_filters( 'get_terms_fields', $selects, $args, $taxonomies ) );
*
* @since 3.1.0
*
- * @param array $pieces Terms query SQL clauses.
- * @param string|array $taxonomies A taxonomy or array of taxonomies.
- * @param array $args An array of terms query arguments.
+ * @param array $pieces Terms query SQL clauses.
+ * @param array $taxonomies An array of taxonomies.
+ * @param array $args An array of terms query arguments.
*/
$clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args );
$fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';
}
if ( $child_of ) {
- $children = _get_term_hierarchy( reset( $taxonomies ) );
- if ( ! empty( $children ) ) {
- $terms = _get_term_children( $child_of, $terms, reset( $taxonomies ) );
+ foreach ( $taxonomies as $_tax ) {
+ $children = _get_term_hierarchy( $_tax );
+ if ( ! empty( $children ) ) {
+ $terms = _get_term_children( $child_of, $terms, $_tax );
+ }
}
}
// Update term counts to include children.
if ( $args['pad_counts'] && 'all' == $_fields ) {
- _pad_term_counts( $terms, reset( $taxonomies ) );
+ foreach ( $taxonomies as $_tax ) {
+ _pad_term_counts( $terms, $_tax );
+ }
}
+
// Make sure we show empty categories that have children.
if ( $hierarchical && $args['hide_empty'] && is_array( $terms ) ) {
foreach ( $terms as $k => $term ) {
if ( ! $term->count ) {
- $children = get_term_children( $term->term_id, reset( $taxonomies ) );
+ $children = get_term_children( $term->term_id, $term->taxonomy );
if ( is_array( $children ) ) {
foreach ( $children as $child_id ) {
- $child = get_term( $child_id, reset( $taxonomies ) );
+ $child = get_term( $child_id, $term->taxonomy );
if ( $child->count ) {
continue 2;
}
}
}
}
- reset( $terms );
$_terms = array();
if ( 'id=>parent' == $_fields ) {
- while ( $term = array_shift( $terms ) ) {
- $_terms[$term->term_id] = $term->parent;
+ foreach ( $terms as $term ) {
+ $_terms[ $term->term_id ] = $term->parent;
}
} elseif ( 'ids' == $_fields ) {
- while ( $term = array_shift( $terms ) ) {
+ foreach ( $terms as $term ) {
$_terms[] = $term->term_id;
}
} elseif ( 'names' == $_fields ) {
- while ( $term = array_shift( $terms ) ) {
+ foreach ( $terms as $term ) {
$_terms[] = $term->name;
}
} elseif ( 'id=>name' == $_fields ) {
- while ( $term = array_shift( $terms ) ) {
- $_terms[$term->term_id] = $term->name;
+ foreach ( $terms as $term ) {
+ $_terms[ $term->term_id ] = $term->name;
}
} elseif ( 'id=>slug' == $_fields ) {
- while ( $term = array_shift( $terms ) ) {
- $_terms[$term->term_id] = $term->slug;
+ foreach ( $terms as $term ) {
+ $_terms[ $term->term_id ] = $term->slug;
}
}
$value = esc_html($value); // textarea_escaped
else
$value = esc_attr($value);
- } else if ( 'db' == $context ) {
+ } elseif ( 'db' == $context ) {
/**
* Filter a term field value before it is sanitized.
$value = apply_filters( 'pre_category_nicename', $value );
}
- } else if ( 'rss' == $context ) {
+ } elseif ( 'rss' == $context ) {
/**
* Filter the term field for use in RSS.
$value = apply_filters( "{$taxonomy}_{$field}", $value, $term_id, $context );
}
- if ( 'attribute' == $context )
+ if ( 'attribute' == $context ) {
$value = esc_attr($value);
- else if ( 'js' == $context )
+ } elseif ( 'js' == $context ) {
$value = esc_js($value);
-
+ }
return $value;
}
/**
* Retrieves the terms associated with the given object(s), in the supplied taxonomies.
*
- * The following information has to do the $args parameter and for what can be
- * contained in the string or array of that parameter, if it exists.
- *
- * The first argument is called, 'orderby' and has the default value of 'name'.
- * The other value that is supported is 'count'.
- *
- * The second argument is called, 'order' and has the default value of 'ASC'.
- * The only other value that will be acceptable is 'DESC'.
- *
- * The final argument supported is called, 'fields' and has the default value of
- * 'all'. There are multiple other options that can be used instead. Supported
- * values are as follows: 'all', 'ids', 'names', and finally
- * 'all_with_object_id'.
- *
- * The fields argument also decides what will be returned. If 'all' or
- * 'all_with_object_id' is chosen or the default kept intact, then all matching
- * terms objects will be returned. If either 'ids' or 'names' is used, then an
- * array of all matching term ids or term names will be returned respectively.
- *
* @since 2.3.0
+ * @since 4.2.0 Added support for 'taxonomy', 'parent', and 'term_taxonomy_id' values of `$orderby`.
+ * Introduced `$parent` argument.
*
* @global wpdb $wpdb WordPress database abstraction object.
*
- * @param int|array $object_ids The ID(s) of the object(s) to retrieve.
+ * @param int|array $object_ids The ID(s) of the object(s) to retrieve.
* @param string|array $taxonomies The taxonomies to retrieve terms from.
- * @param array|string $args Change what is returned
- * @return array|WP_Error The requested term data or empty array if no terms found. WP_Error if any of the $taxonomies don't exist.
+ * @param array|string $args {
+ * Array of arguments.
+ * @type string $orderby Field by which results should be sorted. Accepts 'name', 'count', 'slug', 'term_group',
+ * 'term_order', 'taxonomy', 'parent', or 'term_taxonomy_id'. Default 'name'.
+ * @type string $order Sort order. Accepts 'ASC' or 'DESC'. Default 'ASC'.
+ * @type string $fields Fields to return for matched terms. Accepts 'all', 'ids', 'names', and
+ * 'all_with_object_id'. Note that 'all' or 'all_with_object_id' will result in an array of
+ * term objects being returned, 'ids' will return an array of integers, and 'names' an array
+ * of strings.
+ * @type int $parent Optional. Limit results to the direct children of a given term ID.
+ * }
+ * @return array|WP_Error The requested term data or empty array if no terms found.
+ * WP_Error if any of the $taxonomies don't exist.
*/
function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {
global $wpdb;
$object_ids = array($object_ids);
$object_ids = array_map('intval', $object_ids);
- $defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
+ $defaults = array(
+ 'orderby' => 'name',
+ 'order' => 'ASC',
+ 'fields' => 'all',
+ 'parent' => '',
+ );
$args = wp_parse_args( $args, $defaults );
$terms = array();
$order = $args['order'];
$fields = $args['fields'];
- if ( 'count' == $orderby )
- $orderby = 'tt.count';
- else if ( 'name' == $orderby )
- $orderby = 't.name';
- else if ( 'slug' == $orderby )
- $orderby = 't.slug';
- else if ( 'term_group' == $orderby )
- $orderby = 't.term_group';
- else if ( 'term_order' == $orderby )
+ if ( in_array( $orderby, array( 'term_id', 'name', 'slug', 'term_group' ) ) ) {
+ $orderby = "t.$orderby";
+ } else if ( in_array( $orderby, array( 'count', 'parent', 'taxonomy', 'term_taxonomy_id' ) ) ) {
+ $orderby = "tt.$orderby";
+ } else if ( 'term_order' === $orderby ) {
$orderby = 'tr.term_order';
- else if ( 'none' == $orderby ) {
+ } else if ( 'none' === $orderby ) {
$orderby = '';
$order = '';
} else {
if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) )
$order = 'ASC';
+ $taxonomy_array = $taxonomies;
+ $object_id_array = $object_ids;
$taxonomies = "'" . implode("', '", $taxonomies) . "'";
$object_ids = implode(', ', $object_ids);
$select_this = '';
- if ( 'all' == $fields )
+ if ( 'all' == $fields ) {
$select_this = 't.*, tt.*';
- else if ( 'ids' == $fields )
+ } elseif ( 'ids' == $fields ) {
$select_this = 't.term_id';
- else if ( 'names' == $fields )
+ } elseif ( 'names' == $fields ) {
$select_this = 't.name';
- else if ( 'slugs' == $fields )
+ } elseif ( 'slugs' == $fields ) {
$select_this = 't.slug';
- else if ( 'all_with_object_id' == $fields )
+ } elseif ( 'all_with_object_id' == $fields ) {
$select_this = 't.*, tt.*, tr.object_id';
+ }
+
+ $where = array(
+ "tt.taxonomy IN ($taxonomies)",
+ "tr.object_id IN ($object_ids)",
+ );
+
+ if ( '' !== $args['parent'] ) {
+ $where[] = $wpdb->prepare( 'tt.parent = %d', $args['parent'] );
+ }
- $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) $orderby $order";
+ $where = implode( ' AND ', $where );
+
+ $query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE $where $orderby $order";
$objects = false;
if ( 'all' == $fields || 'all_with_object_id' == $fields ) {
$terms = array_merge( $terms, $_terms );
update_term_cache( $terms );
$objects = true;
- } else if ( 'ids' == $fields || 'names' == $fields || 'slugs' == $fields ) {
+ } elseif ( 'ids' == $fields || 'names' == $fields || 'slugs' == $fields ) {
$_terms = $wpdb->get_col( $query );
$_field = ( 'ids' == $fields ) ? 'term_id' : 'name';
foreach ( $_terms as $key => $term ) {
$_terms[$key] = sanitize_term_field( $_field, $term, $term, $taxonomy, 'raw' );
}
$terms = array_merge( $terms, $_terms );
- } else if ( 'tt_ids' == $fields ) {
+ } elseif ( 'tt_ids' == $fields ) {
$terms = $wpdb->get_col("SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies) $orderby $order");
foreach ( $terms as $key => $tt_id ) {
$terms[$key] = sanitize_term_field( 'term_taxonomy_id', $tt_id, 0, $taxonomy, 'raw' ); // 0 should be the term id, however is not needed when using raw context.
} elseif ( ! $objects ) {
$terms = array_values( array_unique( $terms ) );
}
+
/**
* Filter the terms for a given object or objects.
*
+ * @since 4.2.0
+ *
+ * @param array $terms An array of terms for the given object or objects.
+ * @param array $object_id_array Array of object IDs for which `$terms` were retrieved.
+ * @param array $taxonomy_array Array of taxonomies from which `$terms` were retrieved.
+ * @param array $args An array of arguments for retrieving terms for the given
+ * object(s). See wp_get_object_terms() for details.
+ */
+ $terms = apply_filters( 'get_object_terms', $terms, $object_id_array, $taxonomy_array, $args );
+
+ /**
+ * Filter the terms for a given object or objects.
+ *
+ * The `$taxonomies` parameter passed to this filter is formatted as a SQL fragment. The
+ * {@see 'get_object_terms'} filter is recommended as an alternative.
+ *
* @since 2.8.0
*
- * @param array $terms An array of terms for the given object or objects.
- * @param array|int $object_ids Object ID or array of IDs.
- * @param array|string $taxonomies A taxonomy or array of taxonomies.
- * @param array $args An array of arguments for retrieving terms for
- * the given object(s).
+ * @param array $terms An array of terms for the given object or objects.
+ * @param int|array $object_ids Object ID or array of IDs.
+ * @param string $taxonomies SQL-formatted (comma-separated and quoted) list of taxonomy names.
+ * @param array $args An array of arguments for retrieving terms for the given object(s).
+ * See {@see wp_get_object_terms()} for details.
*/
return apply_filters( 'wp_get_object_terms', $terms, $object_ids, $taxonomies, $args );
}
$slug_provided = ! empty( $args['slug'] );
if ( ! $slug_provided ) {
- $_name = trim( $name );
- $existing_term = get_term_by( 'name', $_name, $taxonomy );
- if ( $existing_term ) {
- $slug = $existing_term->slug;
- } else {
- $slug = sanitize_title( $name );
- }
+ $slug = sanitize_title( $name );
} else {
$slug = $args['slug'];
}
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;
- } else if ( ! empty( $alias->term_id ) ) {
+ } elseif ( ! empty( $alias->term_id ) ) {
/*
* The alias is not in a group, so we create a new one
* and add the alias to it.
}
}
- // Terms with duplicate names are not allowed at the same level of a taxonomy hierarchy.
- if ( $exists = term_exists( $slug, $taxonomy ) ) {
- $existing_term = get_term( $exists['term_id'], $taxonomy );
-
- if ( $name === $existing_term->name ) {
-
+ /*
+ * Prevent the creation of terms with duplicate names at the same level of a taxonomy hierarchy,
+ * unless a unique slug has been explicitly provided.
+ */
+ if ( $name_match = get_term_by( 'name', $name, $taxonomy ) ) {
+ $slug_match = get_term_by( 'slug', $slug, $taxonomy );
+ if ( ! $slug_provided || $name_match->slug === $slug || $slug_match ) {
if ( is_taxonomy_hierarchical( $taxonomy ) ) {
- $siblings = get_terms( $taxonomy, array( 'fields' => 'names', 'get' => 'all', 'parent' => $parent ) );
- if ( in_array( $name, $siblings ) ) {
- return new WP_Error( 'term_exists', __( 'A term with the name and slug already exists with this parent.' ), $exists['term_id'] );
+ $siblings = get_terms( $taxonomy, array( 'get' => 'all', 'parent' => $parent ) );
+
+ $existing_term = null;
+ if ( $name_match->slug === $slug && in_array( $name, wp_list_pluck( $siblings, 'name' ) ) ) {
+ $existing_term = $name_match;
+ } elseif ( $slug_match && in_array( $slug, wp_list_pluck( $siblings, 'slug' ) ) ) {
+ $existing_term = $slug_match;
}
+ if ( $existing_term ) {
+ return new WP_Error( 'term_exists', __( 'A term with the name already exists with this parent.' ), $existing_term->term_id );
+ }
} else {
- return new WP_Error( 'term_exists', __( 'A term with the name and slug already exists in this taxonomy.' ), $exists['term_id'] );
+ return new WP_Error( 'term_exists', __( 'A term with the name already exists in this taxonomy.' ), $name_match->term_id );
}
}
}
* @param int $object_id The object to relate to.
* @param array|int|string $terms A single term slug, single term id, or array of either term slugs or ids.
* Will replace all existing related terms in this taxonomy.
- * @param array|string $taxonomy The context in which to relate the term to the object.
+ * @param string $taxonomy The context in which to relate the term to the object.
* @param bool $append Optional. If false will delete difference of terms. Default false.
* @return array|WP_Error Affected Term IDs.
*/
function wp_update_term( $term_id, $taxonomy, $args = array() ) {
global $wpdb;
- if ( ! taxonomy_exists($taxonomy) )
- return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
+ 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, ARRAY_A);
+ $term = get_term( $term_id, $taxonomy, ARRAY_A );
- if ( is_wp_error( $term ) )
+ if ( is_wp_error( $term ) ) {
return $term;
+ }
+
+ if ( ! $term ) {
+ return new WP_Error( 'invalid_term', __( 'Empty Term' ) );
+ }
// Escape data pulled from DB.
$term = wp_slash($term);
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;
- } else if ( ! empty( $alias->term_id ) ) {
+ } elseif ( ! empty( $alias->term_id ) ) {
/*
* The alias is not in a group, so we create a new one
* and add the alias to it.
$tt_id = $wpdb->get_var( $wpdb->prepare( "SELECT tt.term_taxonomy_id FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.term_id = %d", $taxonomy, $term_id) );
+ // Check whether this is a shared term that needs splitting.
+ $_term_id = _split_shared_term( $term_id, $tt_id );
+ if ( ! is_wp_error( $_term_id ) ) {
+ $term_id = $_term_id;
+ }
+
/**
* Fires immediately before the given terms are edited.
*
*
* @since 2.3.0
*
- * @param int|array $id Term object ID
+ * @param int $id Term object ID
* @param string $taxonomy Taxonomy Name
* @return bool|array Empty array if $terms found, but not $taxonomy. False if nothing is in cache for $taxonomy and $id.
*/
}
/**
- * Updates the cache for Term ID(s).
- *
- * Will only update the cache for terms not already cached.
+ * Updates the cache for the given term object ID(s).
*
- * The $object_ids expects that the ids be separated by commas, if it is a
- * string.
+ * Note: Due to performance concerns, great care should be taken to only update
+ * term caches when necessary. Processing time can increase exponentially depending
+ * on both the number of passed term IDs and the number of taxonomies those terms
+ * belong to.
*
- * It should be noted that update_object_term_cache() is very time extensive. It
- * is advised that the function is not called very often or at least not for a
- * lot of terms that exist in a lot of taxonomies. The amount of time increases
- * for each term and it also increases for each taxonomy the term belongs to.
+ * Caches will only be updated for terms not already cached.
*
* @since 2.3.0
*
- * @param string|array $object_ids Single or list of term object ID(s)
- * @param array|string $object_type The taxonomy object type
- * @return null|false Null value is given with empty $object_ids. False if
+ * @param string|array $object_ids Comma-separated list or array of term object IDs..
+ * @param array|string $object_type The taxonomy object type.
+ * @return null|false Null if `$object_ids` is empty, false if all of the terms in
+ * `$object_ids` are already cached.
*/
function update_object_term_cache($object_ids, $object_type) {
if ( empty($object_ids) )
$object_terms = array();
foreach ( (array) $terms as $term )
- $object_terms[$term->object_id][$term->taxonomy][$term->term_id] = $term;
+ $object_terms[$term->object_id][$term->taxonomy][] = $term;
foreach ( $ids as $id ) {
foreach ( $taxonomies as $taxonomy ) {
* @since 2.3.0
*
* @param int $term_id The ancestor term: all returned terms should be descendants of $term_id.
- * @param array $terms The set of terms---either an array of term objects or term IDs---from which those that are descendants of $term_id will be chosen.
- * @param string $taxonomy The taxonomy which determines the hierarchy of the terms.
+ * @param array $terms The set of terms - either an array of term objects or term IDs - from which those that
+ * are descendants of $term_id will be chosen.
+ * @param string $taxonomy The taxonomy which determines the hierarchy of the terms.
+ * @param array $ancestors Term ancestors that have already been identified. Passed by reference, to keep track of
+ * found terms when recursing the hierarchy. The array of located ancestors is used to prevent
+ * infinite recursion loops. For performance, term_ids are used as array keys, with 1 as value.
* @return array The subset of $terms that are descendants of $term_id.
*/
-function _get_term_children($term_id, $terms, $taxonomy) {
+function _get_term_children( $term_id, $terms, $taxonomy, &$ancestors = array() ) {
$empty_array = array();
if ( empty($terms) )
return $empty_array;
if ( ( 0 != $term_id ) && ! isset($has_children[$term_id]) )
return $empty_array;
+ // Include the term itself in the ancestors array, so we can properly detect when a loop has occurred.
+ if ( empty( $ancestors ) ) {
+ $ancestors[ $term_id ] = 1;
+ }
+
foreach ( (array) $terms as $term ) {
$use_id = false;
if ( !is_object($term) ) {
$use_id = true;
}
- if ( $term->term_id == $term_id ) {
+ // Don't recurse if we've already identified the term as a child - this indicates a loop.
+ if ( isset( $ancestors[ $term->term_id ] ) ) {
continue;
}
if ( !isset($has_children[$term->term_id]) )
continue;
- if ( $children = _get_term_children($term->term_id, $terms, $taxonomy) )
+ $ancestors[ $term->term_id ] = 1;
+
+ if ( $children = _get_term_children( $term->term_id, $terms, $taxonomy, $ancestors) )
$term_list = array_merge($term_list, $children);
}
}
return;
$term_items = array();
+ $terms_by_id = array();
+ $term_ids = array();
foreach ( (array) $terms as $key => $term ) {
$terms_by_id[$term->term_id] = & $terms[$key];
// Touch every ancestor's lookup row for each post in each term
foreach ( $term_ids as $term_id ) {
$child = $term_id;
+ $ancestors = array();
while ( !empty( $terms_by_id[$child] ) && $parent = $terms_by_id[$child]->parent ) {
+ $ancestors[] = $child;
if ( !empty( $term_items[$term_id] ) )
foreach ( $term_items[$term_id] as $item_id => $touches ) {
$term_items[$parent][$item_id] = isset($term_items[$parent][$item_id]) ? ++$term_items[$parent][$item_id]: 1;
}
$child = $parent;
+
+ if ( in_array( $parent, $ancestors ) ) {
+ break;
+ }
}
}
$count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type IN ('" . implode("', '", $object_types ) . "') AND term_taxonomy_id = %d", $term ) );
/** This action is documented in wp-includes/taxonomy.php */
- do_action( 'edit_term_taxonomy', $term, $taxonomy );
+ do_action( 'edit_term_taxonomy', $term, $taxonomy->name );
$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
/** This action is documented in wp-includes/taxonomy.php */
- do_action( 'edited_term_taxonomy', $term, $taxonomy );
+ do_action( 'edited_term_taxonomy', $term, $taxonomy->name );
}
}
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) );
/** This action is documented in wp-includes/taxonomy.php */
- do_action( 'edit_term_taxonomy', $term, $taxonomy );
+ do_action( 'edit_term_taxonomy', $term, $taxonomy->name );
$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
/** This action is documented in wp-includes/taxonomy.php */
- do_action( 'edited_term_taxonomy', $term, $taxonomy );
+ do_action( 'edited_term_taxonomy', $term, $taxonomy->name );
+ }
+}
+
+/**
+ * Create a new term for a term_taxonomy item that currently shares its term with another term_taxonomy.
+ *
+ * @ignore
+ * @since 4.2.0
+ *
+ * @param int $term_id ID of the shared term.
+ * @param int $term_taxonomy_id ID of the term_taxonomy item to receive a new term.
+ * @return int|WP_Error When the current term does not need to be split (or cannot be split on the current
+ * database schema), `$term_id` is returned. When the term is successfully split, the
+ * new term_id is returned. A WP_Error is returned for miscellaneous errors.
+ */
+function _split_shared_term( $term_id, $term_taxonomy_id ) {
+ global $wpdb;
+
+ // Don't try to split terms if database schema does not support shared slugs.
+ $current_db_version = get_option( 'db_version' );
+ if ( $current_db_version < 30133 ) {
+ return $term_id;
+ }
+
+ // If there are no shared term_taxonomy rows, there's nothing to do here.
+ $shared_tt_count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy tt WHERE tt.term_id = %d AND tt.term_taxonomy_id != %d", $term_id, $term_taxonomy_id ) );
+ if ( ! $shared_tt_count ) {
+ return $term_id;
+ }
+
+ // Pull up data about the currently shared slug, which we'll use to populate the new one.
+ $shared_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.* FROM $wpdb->terms t WHERE t.term_id = %d", $term_id ) );
+
+ $new_term_data = array(
+ 'name' => $shared_term->name,
+ 'slug' => $shared_term->slug,
+ 'term_group' => $shared_term->term_group,
+ );
+
+ if ( false === $wpdb->insert( $wpdb->terms, $new_term_data ) ) {
+ return new WP_Error( 'db_insert_error', __( 'Could not split shared term.' ), $wpdb->last_error );
+ }
+
+ $new_term_id = (int) $wpdb->insert_id;
+
+ // Update the existing term_taxonomy to point to the newly created term.
+ $wpdb->update( $wpdb->term_taxonomy,
+ array( 'term_id' => $new_term_id ),
+ array( 'term_taxonomy_id' => $term_taxonomy_id )
+ );
+
+ // Reassign child terms to the new parent.
+ $term_taxonomy = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $term_taxonomy_id ) );
+ $children_tt_ids = $wpdb->get_col( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s AND parent = %d", $term_taxonomy->taxonomy, $term_id ) );
+
+ if ( ! empty( $children_tt_ids ) ) {
+ foreach ( $children_tt_ids as $child_tt_id ) {
+ $wpdb->update( $wpdb->term_taxonomy,
+ array( 'parent' => $new_term_id ),
+ array( 'term_taxonomy_id' => $child_tt_id )
+ );
+ clean_term_cache( $term_id, $term_taxonomy->taxonomy );
+ }
+ } else {
+ // If the term has no children, we must force its taxonomy cache to be rebuilt separately.
+ clean_term_cache( $new_term_id, $term_taxonomy->taxonomy );
+ }
+
+ // Clean the cache for term taxonomies formerly shared with the current term.
+ $shared_term_taxonomies = $wpdb->get_row( $wpdb->prepare( "SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id ) );
+ if ( $shared_term_taxonomies ) {
+ foreach ( $shared_term_taxonomies as $shared_term_taxonomy ) {
+ clean_term_cache( $term_id, $shared_term_taxonomy );
+ }
+ }
+
+ // Keep a record of term_ids that have been split, keyed by old term_id. See {@see wp_get_split_term()}.
+ $split_term_data = get_option( '_split_terms', array() );
+ if ( ! isset( $split_term_data[ $term_id ] ) ) {
+ $split_term_data[ $term_id ] = array();
+ }
+
+ $split_term_data[ $term_id ][ $term_taxonomy->taxonomy ] = $new_term_id;
+
+ update_option( '_split_terms', $split_term_data );
+
+ /**
+ * Fires after a previously shared taxonomy term is split into two separate terms.
+ *
+ * @since 4.2.0
+ *
+ * @param int $term_id ID of the formerly shared term.
+ * @param int $new_term_id ID of the new term created for the $term_taxonomy_id.
+ * @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split.
+ * @param string $taxonomy Taxonomy for the split term.
+ */
+ do_action( 'split_shared_term', $term_id, $new_term_id, $term_taxonomy_id, $term_taxonomy->taxonomy );
+
+ return $new_term_id;
+}
+
+/**
+ * Check default categories when a term gets split to see if any of them need to be updated.
+ *
+ * @ignore
+ * @since 4.2.0
+ *
+ * @param int $term_id ID of the formerly shared term.
+ * @param int $new_term_id ID of the new term created for the $term_taxonomy_id.
+ * @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split.
+ * @param string $taxonomy Taxonomy for the split term.
+ */
+function _wp_check_split_default_terms( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
+ if ( 'category' != $taxonomy ) {
+ return;
}
+
+ foreach ( array( 'default_category', 'default_link_category', 'default_email_category' ) as $option ) {
+ if ( $term_id == get_option( $option, -1 ) ) {
+ update_option( $option, $new_term_id );
+ }
+ }
+}
+
+/**
+ * Check menu items when a term gets split to see if any of them need to be updated.
+ *
+ * @ignore
+ * @since 4.2.0
+ *
+ * @param int $term_id ID of the formerly shared term.
+ * @param int $new_term_id ID of the new term created for the $term_taxonomy_id.
+ * @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split.
+ * @param string $taxonomy Taxonomy for the split term.
+ */
+function _wp_check_split_terms_in_menus( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
+ global $wpdb;
+ $post_ids = $wpdb->get_col( $wpdb->prepare(
+ "SELECT m1.post_id
+ FROM {$wpdb->postmeta} AS m1
+ INNER JOIN {$wpdb->postmeta} AS m2 ON ( m2.post_id = m1.post_id )
+ INNER JOIN {$wpdb->postmeta} AS m3 ON ( m3.post_id = m1.post_id )
+ WHERE ( m1.meta_key = '_menu_item_type' AND m1.meta_value = 'taxonomy' )
+ AND ( m2.meta_key = '_menu_item_object' AND m2.meta_value = '%s' )
+ AND ( m3.meta_key = '_menu_item_object_id' AND m3.meta_value = %d )",
+ $taxonomy,
+ $term_id
+ ) );
+
+ if ( $post_ids ) {
+ foreach ( $post_ids as $post_id ) {
+ update_post_meta( $post_id, '_menu_item_object_id', $new_term_id, $term_id );
+ }
+ }
+}
+
+/**
+ * Get data about terms that previously shared a single term_id, but have since been split.
+ *
+ * @since 4.2.0
+ *
+ * @param int $old_term_id Term ID. This is the old, pre-split term ID.
+ * @return array Array of new term IDs, keyed by taxonomy.
+ */
+function wp_get_split_terms( $old_term_id ) {
+ $split_terms = get_option( '_split_terms', array() );
+
+ $terms = array();
+ if ( isset( $split_terms[ $old_term_id ] ) ) {
+ $terms = $split_terms[ $old_term_id ];
+ }
+
+ return $terms;
+}
+
+/**
+ * Get the new term ID corresponding to a previously split term.
+ *
+ * @since 4.2.0
+ *
+ * @param int $old_term_id Term ID. This is the old, pre-split term ID.
+ * @param string $taxonomy Taxonomy that the term belongs to.
+ * @return bool|int If a previously split term is found corresponding to the old term_id and taxonomy,
+ * the new term_id will be returned. If no previously split term is found matching
+ * the parameters, returns false.
+ */
+function wp_get_split_term( $old_term_id, $taxonomy ) {
+ $split_terms = wp_get_split_terms( $old_term_id );
+
+ $term_id = false;
+ if ( isset( $split_terms[ $taxonomy ] ) ) {
+ $term_id = (int) $split_terms[ $taxonomy ];
+ }
+
+ return $term_id;
}
/**
* @param int $object_id ID of the object (post ID, link ID, ...)
* @param string $taxonomy Single taxonomy name
* @param int|string|array $terms Optional. Term term_id, name, slug or array of said
- * @return bool|WP_Error. WP_Error on input error.
+ * @return bool|WP_Error WP_Error on input error.
*/
function is_object_in_term( $object_id, $taxonomy, $terms = null ) {
if ( !$object_id = (int) $object_id )
if ( ! $resource_type ) {
if ( is_taxonomy_hierarchical( $object_type ) ) {
$resource_type = 'taxonomy';
- } else if ( post_type_exists( $object_type ) ) {
+ } elseif ( post_type_exists( $object_type ) ) {
$resource_type = 'post_type';
}
}
* Filter a given object's ancestors.
*
* @since 3.1.0
- * @since 4.1.1 Introduced the `$resource_type` parameter.
+ * @since 4.1.0 Introduced the `$resource_type` parameter.
*
* @param array $ancestors An array of object ancestors.
* @param int $object_id Object ID.