12 // Taxonomy Registration
16 * Creates the initial taxonomies.
18 * This function fires twice: in wp-settings.php before plugins are loaded (for
19 * backwards compatibility reasons), and again on the {@see 'init'} action. We must
20 * avoid registering rewrite rules before the {@see 'init'} action.
24 * @global WP_Rewrite $wp_rewrite The WordPress rewrite class.
26 function create_initial_taxonomies() {
29 if ( ! did_action( 'init' ) ) {
30 $rewrite = array( 'category' => false, 'post_tag' => false, 'post_format' => false );
34 * Filter the post formats rewrite base.
38 * @param string $context Context of the rewrite base. Default 'type'.
40 $post_format_base = apply_filters( 'post_format_rewrite_base', 'type' );
43 'hierarchical' => true,
44 'slug' => get_option('category_base') ? get_option('category_base') : 'category',
45 'with_front' => ! get_option('category_base') || $wp_rewrite->using_index_permalinks(),
46 'ep_mask' => EP_CATEGORIES,
49 'hierarchical' => false,
50 'slug' => get_option('tag_base') ? get_option('tag_base') : 'tag',
51 'with_front' => ! get_option('tag_base') || $wp_rewrite->using_index_permalinks(),
54 'post_format' => $post_format_base ? array( 'slug' => $post_format_base ) : false,
58 register_taxonomy( 'category', 'post', array(
59 'hierarchical' => true,
60 'query_var' => 'category_name',
61 'rewrite' => $rewrite['category'],
64 'show_admin_column' => true,
68 register_taxonomy( 'post_tag', 'post', array(
69 'hierarchical' => false,
71 'rewrite' => $rewrite['post_tag'],
74 'show_admin_column' => true,
78 register_taxonomy( 'nav_menu', 'nav_menu_item', array(
80 'hierarchical' => false,
82 'name' => __( 'Navigation Menus' ),
83 'singular_name' => __( 'Navigation Menu' ),
89 'show_in_nav_menus' => false,
92 register_taxonomy( 'link_category', 'link', array(
93 'hierarchical' => false,
95 'name' => __( 'Link Categories' ),
96 'singular_name' => __( 'Link Category' ),
97 'search_items' => __( 'Search Link Categories' ),
98 'popular_items' => null,
99 'all_items' => __( 'All Link Categories' ),
100 'edit_item' => __( 'Edit Link Category' ),
101 'update_item' => __( 'Update Link Category' ),
102 'add_new_item' => __( 'Add New Link Category' ),
103 'new_item_name' => __( 'New Link Category Name' ),
104 'separate_items_with_commas' => null,
105 'add_or_remove_items' => null,
106 'choose_from_most_used' => null,
108 'capabilities' => array(
109 'manage_terms' => 'manage_links',
110 'edit_terms' => 'manage_links',
111 'delete_terms' => 'manage_links',
112 'assign_terms' => 'manage_links',
114 'query_var' => false,
121 register_taxonomy( 'post_format', 'post', array(
123 'hierarchical' => false,
125 'name' => _x( 'Format', 'post format' ),
126 'singular_name' => _x( 'Format', 'post format' ),
129 'rewrite' => $rewrite['post_format'],
132 'show_in_nav_menus' => current_theme_supports( 'post-formats' ),
137 * Retrieves a list of registered taxonomy names or objects.
141 * @global array $wp_taxonomies The registered taxonomies.
143 * @param array $args Optional. An array of `key => value` arguments to match against the taxonomy objects.
144 * Default empty array.
145 * @param string $output Optional. The type of output to return in the array. Accepts either taxonomy 'names'
146 * or 'objects'. Default 'names'.
147 * @param string $operator Optional. The logical operation to perform. Accepts 'and' or 'or'. 'or' means only
148 * one element from the array needs to match; 'and' means all elements must match.
150 * @return array A list of taxonomy names or objects.
152 function get_taxonomies( $args = array(), $output = 'names', $operator = 'and' ) {
153 global $wp_taxonomies;
155 $field = ('names' == $output) ? 'name' : false;
157 return wp_filter_object_list($wp_taxonomies, $args, $operator, $field);
161 * Return all of the taxonomy names that are of $object_type.
163 * It appears that this function can be used to find all of the names inside of
164 * $wp_taxonomies global variable.
166 * `<?php $taxonomies = get_object_taxonomies('post'); ?>` Should
167 * result in `Array( 'category', 'post_tag' )`
171 * @global array $wp_taxonomies The registered taxonomies.
173 * @param array|string|WP_Post $object Name of the type of taxonomy object, or an object (row from posts)
174 * @param string $output Optional. The type of output to return in the array. Accepts either
175 * taxonomy 'names' or 'objects'. Default 'names'.
176 * @return array The names of all taxonomy of $object_type.
178 function get_object_taxonomies( $object, $output = 'names' ) {
179 global $wp_taxonomies;
181 if ( is_object($object) ) {
182 if ( $object->post_type == 'attachment' )
183 return get_attachment_taxonomies($object);
184 $object = $object->post_type;
187 $object = (array) $object;
189 $taxonomies = array();
190 foreach ( (array) $wp_taxonomies as $tax_name => $tax_obj ) {
191 if ( array_intersect($object, (array) $tax_obj->object_type) ) {
192 if ( 'names' == $output )
193 $taxonomies[] = $tax_name;
195 $taxonomies[ $tax_name ] = $tax_obj;
203 * Retrieves the taxonomy object of $taxonomy.
205 * The get_taxonomy function will first check that the parameter string given
206 * is a taxonomy object and if it is, it will return it.
210 * @global array $wp_taxonomies The registered taxonomies.
212 * @param string $taxonomy Name of taxonomy object to return.
213 * @return object|false The Taxonomy Object or false if $taxonomy doesn't exist.
215 function get_taxonomy( $taxonomy ) {
216 global $wp_taxonomies;
218 if ( ! taxonomy_exists( $taxonomy ) )
221 return $wp_taxonomies[$taxonomy];
225 * Checks that the taxonomy name exists.
227 * Formerly is_taxonomy(), introduced in 2.3.0.
231 * @global array $wp_taxonomies The registered taxonomies.
233 * @param string $taxonomy Name of taxonomy object.
234 * @return bool Whether the taxonomy exists.
236 function taxonomy_exists( $taxonomy ) {
237 global $wp_taxonomies;
239 return isset( $wp_taxonomies[$taxonomy] );
243 * Whether the taxonomy object is hierarchical.
245 * Checks to make sure that the taxonomy is an object first. Then Gets the
246 * object, and finally returns the hierarchical value in the object.
248 * A false return value might also mean that the taxonomy does not exist.
252 * @param string $taxonomy Name of taxonomy object.
253 * @return bool Whether the taxonomy is hierarchical.
255 function is_taxonomy_hierarchical($taxonomy) {
256 if ( ! taxonomy_exists($taxonomy) )
259 $taxonomy = get_taxonomy($taxonomy);
260 return $taxonomy->hierarchical;
264 * Create or modify a taxonomy object. Do not use before init.
266 * A simple function for creating or modifying a taxonomy object based on the
267 * parameters given. The function will accept an array (third optional
268 * parameter), along with strings for the taxonomy name and another string for
271 * Nothing is returned, so expect error maybe or use taxonomy_exists() to check
272 * whether taxonomy exists.
274 * Optional $args contents:
276 * - label - Name of the taxonomy shown in the menu. Usually plural. If not set, labels['name'] will be used.
277 * - labels - An array of labels for this taxonomy.
278 * * By default tag labels are used for non-hierarchical types and category labels for hierarchical ones.
279 * * You can see accepted values in {@link get_taxonomy_labels()}.
280 * - description - A short descriptive summary of what the taxonomy is for. Defaults to blank.
281 * - public - If the taxonomy should be publicly queryable; //@TODO not implemented.
282 * * Defaults to true.
283 * - hierarchical - Whether the taxonomy is hierarchical (e.g. category). Defaults to false.
284 * - show_ui - Whether to generate a default UI for managing this taxonomy in the admin.
285 * * If not set, the default is inherited from public.
286 * - show_in_menu - Whether to show the taxonomy in the admin menu.
287 * * If true, the taxonomy is shown as a submenu of the object type menu.
288 * * If false, no menu is shown.
289 * * show_ui must be true.
290 * * If not set, the default is inherited from show_ui.
291 * - show_in_nav_menus - Makes this taxonomy available for selection in navigation menus.
292 * * If not set, the default is inherited from public.
293 * - show_tagcloud - Whether to list the taxonomy in the Tag Cloud Widget.
294 * * If not set, the default is inherited from show_ui.
295 * - show_in_quick_edit - Whether to show the taxonomy in the quick/bulk edit panel.
296 * * It not set, the default is inherited from show_ui.
297 * - show_admin_column - Whether to display a column for the taxonomy on its post type listing screens.
298 * * Defaults to false.
299 * - meta_box_cb - Provide a callback function for the meta box display.
300 * * If not set, defaults to post_categories_meta_box for hierarchical taxonomies
301 * and post_tags_meta_box for non-hierarchical.
302 * * If false, no meta box is shown.
303 * - capabilities - Array of capabilities for this taxonomy.
304 * * You can see accepted values in this function.
305 * - rewrite - Triggers the handling of rewrites for this taxonomy. Defaults to true, using $taxonomy as slug.
306 * * To prevent rewrite, set to false.
307 * * To specify rewrite rules, an array can be passed with any of these keys
308 * * 'slug' => string Customize the permastruct slug. Defaults to $taxonomy key
309 * * 'with_front' => bool Should the permastruct be prepended with WP_Rewrite::$front. Defaults to true.
310 * * 'hierarchical' => bool Either hierarchical rewrite tag or not. Defaults to false.
311 * * 'ep_mask' => const Assign an endpoint mask.
312 * * If not specified, defaults to EP_NONE.
313 * - query_var - Sets the query_var key for this taxonomy. Defaults to $taxonomy key
314 * * If false, a taxonomy cannot be loaded at ?{query_var}={term_slug}
315 * * If specified as a string, the query ?{query_var_string}={term_slug} will be valid.
316 * - update_count_callback - Works much like a hook, in that it will be called when the count is updated.
317 * * Defaults to _update_post_term_count() for taxonomies attached to post types, which then confirms
318 * that the objects are published before counting them.
319 * * Defaults to _update_generic_term_count() for taxonomies attached to other object types, such as links.
320 * - _builtin - true if this taxonomy is a native or "built-in" taxonomy. THIS IS FOR INTERNAL USE ONLY!
322 * @todo Document $args as a hash notation.
325 * @since 4.2.0 Introduced `show_in_quick_edit` argument.
327 * @global array $wp_taxonomies Registered taxonomies.
328 * @global WP $wp WP instance.
330 * @param string $taxonomy Taxonomy key, must not exceed 32 characters.
331 * @param array|string $object_type Name of the object type for the taxonomy object.
332 * @param array|string $args See optional args description above.
333 * @return WP_Error|void WP_Error, if errors.
335 function register_taxonomy( $taxonomy, $object_type, $args = array() ) {
336 global $wp_taxonomies, $wp;
338 if ( ! is_array( $wp_taxonomies ) )
339 $wp_taxonomies = array();
345 'hierarchical' => false,
347 'show_in_menu' => null,
348 'show_in_nav_menus' => null,
349 'show_tagcloud' => null,
350 'show_in_quick_edit' => null,
351 'show_admin_column' => false,
352 'meta_box_cb' => null,
353 'capabilities' => array(),
355 'query_var' => $taxonomy,
356 'update_count_callback' => '',
359 $args = wp_parse_args( $args, $defaults );
361 if ( empty( $taxonomy ) || strlen( $taxonomy ) > 32 ) {
362 _doing_it_wrong( __FUNCTION__, __( 'Taxonomy names must be between 1 and 32 characters in length.' ), '4.2' );
363 return new WP_Error( 'taxonomy_length_invalid', __( 'Taxonomy names must be between 1 and 32 characters in length.' ) );
366 if ( false !== $args['query_var'] && ! empty( $wp ) ) {
367 if ( true === $args['query_var'] )
368 $args['query_var'] = $taxonomy;
370 $args['query_var'] = sanitize_title_with_dashes( $args['query_var'] );
371 $wp->add_query_var( $args['query_var'] );
374 if ( false !== $args['rewrite'] && ( is_admin() || '' != get_option( 'permalink_structure' ) ) ) {
375 $args['rewrite'] = wp_parse_args( $args['rewrite'], array(
376 'with_front' => true,
377 'hierarchical' => false,
378 'ep_mask' => EP_NONE,
381 if ( empty( $args['rewrite']['slug'] ) )
382 $args['rewrite']['slug'] = sanitize_title_with_dashes( $taxonomy );
384 if ( $args['hierarchical'] && $args['rewrite']['hierarchical'] )
389 add_rewrite_tag( "%$taxonomy%", $tag, $args['query_var'] ? "{$args['query_var']}=" : "taxonomy=$taxonomy&term=" );
390 add_permastruct( $taxonomy, "{$args['rewrite']['slug']}/%$taxonomy%", $args['rewrite'] );
393 // If not set, default to the setting for public.
394 if ( null === $args['show_ui'] )
395 $args['show_ui'] = $args['public'];
397 // If not set, default to the setting for show_ui.
398 if ( null === $args['show_in_menu' ] || ! $args['show_ui'] )
399 $args['show_in_menu' ] = $args['show_ui'];
401 // If not set, default to the setting for public.
402 if ( null === $args['show_in_nav_menus'] )
403 $args['show_in_nav_menus'] = $args['public'];
405 // If not set, default to the setting for show_ui.
406 if ( null === $args['show_tagcloud'] )
407 $args['show_tagcloud'] = $args['show_ui'];
409 // If not set, default to the setting for show_ui.
410 if ( null === $args['show_in_quick_edit'] ) {
411 $args['show_in_quick_edit'] = $args['show_ui'];
414 $default_caps = array(
415 'manage_terms' => 'manage_categories',
416 'edit_terms' => 'manage_categories',
417 'delete_terms' => 'manage_categories',
418 'assign_terms' => 'edit_posts',
420 $args['cap'] = (object) array_merge( $default_caps, $args['capabilities'] );
421 unset( $args['capabilities'] );
423 $args['name'] = $taxonomy;
424 $args['object_type'] = array_unique( (array) $object_type );
426 $args['labels'] = get_taxonomy_labels( (object) $args );
427 $args['label'] = $args['labels']->name;
429 // If not set, use the default meta box
430 if ( null === $args['meta_box_cb'] ) {
431 if ( $args['hierarchical'] )
432 $args['meta_box_cb'] = 'post_categories_meta_box';
434 $args['meta_box_cb'] = 'post_tags_meta_box';
437 $wp_taxonomies[ $taxonomy ] = (object) $args;
439 // register callback handling for metabox
440 add_filter( 'wp_ajax_add-' . $taxonomy, '_wp_ajax_add_hierarchical_term' );
443 * Fires after a taxonomy is registered.
447 * @param string $taxonomy Taxonomy slug.
448 * @param array|string $object_type Object type or array of object types.
449 * @param array $args Array of taxonomy registration arguments.
451 do_action( 'registered_taxonomy', $taxonomy, $object_type, $args );
455 * Builds an object with all taxonomy labels out of a taxonomy object
457 * Accepted keys of the label array in the taxonomy object:
459 * - name - general name for the taxonomy, usually plural. The same as and overridden by $tax->label. Default is Tags/Categories
460 * - singular_name - name for one object of this taxonomy. Default is Tag/Category
461 * - search_items - Default is Search Tags/Search Categories
462 * - popular_items - This string isn't used on hierarchical taxonomies. Default is Popular Tags
463 * - all_items - Default is All Tags/All Categories
464 * - parent_item - This string isn't used on non-hierarchical taxonomies. In hierarchical ones the default is Parent Category
465 * - parent_item_colon - The same as `parent_item`, but with colon `:` in the end
466 * - edit_item - Default is Edit Tag/Edit Category
467 * - view_item - Default is View Tag/View Category
468 * - update_item - Default is Update Tag/Update Category
469 * - add_new_item - Default is Add New Tag/Add New Category
470 * - new_item_name - Default is New Tag Name/New Category Name
471 * - separate_items_with_commas - This string isn't used on hierarchical taxonomies. Default is "Separate tags with commas", used in the meta box.
472 * - 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.
473 * - 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.
474 * - not_found - Default is "No tags found"/"No categories found", used in the meta box and taxonomy list table.
475 * - no_terms - Default is "No tags"/"No categories", used in the posts and media list tables.
477 * Above, the first default value is for non-hierarchical taxonomies (like tags) and the second one is for hierarchical taxonomies (like categories).
479 * @todo Better documentation for the labels array.
482 * @since 4.3.0 Added the `no_terms` label.
484 * @param object $tax Taxonomy object.
485 * @return object object with all the labels as member variables.
487 function get_taxonomy_labels( $tax ) {
488 $tax->labels = (array) $tax->labels;
490 if ( isset( $tax->helps ) && empty( $tax->labels['separate_items_with_commas'] ) )
491 $tax->labels['separate_items_with_commas'] = $tax->helps;
493 if ( isset( $tax->no_tagcloud ) && empty( $tax->labels['not_found'] ) )
494 $tax->labels['not_found'] = $tax->no_tagcloud;
496 $nohier_vs_hier_defaults = array(
497 'name' => array( _x( 'Tags', 'taxonomy general name' ), _x( 'Categories', 'taxonomy general name' ) ),
498 'singular_name' => array( _x( 'Tag', 'taxonomy singular name' ), _x( 'Category', 'taxonomy singular name' ) ),
499 'search_items' => array( __( 'Search Tags' ), __( 'Search Categories' ) ),
500 'popular_items' => array( __( 'Popular Tags' ), null ),
501 'all_items' => array( __( 'All Tags' ), __( 'All Categories' ) ),
502 'parent_item' => array( null, __( 'Parent Category' ) ),
503 'parent_item_colon' => array( null, __( 'Parent Category:' ) ),
504 'edit_item' => array( __( 'Edit Tag' ), __( 'Edit Category' ) ),
505 'view_item' => array( __( 'View Tag' ), __( 'View Category' ) ),
506 'update_item' => array( __( 'Update Tag' ), __( 'Update Category' ) ),
507 'add_new_item' => array( __( 'Add New Tag' ), __( 'Add New Category' ) ),
508 'new_item_name' => array( __( 'New Tag Name' ), __( 'New Category Name' ) ),
509 'separate_items_with_commas' => array( __( 'Separate tags with commas' ), null ),
510 'add_or_remove_items' => array( __( 'Add or remove tags' ), null ),
511 'choose_from_most_used' => array( __( 'Choose from the most used tags' ), null ),
512 'not_found' => array( __( 'No tags found.' ), __( 'No categories found.' ) ),
513 'no_terms' => array( __( 'No tags' ), __( 'No categories' ) ),
515 $nohier_vs_hier_defaults['menu_name'] = $nohier_vs_hier_defaults['name'];
517 return _get_custom_object_labels( $tax, $nohier_vs_hier_defaults );
521 * Add an already registered taxonomy to an object type.
525 * @global array $wp_taxonomies The registered taxonomies.
527 * @param string $taxonomy Name of taxonomy object.
528 * @param string $object_type Name of the object type.
529 * @return bool True if successful, false if not.
531 function register_taxonomy_for_object_type( $taxonomy, $object_type) {
532 global $wp_taxonomies;
534 if ( !isset($wp_taxonomies[$taxonomy]) )
537 if ( ! get_post_type_object($object_type) )
540 if ( ! in_array( $object_type, $wp_taxonomies[$taxonomy]->object_type ) )
541 $wp_taxonomies[$taxonomy]->object_type[] = $object_type;
543 // Filter out empties.
544 $wp_taxonomies[ $taxonomy ]->object_type = array_filter( $wp_taxonomies[ $taxonomy ]->object_type );
550 * Remove an already registered taxonomy from an object type.
554 * @global array $wp_taxonomies The registered taxonomies.
556 * @param string $taxonomy Name of taxonomy object.
557 * @param string $object_type Name of the object type.
558 * @return bool True if successful, false if not.
560 function unregister_taxonomy_for_object_type( $taxonomy, $object_type ) {
561 global $wp_taxonomies;
563 if ( ! isset( $wp_taxonomies[ $taxonomy ] ) )
566 if ( ! get_post_type_object( $object_type ) )
569 $key = array_search( $object_type, $wp_taxonomies[ $taxonomy ]->object_type, true );
570 if ( false === $key )
573 unset( $wp_taxonomies[ $taxonomy ]->object_type[ $key ] );
582 * Retrieve object_ids of valid taxonomy and term.
584 * The strings of $taxonomies must exist before this function will continue. On
585 * failure of finding a valid taxonomy, it will return an WP_Error class, kind
586 * of like Exceptions in PHP 5, except you can't catch them. Even so, you can
587 * still test for the WP_Error class and get the error message.
589 * The $terms aren't checked the same as $taxonomies, but still need to exist
590 * for $object_ids to be returned.
592 * It is possible to change the order that object_ids is returned by either
593 * using PHP sort family functions or using the database by using $args with
594 * either ASC or DESC array. The value should be in the key named 'order'.
598 * @global wpdb $wpdb WordPress database abstraction object.
600 * @param int|array $term_ids Term id or array of term ids of terms that will be used.
601 * @param string|array $taxonomies String of taxonomy name or Array of string values of taxonomy names.
602 * @param array|string $args Change the order of the object_ids, either ASC or DESC.
603 * @return WP_Error|array If the taxonomy does not exist, then WP_Error will be returned. On success.
604 * the array can be empty meaning that there are no $object_ids found or it will return the $object_ids found.
606 function get_objects_in_term( $term_ids, $taxonomies, $args = array() ) {
609 if ( ! is_array( $term_ids ) ) {
610 $term_ids = array( $term_ids );
612 if ( ! is_array( $taxonomies ) ) {
613 $taxonomies = array( $taxonomies );
615 foreach ( (array) $taxonomies as $taxonomy ) {
616 if ( ! taxonomy_exists( $taxonomy ) ) {
617 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) );
621 $defaults = array( 'order' => 'ASC' );
622 $args = wp_parse_args( $args, $defaults );
624 $order = ( 'desc' == strtolower( $args['order'] ) ) ? 'DESC' : 'ASC';
626 $term_ids = array_map('intval', $term_ids );
628 $taxonomies = "'" . implode( "', '", $taxonomies ) . "'";
629 $term_ids = "'" . implode( "', '", $term_ids ) . "'";
631 $object_ids = $wpdb->get_col("SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tt.term_id IN ($term_ids) ORDER BY tr.object_id $order");
633 if ( ! $object_ids ){
640 * Given a taxonomy query, generates SQL to be appended to a main query.
646 * @param array $tax_query A compact tax query
647 * @param string $primary_table
648 * @param string $primary_id_column
651 function get_tax_sql( $tax_query, $primary_table, $primary_id_column ) {
652 $tax_query_obj = new WP_Tax_Query( $tax_query );
653 return $tax_query_obj->get_sql( $primary_table, $primary_id_column );
657 * Class for generating SQL clauses that filter a primary query according to object taxonomy terms.
659 * `WP_Tax_Query` is a helper that allows primary query classes, such as WP_Query, to filter
660 * their results by object metadata, by generating `JOIN` and `WHERE` subclauses to be attached
661 * to the primary SQL query string.
668 * Array of taxonomy queries.
670 * See {@see WP_Tax_Query::__construct()} for information on tax query arguments.
676 public $queries = array();
679 * The relation between the queries. Can be one of 'AND' or 'OR'.
688 * Standard response when the query should not return any rows.
696 private static $no_results = array( 'join' => array( '' ), 'where' => array( '0 = 1' ) );
699 * A flat list of table aliases used in the JOIN clauses.
705 protected $table_aliases = array();
708 * Terms and taxonomies fetched by this query.
710 * We store this data in a flat array because they are referenced in a
711 * number of places by WP_Query.
717 public $queried_terms = array();
720 * Database table that where the metadata's objects are stored (eg $wpdb->users).
726 public $primary_table;
729 * Column in 'primary_table' that represents the ID of the object.
735 public $primary_id_column;
741 * @since 4.1.0 Added support for `$operator` 'NOT EXISTS' and 'EXISTS' values.
744 * @param array $tax_query {
745 * Array of taxonomy query clauses.
747 * @type string $relation Optional. The MySQL keyword used to join
748 * the clauses of the query. Accepts 'AND', or 'OR'. Default 'AND'.
750 * Optional. An array of first-order clause parameters, or another fully-formed tax query.
752 * @type string $taxonomy Taxonomy being queried. Optional when field=term_taxonomy_id.
753 * @type string|int|array $terms Term or terms to filter by.
754 * @type string $field Field to match $terms against. Accepts 'term_id', 'slug',
755 * 'name', or 'term_taxonomy_id'. Default: 'term_id'.
756 * @type string $operator MySQL operator to be used with $terms in the WHERE clause.
757 * Accepts 'AND', 'IN', 'NOT IN', 'EXISTS', 'NOT EXISTS'.
759 * @type bool $include_children Optional. Whether to include child terms.
760 * Requires a $taxonomy. Default: true.
764 public function __construct( $tax_query ) {
765 if ( isset( $tax_query['relation'] ) ) {
766 $this->relation = $this->sanitize_relation( $tax_query['relation'] );
768 $this->relation = 'AND';
771 $this->queries = $this->sanitize_query( $tax_query );
775 * Ensure the 'tax_query' argument passed to the class constructor is well-formed.
777 * Ensures that each query-level clause has a 'relation' key, and that
778 * each first-order clause contains all the necessary keys from `$defaults`.
783 * @param array $queries Array of queries clauses.
784 * @return array Sanitized array of query clauses.
786 public function sanitize_query( $queries ) {
787 $cleaned_query = array();
792 'field' => 'term_id',
794 'include_children' => true,
797 foreach ( $queries as $key => $query ) {
798 if ( 'relation' === $key ) {
799 $cleaned_query['relation'] = $this->sanitize_relation( $query );
801 // First-order clause.
802 } elseif ( self::is_first_order_clause( $query ) ) {
804 $cleaned_clause = array_merge( $defaults, $query );
805 $cleaned_clause['terms'] = (array) $cleaned_clause['terms'];
806 $cleaned_query[] = $cleaned_clause;
809 * Keep a copy of the clause in the flate
810 * $queried_terms array, for use in WP_Query.
812 if ( ! empty( $cleaned_clause['taxonomy'] ) && 'NOT IN' !== $cleaned_clause['operator'] ) {
813 $taxonomy = $cleaned_clause['taxonomy'];
814 if ( ! isset( $this->queried_terms[ $taxonomy ] ) ) {
815 $this->queried_terms[ $taxonomy ] = array();
819 * Backward compatibility: Only store the first
820 * 'terms' and 'field' found for a given taxonomy.
822 if ( ! empty( $cleaned_clause['terms'] ) && ! isset( $this->queried_terms[ $taxonomy ]['terms'] ) ) {
823 $this->queried_terms[ $taxonomy ]['terms'] = $cleaned_clause['terms'];
826 if ( ! empty( $cleaned_clause['field'] ) && ! isset( $this->queried_terms[ $taxonomy ]['field'] ) ) {
827 $this->queried_terms[ $taxonomy ]['field'] = $cleaned_clause['field'];
831 // Otherwise, it's a nested query, so we recurse.
832 } elseif ( is_array( $query ) ) {
833 $cleaned_subquery = $this->sanitize_query( $query );
835 if ( ! empty( $cleaned_subquery ) ) {
836 // All queries with children must have a relation.
837 if ( ! isset( $cleaned_subquery['relation'] ) ) {
838 $cleaned_subquery['relation'] = 'AND';
841 $cleaned_query[] = $cleaned_subquery;
846 return $cleaned_query;
850 * Sanitize a 'relation' operator.
855 * @param string $relation Raw relation key from the query argument.
856 * @return string Sanitized relation ('AND' or 'OR').
858 public function sanitize_relation( $relation ) {
859 if ( 'OR' === strtoupper( $relation ) ) {
867 * Determine whether a clause is first-order.
869 * A "first-order" clause is one that contains any of the first-order
870 * clause keys ('terms', 'taxonomy', 'include_children', 'field',
871 * 'operator'). An empty clause also counts as a first-order clause,
872 * for backward compatibility. Any clause that doesn't meet this is
873 * determined, by process of elimination, to be a higher-order query.
880 * @param array $query Tax query arguments.
881 * @return bool Whether the query clause is a first-order clause.
883 protected static function is_first_order_clause( $query ) {
884 return is_array( $query ) && ( empty( $query ) || array_key_exists( 'terms', $query ) || array_key_exists( 'taxonomy', $query ) || array_key_exists( 'include_children', $query ) || array_key_exists( 'field', $query ) || array_key_exists( 'operator', $query ) );
888 * Generates SQL clauses to be appended to a main query.
895 * @param string $primary_table Database table where the object being filtered is stored (eg wp_users).
896 * @param string $primary_id_column ID column for the filtered object in $primary_table.
898 * Array containing JOIN and WHERE SQL clauses to append to the main query.
900 * @type string $join SQL fragment to append to the main JOIN clause.
901 * @type string $where SQL fragment to append to the main WHERE clause.
904 public function get_sql( $primary_table, $primary_id_column ) {
905 $this->primary_table = $primary_table;
906 $this->primary_id_column = $primary_id_column;
908 return $this->get_sql_clauses();
912 * Generate SQL clauses to be appended to a main query.
914 * Called by the public WP_Tax_Query::get_sql(), this method
915 * is abstracted out to maintain parity with the other Query classes.
921 * Array containing JOIN and WHERE SQL clauses to append to the main query.
923 * @type string $join SQL fragment to append to the main JOIN clause.
924 * @type string $where SQL fragment to append to the main WHERE clause.
927 protected function get_sql_clauses() {
929 * $queries are passed by reference to get_sql_for_query() for recursion.
930 * To keep $this->queries unaltered, pass a copy.
932 $queries = $this->queries;
933 $sql = $this->get_sql_for_query( $queries );
935 if ( ! empty( $sql['where'] ) ) {
936 $sql['where'] = ' AND ' . $sql['where'];
943 * Generate SQL clauses for a single query array.
945 * If nested subqueries are found, this method recurses the tree to
946 * produce the properly nested SQL.
951 * @param array $query Query to parse, passed by reference.
952 * @param int $depth Optional. Number of tree levels deep we currently are.
953 * Used to calculate indentation. Default 0.
955 * Array containing JOIN and WHERE SQL clauses to append to a single query array.
957 * @type string $join SQL fragment to append to the main JOIN clause.
958 * @type string $where SQL fragment to append to the main WHERE clause.
961 protected function get_sql_for_query( &$query, $depth = 0 ) {
973 for ( $i = 0; $i < $depth; $i++ ) {
977 foreach ( $query as $key => &$clause ) {
978 if ( 'relation' === $key ) {
979 $relation = $query['relation'];
980 } elseif ( is_array( $clause ) ) {
982 // This is a first-order clause.
983 if ( $this->is_first_order_clause( $clause ) ) {
984 $clause_sql = $this->get_sql_for_clause( $clause, $query );
986 $where_count = count( $clause_sql['where'] );
987 if ( ! $where_count ) {
988 $sql_chunks['where'][] = '';
989 } elseif ( 1 === $where_count ) {
990 $sql_chunks['where'][] = $clause_sql['where'][0];
992 $sql_chunks['where'][] = '( ' . implode( ' AND ', $clause_sql['where'] ) . ' )';
995 $sql_chunks['join'] = array_merge( $sql_chunks['join'], $clause_sql['join'] );
996 // This is a subquery, so we recurse.
998 $clause_sql = $this->get_sql_for_query( $clause, $depth + 1 );
1000 $sql_chunks['where'][] = $clause_sql['where'];
1001 $sql_chunks['join'][] = $clause_sql['join'];
1006 // Filter to remove empties.
1007 $sql_chunks['join'] = array_filter( $sql_chunks['join'] );
1008 $sql_chunks['where'] = array_filter( $sql_chunks['where'] );
1010 if ( empty( $relation ) ) {
1014 // Filter duplicate JOIN clauses and combine into a single string.
1015 if ( ! empty( $sql_chunks['join'] ) ) {
1016 $sql['join'] = implode( ' ', array_unique( $sql_chunks['join'] ) );
1019 // Generate a single WHERE clause with proper brackets and indentation.
1020 if ( ! empty( $sql_chunks['where'] ) ) {
1021 $sql['where'] = '( ' . "\n " . $indent . implode( ' ' . "\n " . $indent . $relation . ' ' . "\n " . $indent, $sql_chunks['where'] ) . "\n" . $indent . ')';
1028 * Generate SQL JOIN and WHERE clauses for a "first-order" query clause.
1033 * @global wpdb $wpdb The WordPress database abstraction object.
1035 * @param array $clause Query clause, passed by reference.
1036 * @param array $parent_query Parent query array.
1038 * Array containing JOIN and WHERE SQL clauses to append to a first-order query.
1040 * @type string $join SQL fragment to append to the main JOIN clause.
1041 * @type string $where SQL fragment to append to the main WHERE clause.
1044 public function get_sql_for_clause( &$clause, $parent_query ) {
1052 $join = $where = '';
1054 $this->clean_query( $clause );
1056 if ( is_wp_error( $clause ) ) {
1057 return self::$no_results;
1060 $terms = $clause['terms'];
1061 $operator = strtoupper( $clause['operator'] );
1063 if ( 'IN' == $operator ) {
1065 if ( empty( $terms ) ) {
1066 return self::$no_results;
1069 $terms = implode( ',', $terms );
1072 * Before creating another table join, see if this clause has a
1073 * sibling with an existing join that can be shared.
1075 $alias = $this->find_compatible_table_alias( $clause, $parent_query );
1076 if ( false === $alias ) {
1077 $i = count( $this->table_aliases );
1078 $alias = $i ? 'tt' . $i : $wpdb->term_relationships;
1080 // Store the alias as part of a flat array to build future iterators.
1081 $this->table_aliases[] = $alias;
1083 // Store the alias with this clause, so later siblings can use it.
1084 $clause['alias'] = $alias;
1086 $join .= " INNER JOIN $wpdb->term_relationships";
1087 $join .= $i ? " AS $alias" : '';
1088 $join .= " ON ($this->primary_table.$this->primary_id_column = $alias.object_id)";
1092 $where = "$alias.term_taxonomy_id $operator ($terms)";
1094 } elseif ( 'NOT IN' == $operator ) {
1096 if ( empty( $terms ) ) {
1100 $terms = implode( ',', $terms );
1102 $where = "$this->primary_table.$this->primary_id_column NOT IN (
1104 FROM $wpdb->term_relationships
1105 WHERE term_taxonomy_id IN ($terms)
1108 } elseif ( 'AND' == $operator ) {
1110 if ( empty( $terms ) ) {
1114 $num_terms = count( $terms );
1116 $terms = implode( ',', $terms );
1120 FROM $wpdb->term_relationships
1121 WHERE term_taxonomy_id IN ($terms)
1122 AND object_id = $this->primary_table.$this->primary_id_column
1125 } elseif ( 'NOT EXISTS' === $operator || 'EXISTS' === $operator ) {
1127 $where = $wpdb->prepare( "$operator (
1129 FROM $wpdb->term_relationships
1130 INNER JOIN $wpdb->term_taxonomy
1131 ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id
1132 WHERE $wpdb->term_taxonomy.taxonomy = %s
1133 AND $wpdb->term_relationships.object_id = $this->primary_table.$this->primary_id_column
1134 )", $clause['taxonomy'] );
1138 $sql['join'][] = $join;
1139 $sql['where'][] = $where;
1144 * Identify an existing table alias that is compatible with the current query clause.
1146 * We avoid unnecessary table joins by allowing each clause to look for
1147 * an existing table alias that is compatible with the query that it
1150 * An existing alias is compatible if (a) it is a sibling of `$clause`
1151 * (ie, it's under the scope of the same relation), and (b) the combination
1152 * of operator and relation between the clauses allows for a shared table
1153 * join. In the case of WP_Tax_Query, this only applies to 'IN'
1154 * clauses that are connected by the relation 'OR'.
1159 * @param array $clause Query clause.
1160 * @param array $parent_query Parent query of $clause.
1161 * @return string|false Table alias if found, otherwise false.
1163 protected function find_compatible_table_alias( $clause, $parent_query ) {
1166 // Sanity check. Only IN queries use the JOIN syntax .
1167 if ( ! isset( $clause['operator'] ) || 'IN' !== $clause['operator'] ) {
1171 // Since we're only checking IN queries, we're only concerned with OR relations.
1172 if ( ! isset( $parent_query['relation'] ) || 'OR' !== $parent_query['relation'] ) {
1176 $compatible_operators = array( 'IN' );
1178 foreach ( $parent_query as $sibling ) {
1179 if ( ! is_array( $sibling ) || ! $this->is_first_order_clause( $sibling ) ) {
1183 if ( empty( $sibling['alias'] ) || empty( $sibling['operator'] ) ) {
1187 // The sibling must both have compatible operator to share its alias.
1188 if ( in_array( strtoupper( $sibling['operator'] ), $compatible_operators ) ) {
1189 $alias = $sibling['alias'];
1198 * Validates a single query.
1203 * @param array &$query The single query.
1205 private function clean_query( &$query ) {
1206 if ( empty( $query['taxonomy'] ) ) {
1207 if ( 'term_taxonomy_id' !== $query['field'] ) {
1208 $query = new WP_Error( 'Invalid taxonomy' );
1212 // so long as there are shared terms, include_children requires that a taxonomy is set
1213 $query['include_children'] = false;
1214 } elseif ( ! taxonomy_exists( $query['taxonomy'] ) ) {
1215 $query = new WP_Error( 'Invalid taxonomy' );
1219 $query['terms'] = array_unique( (array) $query['terms'] );
1221 if ( is_taxonomy_hierarchical( $query['taxonomy'] ) && $query['include_children'] ) {
1222 $this->transform_query( $query, 'term_id' );
1224 if ( is_wp_error( $query ) )
1227 $children = array();
1228 foreach ( $query['terms'] as $term ) {
1229 $children = array_merge( $children, get_term_children( $term, $query['taxonomy'] ) );
1230 $children[] = $term;
1232 $query['terms'] = $children;
1235 $this->transform_query( $query, 'term_taxonomy_id' );
1239 * Transforms a single query, from one field to another.
1243 * @global wpdb $wpdb The WordPress database abstraction object.
1245 * @param array &$query The single query.
1246 * @param string $resulting_field The resulting field. Accepts 'slug', 'name', 'term_taxonomy_id',
1247 * or 'term_id'. Default 'term_id'.
1249 public function transform_query( &$query, $resulting_field ) {
1252 if ( empty( $query['terms'] ) )
1255 if ( $query['field'] == $resulting_field )
1258 $resulting_field = sanitize_key( $resulting_field );
1260 switch ( $query['field'] ) {
1263 foreach ( $query['terms'] as &$term ) {
1265 * 0 is the $term_id parameter. We don't have a term ID yet, but it doesn't
1266 * matter because `sanitize_term_field()` ignores the $term_id param when the
1269 $term = "'" . esc_sql( sanitize_term_field( $query['field'], $term, 0, $query['taxonomy'], 'db' ) ) . "'";
1272 $terms = implode( ",", $query['terms'] );
1274 $terms = $wpdb->get_col( "
1275 SELECT $wpdb->term_taxonomy.$resulting_field
1276 FROM $wpdb->term_taxonomy
1277 INNER JOIN $wpdb->terms USING (term_id)
1278 WHERE taxonomy = '{$query['taxonomy']}'
1279 AND $wpdb->terms.{$query['field']} IN ($terms)
1282 case 'term_taxonomy_id':
1283 $terms = implode( ',', array_map( 'intval', $query['terms'] ) );
1284 $terms = $wpdb->get_col( "
1285 SELECT $resulting_field
1286 FROM $wpdb->term_taxonomy
1287 WHERE term_taxonomy_id IN ($terms)
1291 $terms = implode( ',', array_map( 'intval', $query['terms'] ) );
1292 $terms = $wpdb->get_col( "
1293 SELECT $resulting_field
1294 FROM $wpdb->term_taxonomy
1295 WHERE taxonomy = '{$query['taxonomy']}'
1296 AND term_id IN ($terms)
1300 if ( 'AND' == $query['operator'] && count( $terms ) < count( $query['terms'] ) ) {
1301 $query = new WP_Error( 'Inexistent terms' );
1305 $query['terms'] = $terms;
1306 $query['field'] = $resulting_field;
1311 * Get all Term data from database by Term ID.
1313 * The usage of the get_term function is to apply filters to a term object. It
1314 * is possible to get a term object from the database before applying the
1317 * $term ID must be part of $taxonomy, to get from the database. Failure, might
1318 * be able to be captured by the hooks. Failure would be the same value as $wpdb
1319 * returns for the get_row method.
1321 * There are two hooks, one is specifically for each term, named 'get_term', and
1322 * the second is for the taxonomy name, 'term_$taxonomy'. Both hooks gets the
1323 * term object, and the taxonomy name as parameters. Both hooks are expected to
1324 * return a Term object.
1326 * {@see 'get_term'} hook - Takes two parameters the term Object and the taxonomy name.
1327 * Must return term object. Used in get_term() as a catch-all filter for every
1330 * {@see 'get_$taxonomy'} hook - Takes two parameters the term Object and the taxonomy
1331 * name. Must return term object. $taxonomy will be the taxonomy name, so for
1332 * example, if 'category', it would be 'get_category' as the filter name. Useful
1333 * for custom taxonomies or plugging into default taxonomies.
1335 * @todo Better formatting for DocBlock
1339 * @global wpdb $wpdb WordPress database abstraction object.
1340 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
1342 * @param int|object $term If integer, will get from database. If object will apply filters and return $term.
1343 * @param string $taxonomy Taxonomy name that $term is part of.
1344 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
1345 * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
1346 * @return object|array|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not
1347 * exist then WP_Error will be returned.
1349 function get_term($term, $taxonomy, $output = OBJECT, $filter = 'raw') {
1352 if ( empty( $term ) ) {
1353 return new WP_Error( 'invalid_term', __( 'Empty Term' ) );
1356 if ( ! taxonomy_exists( $taxonomy ) ) {
1357 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) );
1360 if ( is_object($term) && empty($term->filter) ) {
1361 wp_cache_add( $term->term_id, $term, $taxonomy );
1364 if ( is_object($term) )
1365 $term = $term->term_id;
1366 if ( !$term = (int) $term )
1368 if ( ! $_term = wp_cache_get( $term, $taxonomy ) ) {
1369 $_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND t.term_id = %d LIMIT 1", $taxonomy, $term) );
1372 wp_cache_add( $term, $_term, $taxonomy );
1381 * @param int|object $_term Term object or ID.
1382 * @param string $taxonomy The taxonomy slug.
1384 $_term = apply_filters( 'get_term', $_term, $taxonomy );
1387 * Filter a taxonomy.
1389 * The dynamic portion of the filter name, `$taxonomy`, refers
1390 * to the taxonomy slug.
1394 * @param int|object $_term Term object or ID.
1395 * @param string $taxonomy The taxonomy slug.
1397 $_term = apply_filters( "get_$taxonomy", $_term, $taxonomy );
1398 $_term = sanitize_term($_term, $taxonomy, $filter);
1400 if ( $output == OBJECT ) {
1402 } elseif ( $output == ARRAY_A ) {
1403 $__term = get_object_vars($_term);
1405 } elseif ( $output == ARRAY_N ) {
1406 $__term = array_values(get_object_vars($_term));
1414 * Get all Term data from database by Term field and data.
1416 * Warning: $value is not escaped for 'name' $field. You must do it yourself, if
1419 * The default $field is 'id', therefore it is possible to also use null for
1420 * field, but not recommended that you do so.
1422 * If $value does not exist, the return value will be false. If $taxonomy exists
1423 * and $field and $value combinations exist, the Term will be returned.
1425 * @todo Better formatting for DocBlock.
1429 * @global wpdb $wpdb WordPress database abstraction object.
1430 * @see sanitize_term_field() The $context param lists the available values for get_term_by() $filter param.
1432 * @param string $field Either 'slug', 'name', 'id' (term_id), or 'term_taxonomy_id'
1433 * @param string|int $value Search for this term value
1434 * @param string $taxonomy Taxonomy Name
1435 * @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N
1436 * @param string $filter Optional, default is raw or no WordPress defined filter will applied.
1437 * @return object|array|null|WP_Error|false Term Row from database.
1438 * Will return false if $taxonomy does not exist or $term was not found.
1440 function get_term_by($field, $value, $taxonomy, $output = OBJECT, $filter = 'raw') {
1443 if ( ! taxonomy_exists($taxonomy) )
1446 if ( 'slug' == $field ) {
1448 $value = sanitize_title($value);
1449 if ( empty($value) )
1451 } elseif ( 'name' == $field ) {
1452 // Assume already escaped
1453 $value = wp_unslash($value);
1455 } elseif ( 'term_taxonomy_id' == $field ) {
1456 $value = (int) $value;
1457 $field = 'tt.term_taxonomy_id';
1459 $term = get_term( (int) $value, $taxonomy, $output, $filter );
1460 if ( is_wp_error( $term ) )
1465 $term = $wpdb->get_row( $wpdb->prepare( "SELECT t.*, tt.* FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id WHERE tt.taxonomy = %s AND $field = %s LIMIT 1", $taxonomy, $value ) );
1469 wp_cache_add( $term->term_id, $term, $taxonomy );
1471 /** This filter is documented in wp-includes/taxonomy.php */
1472 $term = apply_filters( 'get_term', $term, $taxonomy );
1474 /** This filter is documented in wp-includes/taxonomy.php */
1475 $term = apply_filters( "get_$taxonomy", $term, $taxonomy );
1477 $term = sanitize_term($term, $taxonomy, $filter);
1479 if ( $output == OBJECT ) {
1481 } elseif ( $output == ARRAY_A ) {
1482 return get_object_vars($term);
1483 } elseif ( $output == ARRAY_N ) {
1484 return array_values(get_object_vars($term));
1491 * Merge all term children into a single array of their IDs.
1493 * This recursive function will merge all of the children of $term into the same
1494 * array of term IDs. Only useful for taxonomies which are hierarchical.
1496 * Will return an empty array if $term does not exist in $taxonomy.
1500 * @param string $term_id ID of Term to get children.
1501 * @param string $taxonomy Taxonomy Name.
1502 * @return array|WP_Error List of Term IDs. WP_Error returned if `$taxonomy` does not exist.
1504 function get_term_children( $term_id, $taxonomy ) {
1505 if ( ! taxonomy_exists($taxonomy) )
1506 return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
1508 $term_id = intval( $term_id );
1510 $terms = _get_term_hierarchy($taxonomy);
1512 if ( ! isset($terms[$term_id]) )
1515 $children = $terms[$term_id];
1517 foreach ( (array) $terms[$term_id] as $child ) {
1518 if ( $term_id == $child ) {
1522 if ( isset($terms[$child]) )
1523 $children = array_merge($children, get_term_children($child, $taxonomy));
1530 * Get sanitized Term field.
1532 * Does checks for $term, based on the $taxonomy. The function is for contextual
1533 * reasons and for simplicity of usage. See sanitize_term_field() for more
1538 * @param string $field Term field to fetch.
1539 * @param int $term Term ID.
1540 * @param string $taxonomy Taxonomy Name.
1541 * @param string $context Optional, default is display. Look at sanitize_term_field() for available options.
1542 * @return string|int|null|WP_Error Will return an empty string if $term is not an object or if $field is not set in $term.
1544 function get_term_field( $field, $term, $taxonomy, $context = 'display' ) {
1545 $term = (int) $term;
1546 $term = get_term( $term, $taxonomy );
1547 if ( is_wp_error($term) )
1550 if ( !is_object($term) )
1553 if ( !isset($term->$field) )
1556 return sanitize_term_field($field, $term->$field, $term->term_id, $taxonomy, $context);
1560 * Sanitizes Term for editing.
1562 * Return value is sanitize_term() and usage is for sanitizing the term for
1563 * editing. Function is for contextual and simplicity.
1567 * @param int|object $id Term ID or object.
1568 * @param string $taxonomy Taxonomy name.
1569 * @return string|int|null|WP_Error Will return empty string if $term is not an object.
1571 function get_term_to_edit( $id, $taxonomy ) {
1572 $term = get_term( $id, $taxonomy );
1574 if ( is_wp_error($term) )
1577 if ( !is_object($term) )
1580 return sanitize_term($term, $taxonomy, 'edit');
1584 * Retrieve the terms in a given taxonomy or list of taxonomies.
1586 * You can fully inject any customizations to the query before it is sent, as
1587 * well as control the output with a filter.
1589 * The {@see 'get_terms'} filter will be called when the cache has the term and will
1590 * pass the found term along with the array of $taxonomies and array of $args.
1591 * This filter is also called before the array of terms is passed and will pass
1592 * the array of terms, along with the $taxonomies and $args.
1594 * The {@see 'list_terms_exclusions'} filter passes the compiled exclusions along with
1597 * The {@see 'get_terms_orderby'} filter passes the `ORDER BY` clause for the query
1598 * along with the $args array.
1601 * @since 4.2.0 Introduced 'name' and 'childless' parameters.
1603 * @global wpdb $wpdb WordPress database abstraction object.
1604 * @global array $wp_filter
1606 * @param string|array $taxonomies Taxonomy name or list of Taxonomy names.
1607 * @param array|string $args {
1608 * Optional. Array or string of arguments to get terms.
1610 * @type string $orderby Field(s) to order terms by. Accepts term fields ('name', 'slug',
1611 * 'term_group', 'term_id', 'id', 'description'), 'count' for term
1612 * taxonomy count, 'include' to match the 'order' of the $include param,
1613 * or 'none' to skip ORDER BY. Defaults to 'name'.
1614 * @type string $order Whether to order terms in ascending or descending order.
1615 * Accepts 'ASC' (ascending) or 'DESC' (descending).
1617 * @type bool|int $hide_empty Whether to hide terms not assigned to any posts. Accepts
1618 * 1|true or 0|false. Default 1|true.
1619 * @type array|string $include Array or comma/space-separated string of term ids to include.
1620 * Default empty array.
1621 * @type array|string $exclude Array or comma/space-separated string of term ids to exclude.
1622 * If $include is non-empty, $exclude is ignored.
1623 * Default empty array.
1624 * @type array|string $exclude_tree Array or comma/space-separated string of term ids to exclude
1625 * along with all of their descendant terms. If $include is
1626 * non-empty, $exclude_tree is ignored. Default empty array.
1627 * @type int|string $number Maximum number of terms to return. Accepts ''|0 (all) or any
1628 * positive number. Default ''|0 (all).
1629 * @type int $offset The number by which to offset the terms query. Default empty.
1630 * @type string $fields Term fields to query for. Accepts 'all' (returns an array of
1631 * term objects), 'ids' or 'names' (returns an array of integers
1632 * or strings, respectively. Default 'all'.
1633 * @type string|array $name Optional. Name or array of names to return term(s) for. Default empty.
1634 * @type string|array $slug Optional. Slug or array of slugs to return term(s) for. Default empty.
1635 * @type bool $hierarchical Whether to include terms that have non-empty descendants (even
1636 * if $hide_empty is set to true). Default true.
1637 * @type string $search Search criteria to match terms. Will be SQL-formatted with
1638 * wildcards before and after. Default empty.
1639 * @type string $name__like Retrieve terms with criteria by which a term is LIKE $name__like.
1641 * @type string $description__like Retrieve terms where the description is LIKE $description__like.
1643 * @type bool $pad_counts Whether to pad the quantity of a term's children in the quantity
1644 * of each term's "count" object variable. Default false.
1645 * @type string $get Whether to return terms regardless of ancestry or whether the terms
1646 * are empty. Accepts 'all' or empty (disabled). Default empty.
1647 * @type int $child_of Term ID to retrieve child terms of. If multiple taxonomies
1648 * are passed, $child_of is ignored. Default 0.
1649 * @type int|string $parent Parent term ID to retrieve direct-child terms of. Default empty.
1650 * @type bool $childless True to limit results to terms that have no children. This parameter has
1651 * no effect on non-hierarchical taxonomies. Default false.
1652 * @type string $cache_domain Unique cache key to be produced when this query is stored in an
1653 * object cache. Default is 'core'.
1655 * @return array|int|WP_Error List of Term Objects and their children. Will return WP_Error, if any of $taxonomies
1658 function get_terms( $taxonomies, $args = '' ) {
1660 $empty_array = array();
1662 $single_taxonomy = ! is_array( $taxonomies ) || 1 === count( $taxonomies );
1663 if ( ! is_array( $taxonomies ) ) {
1664 $taxonomies = array( $taxonomies );
1667 foreach ( $taxonomies as $taxonomy ) {
1668 if ( ! taxonomy_exists($taxonomy) ) {
1669 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) );
1673 $defaults = array('orderby' => 'name', 'order' => 'ASC',
1674 'hide_empty' => true, 'exclude' => array(), 'exclude_tree' => array(), 'include' => array(),
1675 'number' => '', 'fields' => 'all', 'name' => '', 'slug' => '', 'parent' => '', 'childless' => false,
1676 'hierarchical' => true, 'child_of' => 0, 'get' => '', 'name__like' => '', 'description__like' => '',
1677 'pad_counts' => false, 'offset' => '', 'search' => '', 'cache_domain' => 'core' );
1678 $args = wp_parse_args( $args, $defaults );
1679 $args['number'] = absint( $args['number'] );
1680 $args['offset'] = absint( $args['offset'] );
1682 // Save queries by not crawling the tree in the case of multiple taxes or a flat tax.
1683 $has_hierarchical_tax = false;
1684 foreach ( $taxonomies as $_tax ) {
1685 if ( is_taxonomy_hierarchical( $_tax ) ) {
1686 $has_hierarchical_tax = true;
1690 if ( ! $has_hierarchical_tax ) {
1691 $args['hierarchical'] = false;
1692 $args['pad_counts'] = false;
1695 // 'parent' overrides 'child_of'.
1696 if ( 0 < intval( $args['parent'] ) ) {
1697 $args['child_of'] = false;
1700 if ( 'all' == $args['get'] ) {
1701 $args['childless'] = false;
1702 $args['child_of'] = 0;
1703 $args['hide_empty'] = 0;
1704 $args['hierarchical'] = false;
1705 $args['pad_counts'] = false;
1709 * Filter the terms query arguments.
1713 * @param array $args An array of get_term() arguments.
1714 * @param array $taxonomies An array of taxonomies.
1716 $args = apply_filters( 'get_terms_args', $args, $taxonomies );
1718 // Avoid the query if the queried parent/child_of term has no descendants.
1719 $child_of = $args['child_of'];
1720 $parent = $args['parent'];
1723 $_parent = $child_of;
1724 } elseif ( $parent ) {
1731 $in_hierarchy = false;
1732 foreach ( $taxonomies as $_tax ) {
1733 $hierarchy = _get_term_hierarchy( $_tax );
1735 if ( isset( $hierarchy[ $_parent ] ) ) {
1736 $in_hierarchy = true;
1740 if ( ! $in_hierarchy ) {
1741 return $empty_array;
1745 // $args can be whatever, only use the args defined in defaults to compute the key.
1746 $filter_key = ( has_filter('list_terms_exclusions') ) ? serialize($GLOBALS['wp_filter']['list_terms_exclusions']) : '';
1747 $key = md5( serialize( wp_array_slice_assoc( $args, array_keys( $defaults ) ) ) . serialize( $taxonomies ) . $filter_key );
1748 $last_changed = wp_cache_get( 'last_changed', 'terms' );
1749 if ( ! $last_changed ) {
1750 $last_changed = microtime();
1751 wp_cache_set( 'last_changed', $last_changed, 'terms' );
1753 $cache_key = "get_terms:$key:$last_changed";
1754 $cache = wp_cache_get( $cache_key, 'terms' );
1755 if ( false !== $cache ) {
1758 * Filter the given taxonomy's terms cache.
1762 * @param array $cache Cached array of terms for the given taxonomy.
1763 * @param array $taxonomies An array of taxonomies.
1764 * @param array $args An array of get_terms() arguments.
1766 return apply_filters( 'get_terms', $cache, $taxonomies, $args );
1769 $_orderby = strtolower( $args['orderby'] );
1770 if ( 'count' == $_orderby ) {
1771 $orderby = 'tt.count';
1772 } elseif ( 'name' == $_orderby ) {
1773 $orderby = 't.name';
1774 } elseif ( 'slug' == $_orderby ) {
1775 $orderby = 't.slug';
1776 } elseif ( 'include' == $_orderby && ! empty( $args['include'] ) ) {
1777 $include = implode( ',', array_map( 'absint', $args['include'] ) );
1778 $orderby = "FIELD( t.term_id, $include )";
1779 } elseif ( 'term_group' == $_orderby ) {
1780 $orderby = 't.term_group';
1781 } elseif ( 'description' == $_orderby ) {
1782 $orderby = 'tt.description';
1783 } elseif ( 'none' == $_orderby ) {
1785 } elseif ( empty($_orderby) || 'id' == $_orderby ) {
1786 $orderby = 't.term_id';
1788 $orderby = 't.name';
1792 * Filter the ORDERBY clause of the terms query.
1796 * @param string $orderby `ORDERBY` clause of the terms query.
1797 * @param array $args An array of terms query arguments.
1798 * @param array $taxonomies An array of taxonomies.
1800 $orderby = apply_filters( 'get_terms_orderby', $orderby, $args, $taxonomies );
1802 $order = strtoupper( $args['order'] );
1803 if ( ! empty( $orderby ) ) {
1804 $orderby = "ORDER BY $orderby";
1809 if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) ) {
1813 $where = "tt.taxonomy IN ('" . implode("', '", $taxonomies) . "')";
1815 $exclude = $args['exclude'];
1816 $exclude_tree = $args['exclude_tree'];
1817 $include = $args['include'];
1820 if ( ! empty( $include ) ) {
1823 $inclusions = implode( ',', wp_parse_id_list( $include ) );
1826 if ( ! empty( $inclusions ) ) {
1827 $inclusions = ' AND t.term_id IN ( ' . $inclusions . ' )';
1828 $where .= $inclusions;
1831 $exclusions = array();
1832 if ( ! empty( $exclude_tree ) ) {
1833 $exclude_tree = wp_parse_id_list( $exclude_tree );
1834 $excluded_children = $exclude_tree;
1835 foreach ( $exclude_tree as $extrunk ) {
1836 $excluded_children = array_merge(
1838 (array) get_terms( $taxonomies[0], array( 'child_of' => intval( $extrunk ), 'fields' => 'ids', 'hide_empty' => 0 ) )
1841 $exclusions = array_merge( $excluded_children, $exclusions );
1844 if ( ! empty( $exclude ) ) {
1845 $exclusions = array_merge( wp_parse_id_list( $exclude ), $exclusions );
1848 // 'childless' terms are those without an entry in the flattened term hierarchy.
1849 $childless = (bool) $args['childless'];
1851 foreach ( $taxonomies as $_tax ) {
1852 $term_hierarchy = _get_term_hierarchy( $_tax );
1853 $exclusions = array_merge( array_keys( $term_hierarchy ), $exclusions );
1857 if ( ! empty( $exclusions ) ) {
1858 $exclusions = ' AND t.term_id NOT IN (' . implode( ',', array_map( 'intval', $exclusions ) ) . ')';
1864 * Filter the terms to exclude from the terms query.
1868 * @param string $exclusions `NOT IN` clause of the terms query.
1869 * @param array $args An array of terms query arguments.
1870 * @param array $taxonomies An array of taxonomies.
1872 $exclusions = apply_filters( 'list_terms_exclusions', $exclusions, $args, $taxonomies );
1874 if ( ! empty( $exclusions ) ) {
1875 $where .= $exclusions;
1878 if ( ! empty( $args['name'] ) ) {
1879 $names = (array) $args['name'];
1880 foreach ( $names as &$_name ) {
1881 $_name = sanitize_term_field( 'name', $_name, 0, reset( $taxonomies ), 'db' );
1884 $where .= " AND t.name IN ('" . implode( "', '", array_map( 'esc_sql', $names ) ) . "')";
1887 if ( ! empty( $args['slug'] ) ) {
1888 if ( is_array( $args['slug'] ) ) {
1889 $slug = array_map( 'sanitize_title', $args['slug'] );
1890 $where .= " AND t.slug IN ('" . implode( "', '", $slug ) . "')";
1892 $slug = sanitize_title( $args['slug'] );
1893 $where .= " AND t.slug = '$slug'";
1897 if ( ! empty( $args['name__like'] ) ) {
1898 $where .= $wpdb->prepare( " AND t.name LIKE %s", '%' . $wpdb->esc_like( $args['name__like'] ) . '%' );
1901 if ( ! empty( $args['description__like'] ) ) {
1902 $where .= $wpdb->prepare( " AND tt.description LIKE %s", '%' . $wpdb->esc_like( $args['description__like'] ) . '%' );
1905 if ( '' !== $parent ) {
1906 $parent = (int) $parent;
1907 $where .= " AND tt.parent = '$parent'";
1910 $hierarchical = $args['hierarchical'];
1911 if ( 'count' == $args['fields'] ) {
1912 $hierarchical = false;
1914 if ( $args['hide_empty'] && !$hierarchical ) {
1915 $where .= ' AND tt.count > 0';
1918 $number = $args['number'];
1919 $offset = $args['offset'];
1921 // Don't limit the query results when we have to descend the family tree.
1922 if ( $number && ! $hierarchical && ! $child_of && '' === $parent ) {
1924 $limits = 'LIMIT ' . $offset . ',' . $number;
1926 $limits = 'LIMIT ' . $number;
1932 if ( ! empty( $args['search'] ) ) {
1933 $like = '%' . $wpdb->esc_like( $args['search'] ) . '%';
1934 $where .= $wpdb->prepare( ' AND ((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like );
1938 switch ( $args['fields'] ) {
1940 $selects = array( 't.*', 'tt.*' );
1944 $selects = array( 't.term_id', 'tt.parent', 'tt.count', 'tt.taxonomy' );
1947 $selects = array( 't.term_id', 'tt.parent', 'tt.count', 't.name', 'tt.taxonomy' );
1952 $selects = array( 'COUNT(*)' );
1955 $selects = array( 't.term_id', 't.name', 'tt.count', 'tt.taxonomy' );
1958 $selects = array( 't.term_id', 't.slug', 'tt.count', 'tt.taxonomy' );
1962 $_fields = $args['fields'];
1965 * Filter the fields to select in the terms query.
1967 * Field lists modified using this filter will only modify the term fields returned
1968 * by the function when the `$fields` parameter set to 'count' or 'all'. In all other
1969 * cases, the term fields in the results array will be determined by the `$fields`
1972 * Use of this filter can result in unpredictable behavior, and is not recommended.
1976 * @param array $selects An array of fields to select for the terms query.
1977 * @param array $args An array of term query arguments.
1978 * @param array $taxonomies An array of taxonomies.
1980 $fields = implode( ', ', apply_filters( 'get_terms_fields', $selects, $args, $taxonomies ) );
1982 $join = "INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
1984 $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits' );
1987 * Filter the terms query SQL clauses.
1991 * @param array $pieces Terms query SQL clauses.
1992 * @param array $taxonomies An array of taxonomies.
1993 * @param array $args An array of terms query arguments.
1995 $clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args );
1997 $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';
1998 $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';
1999 $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';
2000 $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';
2001 $order = isset( $clauses[ 'order' ] ) ? $clauses[ 'order' ] : '';
2002 $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';
2004 $query = "SELECT $fields FROM $wpdb->terms AS t $join WHERE $where $orderby $order $limits";
2006 if ( 'count' == $_fields ) {
2007 return $wpdb->get_var( $query );
2010 $terms = $wpdb->get_results($query);
2011 if ( 'all' == $_fields ) {
2012 update_term_cache( $terms );
2015 if ( empty($terms) ) {
2016 wp_cache_add( $cache_key, array(), 'terms', DAY_IN_SECONDS );
2018 /** This filter is documented in wp-includes/taxonomy.php */
2019 return apply_filters( 'get_terms', array(), $taxonomies, $args );
2023 foreach ( $taxonomies as $_tax ) {
2024 $children = _get_term_hierarchy( $_tax );
2025 if ( ! empty( $children ) ) {
2026 $terms = _get_term_children( $child_of, $terms, $_tax );
2031 // Update term counts to include children.
2032 if ( $args['pad_counts'] && 'all' == $_fields ) {
2033 foreach ( $taxonomies as $_tax ) {
2034 _pad_term_counts( $terms, $_tax );
2038 // Make sure we show empty categories that have children.
2039 if ( $hierarchical && $args['hide_empty'] && is_array( $terms ) ) {
2040 foreach ( $terms as $k => $term ) {
2041 if ( ! $term->count ) {
2042 $children = get_term_children( $term->term_id, $term->taxonomy );
2043 if ( is_array( $children ) ) {
2044 foreach ( $children as $child_id ) {
2045 $child = get_term( $child_id, $term->taxonomy );
2046 if ( $child->count ) {
2052 // It really is empty.
2059 if ( 'id=>parent' == $_fields ) {
2060 foreach ( $terms as $term ) {
2061 $_terms[ $term->term_id ] = $term->parent;
2063 } elseif ( 'ids' == $_fields ) {
2064 foreach ( $terms as $term ) {
2065 $_terms[] = $term->term_id;
2067 } elseif ( 'names' == $_fields ) {
2068 foreach ( $terms as $term ) {
2069 $_terms[] = $term->name;
2071 } elseif ( 'id=>name' == $_fields ) {
2072 foreach ( $terms as $term ) {
2073 $_terms[ $term->term_id ] = $term->name;
2075 } elseif ( 'id=>slug' == $_fields ) {
2076 foreach ( $terms as $term ) {
2077 $_terms[ $term->term_id ] = $term->slug;
2081 if ( ! empty( $_terms ) ) {
2085 if ( $number && is_array( $terms ) && count( $terms ) > $number ) {
2086 $terms = array_slice( $terms, $offset, $number );
2089 wp_cache_add( $cache_key, $terms, 'terms', DAY_IN_SECONDS );
2091 /** This filter is documented in wp-includes/taxonomy */
2092 return apply_filters( 'get_terms', $terms, $taxonomies, $args );
2096 * Check if Term exists.
2098 * Formerly is_term(), introduced in 2.3.0.
2102 * @global wpdb $wpdb WordPress database abstraction object.
2104 * @param int|string $term The term to check
2105 * @param string $taxonomy The taxonomy name to use
2106 * @param int $parent Optional. ID of parent term under which to confine the exists search.
2107 * @return mixed Returns null if the term does not exist. Returns the term ID
2108 * if no taxonomy is specified and the term ID exists. Returns
2109 * an array of the term ID and the term taxonomy ID the taxonomy
2110 * is specified and the pairing exists.
2112 function term_exists( $term, $taxonomy = '', $parent = null ) {
2115 $select = "SELECT term_id FROM $wpdb->terms as t WHERE ";
2116 $tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE ";
2118 if ( is_int($term) ) {
2121 $where = 't.term_id = %d';
2122 if ( !empty($taxonomy) )
2123 return $wpdb->get_row( $wpdb->prepare( $tax_select . $where . " AND tt.taxonomy = %s", $term, $taxonomy ), ARRAY_A );
2125 return $wpdb->get_var( $wpdb->prepare( $select . $where, $term ) );
2128 $term = trim( wp_unslash( $term ) );
2129 $slug = sanitize_title( $term );
2131 $where = 't.slug = %s';
2132 $else_where = 't.name = %s';
2133 $where_fields = array($slug);
2134 $else_where_fields = array($term);
2135 $orderby = 'ORDER BY t.term_id ASC';
2137 if ( !empty($taxonomy) ) {
2138 if ( is_numeric( $parent ) ) {
2139 $parent = (int) $parent;
2140 $where_fields[] = $parent;
2141 $else_where_fields[] = $parent;
2142 $where .= ' AND tt.parent = %d';
2143 $else_where .= ' AND tt.parent = %d';
2146 $where_fields[] = $taxonomy;
2147 $else_where_fields[] = $taxonomy;
2149 if ( $result = $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = %s $orderby $limit", $where_fields), ARRAY_A) )
2152 return $wpdb->get_row( $wpdb->prepare("SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $else_where AND tt.taxonomy = %s $orderby $limit", $else_where_fields), ARRAY_A);
2155 if ( $result = $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $where $orderby $limit", $where_fields) ) )
2158 return $wpdb->get_var( $wpdb->prepare("SELECT term_id FROM $wpdb->terms as t WHERE $else_where $orderby $limit", $else_where_fields) );
2162 * Check if a term is an ancestor of another term.
2164 * You can use either an id or the term object for both parameters.
2168 * @param int|object $term1 ID or object to check if this is the parent term.
2169 * @param int|object $term2 The child term.
2170 * @param string $taxonomy Taxonomy name that $term1 and `$term2` belong to.
2171 * @return bool Whether `$term2` is a child of `$term1`.
2173 function term_is_ancestor_of( $term1, $term2, $taxonomy ) {
2174 if ( ! isset( $term1->term_id ) )
2175 $term1 = get_term( $term1, $taxonomy );
2176 if ( ! isset( $term2->parent ) )
2177 $term2 = get_term( $term2, $taxonomy );
2179 if ( empty( $term1->term_id ) || empty( $term2->parent ) )
2181 if ( $term2->parent == $term1->term_id )
2184 return term_is_ancestor_of( $term1, get_term( $term2->parent, $taxonomy ), $taxonomy );
2188 * Sanitize Term all fields.
2190 * Relies on sanitize_term_field() to sanitize the term. The difference is that
2191 * this function will sanitize <strong>all</strong> fields. The context is based
2192 * on sanitize_term_field().
2194 * The $term is expected to be either an array or an object.
2198 * @param array|object $term The term to check.
2199 * @param string $taxonomy The taxonomy name to use.
2200 * @param string $context Optional. Context in which to sanitize the term. Accepts 'edit', 'db',
2201 * 'display', 'attribute', or 'js'. Default 'display'.
2202 * @return array|object Term with all fields sanitized.
2204 function sanitize_term($term, $taxonomy, $context = 'display') {
2205 $fields = array( 'term_id', 'name', 'description', 'slug', 'count', 'parent', 'term_group', 'term_taxonomy_id', 'object_id' );
2207 $do_object = is_object( $term );
2209 $term_id = $do_object ? $term->term_id : (isset($term['term_id']) ? $term['term_id'] : 0);
2211 foreach ( (array) $fields as $field ) {
2213 if ( isset($term->$field) )
2214 $term->$field = sanitize_term_field($field, $term->$field, $term_id, $taxonomy, $context);
2216 if ( isset($term[$field]) )
2217 $term[$field] = sanitize_term_field($field, $term[$field], $term_id, $taxonomy, $context);
2222 $term->filter = $context;
2224 $term['filter'] = $context;
2230 * Cleanse the field value in the term based on the context.
2232 * Passing a term field value through the function should be assumed to have
2233 * cleansed the value for whatever context the term field is going to be used.
2235 * If no context or an unsupported context is given, then default filters will
2238 * There are enough filters for each context to support a custom filtering
2239 * without creating your own filter function. Simply create a function that
2240 * hooks into the filter you need.
2244 * @param string $field Term field to sanitize.
2245 * @param string $value Search for this term value.
2246 * @param int $term_id Term ID.
2247 * @param string $taxonomy Taxonomy Name.
2248 * @param string $context Context in which to sanitize the term field. Accepts 'edit', 'db', 'display',
2249 * 'attribute', or 'js'.
2250 * @return mixed Sanitized field.
2252 function sanitize_term_field($field, $value, $term_id, $taxonomy, $context) {
2253 $int_fields = array( 'parent', 'term_id', 'count', 'term_group', 'term_taxonomy_id', 'object_id' );
2254 if ( in_array( $field, $int_fields ) ) {
2255 $value = (int) $value;
2260 if ( 'raw' == $context )
2263 if ( 'edit' == $context ) {
2266 * Filter a term field to edit before it is sanitized.
2268 * The dynamic portion of the filter name, `$field`, refers to the term field.
2272 * @param mixed $value Value of the term field.
2273 * @param int $term_id Term ID.
2274 * @param string $taxonomy Taxonomy slug.
2276 $value = apply_filters( "edit_term_{$field}", $value, $term_id, $taxonomy );
2279 * Filter the taxonomy field to edit before it is sanitized.
2281 * The dynamic portions of the filter name, `$taxonomy` and `$field`, refer
2282 * to the taxonomy slug and taxonomy field, respectively.
2286 * @param mixed $value Value of the taxonomy field to edit.
2287 * @param int $term_id Term ID.
2289 $value = apply_filters( "edit_{$taxonomy}_{$field}", $value, $term_id );
2291 if ( 'description' == $field )
2292 $value = esc_html($value); // textarea_escaped
2294 $value = esc_attr($value);
2295 } elseif ( 'db' == $context ) {
2298 * Filter a term field value before it is sanitized.
2300 * The dynamic portion of the filter name, `$field`, refers to the term field.
2304 * @param mixed $value Value of the term field.
2305 * @param string $taxonomy Taxonomy slug.
2307 $value = apply_filters( "pre_term_{$field}", $value, $taxonomy );
2310 * Filter a taxonomy field before it is sanitized.
2312 * The dynamic portions of the filter name, `$taxonomy` and `$field`, refer
2313 * to the taxonomy slug and field name, respectively.
2317 * @param mixed $value Value of the taxonomy field.
2319 $value = apply_filters( "pre_{$taxonomy}_{$field}", $value );
2321 // Back compat filters
2322 if ( 'slug' == $field ) {
2324 * Filter the category nicename before it is sanitized.
2326 * Use the pre_{$taxonomy}_{$field} hook instead.
2330 * @param string $value The category nicename.
2332 $value = apply_filters( 'pre_category_nicename', $value );
2335 } elseif ( 'rss' == $context ) {
2338 * Filter the term field for use in RSS.
2340 * The dynamic portion of the filter name, `$field`, refers to the term field.
2344 * @param mixed $value Value of the term field.
2345 * @param string $taxonomy Taxonomy slug.
2347 $value = apply_filters( "term_{$field}_rss", $value, $taxonomy );
2350 * Filter the taxonomy field for use in RSS.
2352 * The dynamic portions of the hook name, `$taxonomy`, and `$field`, refer
2353 * to the taxonomy slug and field name, respectively.
2357 * @param mixed $value Value of the taxonomy field.
2359 $value = apply_filters( "{$taxonomy}_{$field}_rss", $value );
2361 // Use display filters by default.
2364 * Filter the term field sanitized for display.
2366 * The dynamic portion of the filter name, `$field`, refers to the term field name.
2370 * @param mixed $value Value of the term field.
2371 * @param int $term_id Term ID.
2372 * @param string $taxonomy Taxonomy slug.
2373 * @param string $context Context to retrieve the term field value.
2375 $value = apply_filters( "term_{$field}", $value, $term_id, $taxonomy, $context );
2378 * Filter the taxonomy field sanitized for display.
2380 * The dynamic portions of the filter name, `$taxonomy`, and `$field`, refer
2381 * to the taxonomy slug and taxonomy field, respectively.
2385 * @param mixed $value Value of the taxonomy field.
2386 * @param int $term_id Term ID.
2387 * @param string $context Context to retrieve the taxonomy field value.
2389 $value = apply_filters( "{$taxonomy}_{$field}", $value, $term_id, $context );
2392 if ( 'attribute' == $context ) {
2393 $value = esc_attr($value);
2394 } elseif ( 'js' == $context ) {
2395 $value = esc_js($value);
2401 * Count how many terms are in Taxonomy.
2403 * Default $args is 'hide_empty' which can be 'hide_empty=true' or array('hide_empty' => true).
2405 * @todo Document $args as a hash notation.
2409 * @param string $taxonomy Taxonomy name
2410 * @param array|string $args Overwrite defaults. See get_terms()
2411 * @return array|int|WP_Error How many terms are in $taxonomy. WP_Error if $taxonomy does not exist.
2413 function wp_count_terms( $taxonomy, $args = array() ) {
2414 $defaults = array('hide_empty' => false);
2415 $args = wp_parse_args($args, $defaults);
2417 // backwards compatibility
2418 if ( isset($args['ignore_empty']) ) {
2419 $args['hide_empty'] = $args['ignore_empty'];
2420 unset($args['ignore_empty']);
2423 $args['fields'] = 'count';
2425 return get_terms($taxonomy, $args);
2429 * Will unlink the object from the taxonomy or taxonomies.
2431 * Will remove all relationships between the object and any terms in
2432 * a particular taxonomy or taxonomies. Does not remove the term or
2437 * @param int $object_id The term Object Id that refers to the term.
2438 * @param string|array $taxonomies List of Taxonomy Names or single Taxonomy name.
2440 function wp_delete_object_term_relationships( $object_id, $taxonomies ) {
2441 $object_id = (int) $object_id;
2443 if ( !is_array($taxonomies) )
2444 $taxonomies = array($taxonomies);
2446 foreach ( (array) $taxonomies as $taxonomy ) {
2447 $term_ids = wp_get_object_terms( $object_id, $taxonomy, array( 'fields' => 'ids' ) );
2448 $term_ids = array_map( 'intval', $term_ids );
2449 wp_remove_object_terms( $object_id, $term_ids, $taxonomy );
2454 * Removes a term from the database.
2456 * If the term is a parent of other terms, then the children will be updated to
2457 * that term's parent.
2459 * The `$args` 'default' will only override the terms found, if there is only one
2460 * term found. Any other and the found terms are used.
2462 * The $args 'force_default' will force the term supplied as default to be
2463 * assigned even if the object was not going to be termless
2465 * @todo Document $args as a hash notation.
2469 * @global wpdb $wpdb WordPress database abstraction object.
2471 * @param int $term Term ID.
2472 * @param string $taxonomy Taxonomy Name.
2473 * @param array|string $args Optional. Change 'default' term id and override found term ids.
2474 * @return bool|int|WP_Error Returns false if not term; true if completes delete action.
2476 function wp_delete_term( $term, $taxonomy, $args = array() ) {
2479 $term = (int) $term;
2481 if ( ! $ids = term_exists($term, $taxonomy) )
2483 if ( is_wp_error( $ids ) )
2486 $tt_id = $ids['term_taxonomy_id'];
2488 $defaults = array();
2490 if ( 'category' == $taxonomy ) {
2491 $defaults['default'] = get_option( 'default_category' );
2492 if ( $defaults['default'] == $term )
2493 return 0; // Don't delete the default category
2496 $args = wp_parse_args($args, $defaults);
2498 if ( isset( $args['default'] ) ) {
2499 $default = (int) $args['default'];
2500 if ( ! term_exists( $default, $taxonomy ) ) {
2505 if ( isset( $args['force_default'] ) ) {
2506 $force_default = $args['force_default'];
2510 * Fires when deleting a term, before any modifications are made to posts or terms.
2514 * @param int $term Term ID.
2515 * @param string $taxonomy Taxonomy Name.
2517 do_action( 'pre_delete_term', $term, $taxonomy );
2519 // Update children to point to new parent
2520 if ( is_taxonomy_hierarchical($taxonomy) ) {
2521 $term_obj = get_term($term, $taxonomy);
2522 if ( is_wp_error( $term_obj ) )
2524 $parent = $term_obj->parent;
2526 $edit_ids = $wpdb->get_results( "SELECT term_id, term_taxonomy_id FROM $wpdb->term_taxonomy WHERE `parent` = " . (int)$term_obj->term_id );
2527 $edit_tt_ids = wp_list_pluck( $edit_ids, 'term_taxonomy_id' );
2530 * Fires immediately before a term to delete's children are reassigned a parent.
2534 * @param array $edit_tt_ids An array of term taxonomy IDs for the given term.
2536 do_action( 'edit_term_taxonomies', $edit_tt_ids );
2538 $wpdb->update( $wpdb->term_taxonomy, compact( 'parent' ), array( 'parent' => $term_obj->term_id) + compact( 'taxonomy' ) );
2540 // Clean the cache for all child terms.
2541 $edit_term_ids = wp_list_pluck( $edit_ids, 'term_id' );
2542 clean_term_cache( $edit_term_ids, $taxonomy );
2545 * Fires immediately after a term to delete's children are reassigned a parent.
2549 * @param array $edit_tt_ids An array of term taxonomy IDs for the given term.
2551 do_action( 'edited_term_taxonomies', $edit_tt_ids );
2554 $objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) );
2556 foreach ( (array) $objects as $object ) {
2557 $terms = wp_get_object_terms($object, $taxonomy, array('fields' => 'ids', 'orderby' => 'none'));
2558 if ( 1 == count($terms) && isset($default) ) {
2559 $terms = array($default);
2561 $terms = array_diff($terms, array($term));
2562 if (isset($default) && isset($force_default) && $force_default)
2563 $terms = array_merge($terms, array($default));
2565 $terms = array_map('intval', $terms);
2566 wp_set_object_terms($object, $terms, $taxonomy);
2569 // Clean the relationship caches for all object types using this term.
2570 $tax_object = get_taxonomy( $taxonomy );
2571 foreach ( $tax_object->object_type as $object_type )
2572 clean_object_term_cache( $objects, $object_type );
2574 // Get the object before deletion so we can pass to actions below
2575 $deleted_term = get_term( $term, $taxonomy );
2578 * Fires immediately before a term taxonomy ID is deleted.
2582 * @param int $tt_id Term taxonomy ID.
2584 do_action( 'delete_term_taxonomy', $tt_id );
2585 $wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) );
2588 * Fires immediately after a term taxonomy ID is deleted.
2592 * @param int $tt_id Term taxonomy ID.
2594 do_action( 'deleted_term_taxonomy', $tt_id );
2596 // Delete the term if no taxonomies use it.
2597 if ( !$wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy WHERE term_id = %d", $term) ) )
2598 $wpdb->delete( $wpdb->terms, array( 'term_id' => $term ) );
2600 clean_term_cache($term, $taxonomy);
2603 * Fires after a term is deleted from the database and the cache is cleaned.
2607 * @param int $term Term ID.
2608 * @param int $tt_id Term taxonomy ID.
2609 * @param string $taxonomy Taxonomy slug.
2610 * @param mixed $deleted_term Copy of the already-deleted term, in the form specified
2611 * by the parent function. WP_Error otherwise.
2613 do_action( 'delete_term', $term, $tt_id, $taxonomy, $deleted_term );
2616 * Fires after a term in a specific taxonomy is deleted.
2618 * The dynamic portion of the hook name, `$taxonomy`, refers to the specific
2619 * taxonomy the term belonged to.
2623 * @param int $term Term ID.
2624 * @param int $tt_id Term taxonomy ID.
2625 * @param mixed $deleted_term Copy of the already-deleted term, in the form specified
2626 * by the parent function. WP_Error otherwise.
2628 do_action( "delete_$taxonomy", $term, $tt_id, $deleted_term );
2634 * Deletes one existing category.
2638 * @param int $cat_ID
2639 * @return bool|int|WP_Error Returns true if completes delete action; false if term doesn't exist;
2640 * Zero on attempted deletion of default Category; WP_Error object is also a possibility.
2642 function wp_delete_category( $cat_ID ) {
2643 return wp_delete_term( $cat_ID, 'category' );
2647 * Retrieves the terms associated with the given object(s), in the supplied taxonomies.
2650 * @since 4.2.0 Added support for 'taxonomy', 'parent', and 'term_taxonomy_id' values of `$orderby`.
2651 * Introduced `$parent` argument.
2653 * @global wpdb $wpdb WordPress database abstraction object.
2655 * @param int|array $object_ids The ID(s) of the object(s) to retrieve.
2656 * @param string|array $taxonomies The taxonomies to retrieve terms from.
2657 * @param array|string $args {
2658 * Array of arguments.
2659 * @type string $orderby Field by which results should be sorted. Accepts 'name', 'count', 'slug', 'term_group',
2660 * 'term_order', 'taxonomy', 'parent', or 'term_taxonomy_id'. Default 'name'.
2661 * @type string $order Sort order. Accepts 'ASC' or 'DESC'. Default 'ASC'.
2662 * @type string $fields Fields to return for matched terms. Accepts 'all', 'ids', 'names', and
2663 * 'all_with_object_id'. Note that 'all' or 'all_with_object_id' will result in an array of
2664 * term objects being returned, 'ids' will return an array of integers, and 'names' an array
2666 * @type int $parent Optional. Limit results to the direct children of a given term ID.
2668 * @return array|WP_Error The requested term data or empty array if no terms found.
2669 * WP_Error if any of the $taxonomies don't exist.
2671 function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {
2674 if ( empty( $object_ids ) || empty( $taxonomies ) )
2677 if ( !is_array($taxonomies) )
2678 $taxonomies = array($taxonomies);
2680 foreach ( $taxonomies as $taxonomy ) {
2681 if ( ! taxonomy_exists($taxonomy) )
2682 return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
2685 if ( !is_array($object_ids) )
2686 $object_ids = array($object_ids);
2687 $object_ids = array_map('intval', $object_ids);
2690 'orderby' => 'name',
2695 $args = wp_parse_args( $args, $defaults );
2698 if ( count($taxonomies) > 1 ) {
2699 foreach ( $taxonomies as $index => $taxonomy ) {
2700 $t = get_taxonomy($taxonomy);
2701 if ( isset($t->args) && is_array($t->args) && $args != array_merge($args, $t->args) ) {
2702 unset($taxonomies[$index]);
2703 $terms = array_merge($terms, wp_get_object_terms($object_ids, $taxonomy, array_merge($args, $t->args)));
2707 $t = get_taxonomy($taxonomies[0]);
2708 if ( isset($t->args) && is_array($t->args) )
2709 $args = array_merge($args, $t->args);
2712 $orderby = $args['orderby'];
2713 $order = $args['order'];
2714 $fields = $args['fields'];
2716 if ( in_array( $orderby, array( 'term_id', 'name', 'slug', 'term_group' ) ) ) {
2717 $orderby = "t.$orderby";
2718 } elseif ( in_array( $orderby, array( 'count', 'parent', 'taxonomy', 'term_taxonomy_id' ) ) ) {
2719 $orderby = "tt.$orderby";
2720 } elseif ( 'term_order' === $orderby ) {
2721 $orderby = 'tr.term_order';
2722 } elseif ( 'none' === $orderby ) {
2726 $orderby = 't.term_id';
2729 // tt_ids queries can only be none or tr.term_taxonomy_id
2730 if ( ('tt_ids' == $fields) && !empty($orderby) )
2731 $orderby = 'tr.term_taxonomy_id';
2733 if ( !empty($orderby) )
2734 $orderby = "ORDER BY $orderby";
2736 $order = strtoupper( $order );
2737 if ( '' !== $order && ! in_array( $order, array( 'ASC', 'DESC' ) ) )
2740 $taxonomy_array = $taxonomies;
2741 $object_id_array = $object_ids;
2742 $taxonomies = "'" . implode("', '", $taxonomies) . "'";
2743 $object_ids = implode(', ', $object_ids);
2746 if ( 'all' == $fields ) {
2747 $select_this = 't.*, tt.*';
2748 } elseif ( 'ids' == $fields ) {
2749 $select_this = 't.term_id';
2750 } elseif ( 'names' == $fields ) {
2751 $select_this = 't.name';
2752 } elseif ( 'slugs' == $fields ) {
2753 $select_this = 't.slug';
2754 } elseif ( 'all_with_object_id' == $fields ) {
2755 $select_this = 't.*, tt.*, tr.object_id';
2759 "tt.taxonomy IN ($taxonomies)",
2760 "tr.object_id IN ($object_ids)",
2763 if ( '' !== $args['parent'] ) {
2764 $where[] = $wpdb->prepare( 'tt.parent = %d', $args['parent'] );
2767 $where = implode( ' AND ', $where );
2769 $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";
2772 if ( 'all' == $fields || 'all_with_object_id' == $fields ) {
2773 $_terms = $wpdb->get_results( $query );
2774 foreach ( $_terms as $key => $term ) {
2775 $_terms[$key] = sanitize_term( $term, $taxonomy, 'raw' );
2777 $terms = array_merge( $terms, $_terms );
2778 update_term_cache( $terms );
2780 } elseif ( 'ids' == $fields || 'names' == $fields || 'slugs' == $fields ) {
2781 $_terms = $wpdb->get_col( $query );
2782 $_field = ( 'ids' == $fields ) ? 'term_id' : 'name';
2783 foreach ( $_terms as $key => $term ) {
2784 $_terms[$key] = sanitize_term_field( $_field, $term, $term, $taxonomy, 'raw' );
2786 $terms = array_merge( $terms, $_terms );
2787 } elseif ( 'tt_ids' == $fields ) {
2788 $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");
2789 foreach ( $terms as $key => $tt_id ) {
2790 $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.
2796 } elseif ( $objects && 'all_with_object_id' !== $fields ) {
2799 foreach ( $terms as $term ) {
2800 if ( in_array( $term->term_taxonomy_id, $_tt_ids ) ) {
2804 $_tt_ids[] = $term->term_taxonomy_id;
2808 } elseif ( ! $objects ) {
2809 $terms = array_values( array_unique( $terms ) );
2813 * Filter the terms for a given object or objects.
2817 * @param array $terms An array of terms for the given object or objects.
2818 * @param array $object_id_array Array of object IDs for which `$terms` were retrieved.
2819 * @param array $taxonomy_array Array of taxonomies from which `$terms` were retrieved.
2820 * @param array $args An array of arguments for retrieving terms for the given
2821 * object(s). See wp_get_object_terms() for details.
2823 $terms = apply_filters( 'get_object_terms', $terms, $object_id_array, $taxonomy_array, $args );
2826 * Filter the terms for a given object or objects.
2828 * The `$taxonomies` parameter passed to this filter is formatted as a SQL fragment. The
2829 * {@see 'get_object_terms'} filter is recommended as an alternative.
2833 * @param array $terms An array of terms for the given object or objects.
2834 * @param int|array $object_ids Object ID or array of IDs.
2835 * @param string $taxonomies SQL-formatted (comma-separated and quoted) list of taxonomy names.
2836 * @param array $args An array of arguments for retrieving terms for the given object(s).
2837 * See {@see wp_get_object_terms()} for details.
2839 return apply_filters( 'wp_get_object_terms', $terms, $object_ids, $taxonomies, $args );
2843 * Add a new term to the database.
2845 * A non-existent term is inserted in the following sequence:
2846 * 1. The term is added to the term table, then related to the taxonomy.
2847 * 2. If everything is correct, several actions are fired.
2848 * 3. The 'term_id_filter' is evaluated.
2849 * 4. The term cache is cleaned.
2850 * 5. Several more actions are fired.
2851 * 6. An array is returned containing the term_id and term_taxonomy_id.
2853 * If the 'slug' argument is not empty, then it is checked to see if the term
2854 * is invalid. If it is not a valid, existing term, it is added and the term_id
2857 * If the taxonomy is hierarchical, and the 'parent' argument is not empty,
2858 * the term is inserted and the term_id will be given.
2861 * If $taxonomy does not exist or $term is empty,
2862 * a WP_Error object will be returned.
2864 * If the term already exists on the same hierarchical level,
2865 * or the term slug and name are not unique, a WP_Error object will be returned.
2867 * @global wpdb $wpdb WordPress database abstraction object.
2871 * @param string $term The term to add or update.
2872 * @param string $taxonomy The taxonomy to which to add the term.
2873 * @param array|string $args {
2874 * Optional. Array or string of arguments for inserting a term.
2876 * @type string $alias_of Slug of the term to make this term an alias of.
2877 * Default empty string. Accepts a term slug.
2878 * @type string $description The term description. Default empty string.
2879 * @type int $parent The id of the parent term. Default 0.
2880 * @type string $slug The term slug to use. Default empty string.
2882 * @return array|WP_Error An array containing the `term_id` and `term_taxonomy_id`,
2883 * {@see WP_Error} otherwise.
2885 function wp_insert_term( $term, $taxonomy, $args = array() ) {
2888 if ( ! taxonomy_exists($taxonomy) ) {
2889 return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
2892 * Filter a term before it is sanitized and inserted into the database.
2896 * @param string $term The term to add or update.
2897 * @param string $taxonomy Taxonomy slug.
2899 $term = apply_filters( 'pre_insert_term', $term, $taxonomy );
2900 if ( is_wp_error( $term ) ) {
2903 if ( is_int($term) && 0 == $term ) {
2904 return new WP_Error('invalid_term_id', __('Invalid term ID'));
2906 if ( '' == trim($term) ) {
2907 return new WP_Error('empty_term_name', __('A name is required for this term'));
2909 $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
2910 $args = wp_parse_args( $args, $defaults );
2912 if ( $args['parent'] > 0 && ! term_exists( (int) $args['parent'] ) ) {
2913 return new WP_Error( 'missing_parent', __( 'Parent term does not exist.' ) );
2915 $args['name'] = $term;
2916 $args['taxonomy'] = $taxonomy;
2917 $args = sanitize_term($args, $taxonomy, 'db');
2919 // expected_slashed ($name)
2920 $name = wp_unslash( $args['name'] );
2921 $description = wp_unslash( $args['description'] );
2922 $parent = (int) $args['parent'];
2924 $slug_provided = ! empty( $args['slug'] );
2925 if ( ! $slug_provided ) {
2926 $slug = sanitize_title( $name );
2928 $slug = $args['slug'];
2932 if ( $args['alias_of'] ) {
2933 $alias = get_term_by( 'slug', $args['alias_of'], $taxonomy );
2934 if ( ! empty( $alias->term_group ) ) {
2935 // The alias we want is already in a group, so let's use that one.
2936 $term_group = $alias->term_group;
2937 } elseif ( ! empty( $alias->term_id ) ) {
2939 * The alias is not in a group, so we create a new one
2940 * and add the alias to it.
2942 $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
2944 wp_update_term( $alias->term_id, $taxonomy, array(
2945 'term_group' => $term_group,
2951 * Prevent the creation of terms with duplicate names at the same level of a taxonomy hierarchy,
2952 * unless a unique slug has been explicitly provided.
2954 if ( $name_match = get_term_by( 'name', $name, $taxonomy ) ) {
2955 $slug_match = get_term_by( 'slug', $slug, $taxonomy );
2956 if ( ! $slug_provided || $name_match->slug === $slug || $slug_match ) {
2957 if ( is_taxonomy_hierarchical( $taxonomy ) ) {
2958 $siblings = get_terms( $taxonomy, array( 'get' => 'all', 'parent' => $parent ) );
2960 $existing_term = null;
2961 if ( $name_match->slug === $slug && in_array( $name, wp_list_pluck( $siblings, 'name' ) ) ) {
2962 $existing_term = $name_match;
2963 } elseif ( $slug_match && in_array( $slug, wp_list_pluck( $siblings, 'slug' ) ) ) {
2964 $existing_term = $slug_match;
2967 if ( $existing_term ) {
2968 return new WP_Error( 'term_exists', __( 'A term with the name provided already exists with this parent.' ), $existing_term->term_id );
2971 return new WP_Error( 'term_exists', __( 'A term with the name provided already exists in this taxonomy.' ), $name_match->term_id );
2976 $slug = wp_unique_term_slug( $slug, (object) $args );
2978 if ( false === $wpdb->insert( $wpdb->terms, compact( 'name', 'slug', 'term_group' ) ) ) {
2979 return new WP_Error( 'db_insert_error', __( 'Could not insert term into the database' ), $wpdb->last_error );
2982 $term_id = (int) $wpdb->insert_id;
2984 // Seems unreachable, However, Is used in the case that a term name is provided, which sanitizes to an empty string.
2985 if ( empty($slug) ) {
2986 $slug = sanitize_title($slug, $term_id);
2988 /** This action is documented in wp-includes/taxonomy.php */
2989 do_action( 'edit_terms', $term_id, $taxonomy );
2990 $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );
2992 /** This action is documented in wp-includes/taxonomy.php */
2993 do_action( 'edited_terms', $term_id, $taxonomy );
2996 $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 ) );
2998 if ( !empty($tt_id) ) {
2999 return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
3001 $wpdb->insert( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent') + array( 'count' => 0 ) );
3002 $tt_id = (int) $wpdb->insert_id;
3005 * Sanity check: if we just created a term with the same parent + taxonomy + slug but a higher term_id than
3006 * an existing term, then we have unwittingly created a duplicate term. Delete the dupe, and use the term_id
3007 * and term_taxonomy_id of the older term instead. Then return out of the function so that the "create" hooks
3010 $duplicate_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.term_id, tt.term_taxonomy_id FROM $wpdb->terms t INNER JOIN $wpdb->term_taxonomy tt ON ( tt.term_id = t.term_id ) WHERE t.slug = %s AND tt.parent = %d AND tt.taxonomy = %s AND t.term_id < %d AND tt.term_taxonomy_id != %d", $slug, $parent, $taxonomy, $term_id, $tt_id ) );
3011 if ( $duplicate_term ) {
3012 $wpdb->delete( $wpdb->terms, array( 'term_id' => $term_id ) );
3013 $wpdb->delete( $wpdb->term_taxonomy, array( 'term_taxonomy_id' => $tt_id ) );
3015 $term_id = (int) $duplicate_term->term_id;
3016 $tt_id = (int) $duplicate_term->term_taxonomy_id;
3018 clean_term_cache( $term_id, $taxonomy );
3019 return array( 'term_id' => $term_id, 'term_taxonomy_id' => $tt_id );
3023 * Fires immediately after a new term is created, before the term cache is cleaned.
3027 * @param int $term_id Term ID.
3028 * @param int $tt_id Term taxonomy ID.
3029 * @param string $taxonomy Taxonomy slug.
3031 do_action( "create_term", $term_id, $tt_id, $taxonomy );
3034 * Fires after a new term is created for a specific taxonomy.
3036 * The dynamic portion of the hook name, `$taxonomy`, refers
3037 * to the slug of the taxonomy the term was created for.
3041 * @param int $term_id Term ID.
3042 * @param int $tt_id Term taxonomy ID.
3044 do_action( "create_$taxonomy", $term_id, $tt_id );
3047 * Filter the term ID after a new term is created.
3051 * @param int $term_id Term ID.
3052 * @param int $tt_id Taxonomy term ID.
3054 $term_id = apply_filters( 'term_id_filter', $term_id, $tt_id );
3056 clean_term_cache($term_id, $taxonomy);
3059 * Fires after a new term is created, and after the term cache has been cleaned.
3063 * @param int $term_id Term ID.
3064 * @param int $tt_id Term taxonomy ID.
3065 * @param string $taxonomy Taxonomy slug.
3067 do_action( 'created_term', $term_id, $tt_id, $taxonomy );
3070 * Fires after a new term in a specific taxonomy is created, and after the term
3071 * cache has been cleaned.
3073 * The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug.
3077 * @param int $term_id Term ID.
3078 * @param int $tt_id Term taxonomy ID.
3080 do_action( "created_$taxonomy", $term_id, $tt_id );
3082 return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
3086 * Create Term and Taxonomy Relationships.
3088 * Relates an object (post, link etc) to a term and taxonomy type. Creates the
3089 * term and taxonomy relationship if it doesn't already exist. Creates a term if
3090 * it doesn't exist (using the slug).
3092 * A relationship means that the term is grouped in or belongs to the taxonomy.
3093 * A term has no meaning until it is given context by defining which taxonomy it
3098 * @global wpdb $wpdb The WordPress database abstraction object.
3100 * @param int $object_id The object to relate to.
3101 * @param array|int|string $terms A single term slug, single term id, or array of either term slugs or ids.
3102 * Will replace all existing related terms in this taxonomy.
3103 * @param string $taxonomy The context in which to relate the term to the object.
3104 * @param bool $append Optional. If false will delete difference of terms. Default false.
3105 * @return array|WP_Error Affected Term IDs.
3107 function wp_set_object_terms( $object_id, $terms, $taxonomy, $append = false ) {
3110 $object_id = (int) $object_id;
3112 if ( ! taxonomy_exists($taxonomy) )
3113 return new WP_Error('invalid_taxonomy', __('Invalid taxonomy'));
3115 if ( !is_array($terms) )
3116 $terms = array($terms);
3119 $old_tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids', 'orderby' => 'none'));
3121 $old_tt_ids = array();
3124 $term_ids = array();
3125 $new_tt_ids = array();
3127 foreach ( (array) $terms as $term) {
3128 if ( !strlen(trim($term)) )
3131 if ( !$term_info = term_exists($term, $taxonomy) ) {
3132 // Skip if a non-existent term ID is passed.
3133 if ( is_int($term) )
3135 $term_info = wp_insert_term($term, $taxonomy);
3137 if ( is_wp_error($term_info) )
3139 $term_ids[] = $term_info['term_id'];
3140 $tt_id = $term_info['term_taxonomy_id'];
3143 if ( $wpdb->get_var( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id = %d", $object_id, $tt_id ) ) )
3147 * Fires immediately before an object-term relationship is added.
3151 * @param int $object_id Object ID.
3152 * @param int $tt_id Term taxonomy ID.
3154 do_action( 'add_term_relationship', $object_id, $tt_id );
3155 $wpdb->insert( $wpdb->term_relationships, array( 'object_id' => $object_id, 'term_taxonomy_id' => $tt_id ) );
3158 * Fires immediately after an object-term relationship is added.
3162 * @param int $object_id Object ID.
3163 * @param int $tt_id Term taxonomy ID.
3165 do_action( 'added_term_relationship', $object_id, $tt_id );
3166 $new_tt_ids[] = $tt_id;
3170 wp_update_term_count( $new_tt_ids, $taxonomy );
3173 $delete_tt_ids = array_diff( $old_tt_ids, $tt_ids );
3175 if ( $delete_tt_ids ) {
3176 $in_delete_tt_ids = "'" . implode( "', '", $delete_tt_ids ) . "'";
3177 $delete_term_ids = $wpdb->get_col( $wpdb->prepare( "SELECT tt.term_id FROM $wpdb->term_taxonomy AS tt WHERE tt.taxonomy = %s AND tt.term_taxonomy_id IN ($in_delete_tt_ids)", $taxonomy ) );
3178 $delete_term_ids = array_map( 'intval', $delete_term_ids );
3180 $remove = wp_remove_object_terms( $object_id, $delete_term_ids, $taxonomy );
3181 if ( is_wp_error( $remove ) ) {
3187 $t = get_taxonomy($taxonomy);
3188 if ( ! $append && isset($t->sort) && $t->sort ) {
3191 $final_tt_ids = wp_get_object_terms($object_id, $taxonomy, array('fields' => 'tt_ids'));
3192 foreach ( $tt_ids as $tt_id )
3193 if ( in_array($tt_id, $final_tt_ids) )
3194 $values[] = $wpdb->prepare( "(%d, %d, %d)", $object_id, $tt_id, ++$term_order);
3196 if ( false === $wpdb->query( "INSERT INTO $wpdb->term_relationships (object_id, term_taxonomy_id, term_order) VALUES " . join( ',', $values ) . " ON DUPLICATE KEY UPDATE term_order = VALUES(term_order)" ) )
3197 return new WP_Error( 'db_insert_error', __( 'Could not insert term relationship into the database' ), $wpdb->last_error );
3200 wp_cache_delete( $object_id, $taxonomy . '_relationships' );
3203 * Fires after an object's terms have been set.
3207 * @param int $object_id Object ID.
3208 * @param array $terms An array of object terms.
3209 * @param array $tt_ids An array of term taxonomy IDs.
3210 * @param string $taxonomy Taxonomy slug.
3211 * @param bool $append Whether to append new terms to the old terms.
3212 * @param array $old_tt_ids Old array of term taxonomy IDs.
3214 do_action( 'set_object_terms', $object_id, $terms, $tt_ids, $taxonomy, $append, $old_tt_ids );
3219 * Add term(s) associated with a given object.
3223 * @param int $object_id The ID of the object to which the terms will be added.
3224 * @param array|int|string $terms The slug(s) or ID(s) of the term(s) to add.
3225 * @param array|string $taxonomy Taxonomy name.
3226 * @return array|WP_Error Affected Term IDs
3228 function wp_add_object_terms( $object_id, $terms, $taxonomy ) {
3229 return wp_set_object_terms( $object_id, $terms, $taxonomy, true );
3233 * Remove term(s) associated with a given object.
3237 * @global wpdb $wpdb WordPress database abstraction object.
3239 * @param int $object_id The ID of the object from which the terms will be removed.
3240 * @param array|int|string $terms The slug(s) or ID(s) of the term(s) to remove.
3241 * @param array|string $taxonomy Taxonomy name.
3242 * @return bool|WP_Error True on success, false or WP_Error on failure.
3244 function wp_remove_object_terms( $object_id, $terms, $taxonomy ) {
3247 $object_id = (int) $object_id;
3249 if ( ! taxonomy_exists( $taxonomy ) ) {
3250 return new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy' ) );
3253 if ( ! is_array( $terms ) ) {
3254 $terms = array( $terms );
3259 foreach ( (array) $terms as $term ) {
3260 if ( ! strlen( trim( $term ) ) ) {
3264 if ( ! $term_info = term_exists( $term, $taxonomy ) ) {
3265 // Skip if a non-existent term ID is passed.
3266 if ( is_int( $term ) ) {
3271 if ( is_wp_error( $term_info ) ) {
3275 $tt_ids[] = $term_info['term_taxonomy_id'];
3279 $in_tt_ids = "'" . implode( "', '", $tt_ids ) . "'";
3282 * Fires immediately before an object-term relationship is deleted.
3286 * @param int $object_id Object ID.
3287 * @param array $tt_ids An array of term taxonomy IDs.
3289 do_action( 'delete_term_relationships', $object_id, $tt_ids );
3290 $deleted = $wpdb->query( $wpdb->prepare( "DELETE FROM $wpdb->term_relationships WHERE object_id = %d AND term_taxonomy_id IN ($in_tt_ids)", $object_id ) );
3293 * Fires immediately after an object-term relationship is deleted.
3297 * @param int $object_id Object ID.
3298 * @param array $tt_ids An array of term taxonomy IDs.
3300 do_action( 'deleted_term_relationships', $object_id, $tt_ids );
3302 wp_update_term_count( $tt_ids, $taxonomy );
3304 return (bool) $deleted;
3311 * Will make slug unique, if it isn't already.
3313 * The `$slug` has to be unique global to every taxonomy, meaning that one
3314 * taxonomy term can't have a matching slug with another taxonomy term. Each
3315 * slug has to be globally unique for every taxonomy.
3317 * The way this works is that if the taxonomy that the term belongs to is
3318 * hierarchical and has a parent, it will append that parent to the $slug.
3320 * If that still doesn't return an unique slug, then it try to append a number
3321 * until it finds a number that is truly unique.
3323 * The only purpose for `$term` is for appending a parent, if one exists.
3327 * @global wpdb $wpdb WordPress database abstraction object.
3329 * @param string $slug The string that will be tried for a unique slug.
3330 * @param object $term The term object that the `$slug` will belong to.
3331 * @return string Will return a true unique slug.
3333 function wp_unique_term_slug( $slug, $term ) {
3336 $needs_suffix = true;
3337 $original_slug = $slug;
3339 // As of 4.1, duplicate slugs are allowed as long as they're in different taxonomies.
3340 if ( ! term_exists( $slug ) || get_option( 'db_version' ) >= 30133 && ! get_term_by( 'slug', $slug, $term->taxonomy ) ) {
3341 $needs_suffix = false;
3345 * If the taxonomy supports hierarchy and the term has a parent, make the slug unique
3346 * by incorporating parent slugs.
3348 $parent_suffix = '';
3349 if ( $needs_suffix && is_taxonomy_hierarchical( $term->taxonomy ) && ! empty( $term->parent ) ) {
3350 $the_parent = $term->parent;
3351 while ( ! empty($the_parent) ) {
3352 $parent_term = get_term($the_parent, $term->taxonomy);
3353 if ( is_wp_error($parent_term) || empty($parent_term) )
3355 $parent_suffix .= '-' . $parent_term->slug;
3356 if ( ! term_exists( $slug . $parent_suffix ) ) {
3360 if ( empty($parent_term->parent) )
3362 $the_parent = $parent_term->parent;
3366 // If we didn't get a unique slug, try appending a number to make it unique.
3369 * Filter whether the proposed unique term slug is bad.
3373 * @param bool $needs_suffix Whether the slug needs to be made unique with a suffix.
3374 * @param string $slug The slug.
3375 * @param object $term Term object.
3377 if ( apply_filters( 'wp_unique_term_slug_is_bad_slug', $needs_suffix, $slug, $term ) ) {
3378 if ( $parent_suffix ) {
3379 $slug .= $parent_suffix;
3381 if ( ! empty( $term->term_id ) )
3382 $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s AND term_id != %d", $slug, $term->term_id );
3384 $query = $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $slug );
3386 if ( $wpdb->get_var( $query ) ) {
3389 $alt_slug = $slug . "-$num";
3391 $slug_check = $wpdb->get_var( $wpdb->prepare( "SELECT slug FROM $wpdb->terms WHERE slug = %s", $alt_slug ) );
3392 } while ( $slug_check );
3399 * Filter the unique term slug.
3403 * @param string $slug Unique term slug.
3404 * @param object $term Term object.
3405 * @param string $original_slug Slug originally passed to the function for testing.
3407 return apply_filters( 'wp_unique_term_slug', $slug, $term, $original_slug );
3411 * Update term based on arguments provided.
3413 * The $args will indiscriminately override all values with the same field name.
3414 * Care must be taken to not override important information need to update or
3415 * update will fail (or perhaps create a new term, neither would be acceptable).
3417 * Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not
3418 * defined in $args already.
3420 * 'alias_of' will create a term group, if it doesn't already exist, and update
3423 * If the 'slug' argument in $args is missing, then the 'name' in $args will be
3424 * used. It should also be noted that if you set 'slug' and it isn't unique then
3425 * a WP_Error will be passed back. If you don't pass any slug, then a unique one
3426 * will be created for you.
3428 * For what can be overrode in `$args`, check the term scheme can contain and stay
3429 * away from the term keys.
3433 * @global wpdb $wpdb WordPress database abstraction object.
3435 * @param int $term_id The ID of the term
3436 * @param string $taxonomy The context in which to relate the term to the object.
3437 * @param array|string $args Optional. Array of get_terms() arguments. Default empty array.
3438 * @return array|WP_Error Returns Term ID and Taxonomy Term ID
3440 function wp_update_term( $term_id, $taxonomy, $args = array() ) {
3443 if ( ! taxonomy_exists( $taxonomy ) ) {
3444 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy' ) );
3447 $term_id = (int) $term_id;
3449 // First, get all of the original args
3450 $term = get_term( $term_id, $taxonomy, ARRAY_A );
3452 if ( is_wp_error( $term ) ) {
3457 return new WP_Error( 'invalid_term', __( 'Empty Term' ) );
3460 // Escape data pulled from DB.
3461 $term = wp_slash($term);
3463 // Merge old and new args with new args overwriting old ones.
3464 $args = array_merge($term, $args);
3466 $defaults = array( 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => '');
3467 $args = wp_parse_args($args, $defaults);
3468 $args = sanitize_term($args, $taxonomy, 'db');
3469 $parsed_args = $args;
3471 // expected_slashed ($name)
3472 $name = wp_unslash( $args['name'] );
3473 $description = wp_unslash( $args['description'] );
3475 $parsed_args['name'] = $name;
3476 $parsed_args['description'] = $description;
3478 if ( '' == trim($name) )
3479 return new WP_Error('empty_term_name', __('A name is required for this term'));
3481 if ( $parsed_args['parent'] > 0 && ! term_exists( (int) $parsed_args['parent'] ) ) {
3482 return new WP_Error( 'missing_parent', __( 'Parent term does not exist.' ) );
3485 $empty_slug = false;
3486 if ( empty( $args['slug'] ) ) {
3488 $slug = sanitize_title($name);
3490 $slug = $args['slug'];
3493 $parsed_args['slug'] = $slug;
3495 $term_group = isset( $parsed_args['term_group'] ) ? $parsed_args['term_group'] : 0;
3496 if ( $args['alias_of'] ) {
3497 $alias = get_term_by( 'slug', $args['alias_of'], $taxonomy );
3498 if ( ! empty( $alias->term_group ) ) {
3499 // The alias we want is already in a group, so let's use that one.
3500 $term_group = $alias->term_group;
3501 } elseif ( ! empty( $alias->term_id ) ) {
3503 * The alias is not in a group, so we create a new one
3504 * and add the alias to it.
3506 $term_group = $wpdb->get_var("SELECT MAX(term_group) FROM $wpdb->terms") + 1;
3508 wp_update_term( $alias->term_id, $taxonomy, array(
3509 'term_group' => $term_group,
3513 $parsed_args['term_group'] = $term_group;
3517 * Filter the term parent.
3519 * Hook to this filter to see if it will cause a hierarchy loop.
3523 * @param int $parent ID of the parent term.
3524 * @param int $term_id Term ID.
3525 * @param string $taxonomy Taxonomy slug.
3526 * @param array $parsed_args An array of potentially altered update arguments for the given term.
3527 * @param array $args An array of update arguments for the given term.
3529 $parent = apply_filters( 'wp_update_term_parent', $args['parent'], $term_id, $taxonomy, $parsed_args, $args );
3531 // Check for duplicate slug
3532 $duplicate = get_term_by( 'slug', $slug, $taxonomy );
3533 if ( $duplicate && $duplicate->term_id != $term_id ) {
3534 // If an empty slug was passed or the parent changed, reset the slug to something unique.
3536 if ( $empty_slug || ( $parent != $term['parent']) )
3537 $slug = wp_unique_term_slug($slug, (object) $args);
3539 return new WP_Error('duplicate_term_slug', sprintf(__('The slug “%s” is already in use by another term'), $slug));
3542 $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) );
3544 // Check whether this is a shared term that needs splitting.
3545 $_term_id = _split_shared_term( $term_id, $tt_id );
3546 if ( ! is_wp_error( $_term_id ) ) {
3547 $term_id = $_term_id;
3551 * Fires immediately before the given terms are edited.
3555 * @param int $term_id Term ID.
3556 * @param string $taxonomy Taxonomy slug.
3558 do_action( 'edit_terms', $term_id, $taxonomy );
3559 $wpdb->update($wpdb->terms, compact( 'name', 'slug', 'term_group' ), compact( 'term_id' ) );
3560 if ( empty($slug) ) {
3561 $slug = sanitize_title($name, $term_id);
3562 $wpdb->update( $wpdb->terms, compact( 'slug' ), compact( 'term_id' ) );
3566 * Fires immediately after the given terms are edited.
3570 * @param int $term_id Term ID
3571 * @param string $taxonomy Taxonomy slug.
3573 do_action( 'edited_terms', $term_id, $taxonomy );
3576 * Fires immediate before a term-taxonomy relationship is updated.
3580 * @param int $tt_id Term taxonomy ID.
3581 * @param string $taxonomy Taxonomy slug.
3583 do_action( 'edit_term_taxonomy', $tt_id, $taxonomy );
3585 $wpdb->update( $wpdb->term_taxonomy, compact( 'term_id', 'taxonomy', 'description', 'parent' ), array( 'term_taxonomy_id' => $tt_id ) );
3588 * Fires immediately after a term-taxonomy relationship is updated.
3592 * @param int $tt_id Term taxonomy ID.
3593 * @param string $taxonomy Taxonomy slug.
3595 do_action( 'edited_term_taxonomy', $tt_id, $taxonomy );
3597 // Clean the relationship caches for all object types using this term.
3598 $objects = $wpdb->get_col( $wpdb->prepare( "SELECT object_id FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $tt_id ) );
3599 $tax_object = get_taxonomy( $taxonomy );
3600 foreach ( $tax_object->object_type as $object_type ) {
3601 clean_object_term_cache( $objects, $object_type );
3605 * Fires after a term has been updated, but before the term cache has been cleaned.
3609 * @param int $term_id Term ID.
3610 * @param int $tt_id Term taxonomy ID.
3611 * @param string $taxonomy Taxonomy slug.
3613 do_action( "edit_term", $term_id, $tt_id, $taxonomy );
3616 * Fires after a term in a specific taxonomy has been updated, but before the term
3617 * cache has been cleaned.
3619 * The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug.
3623 * @param int $term_id Term ID.
3624 * @param int $tt_id Term taxonomy ID.
3626 do_action( "edit_$taxonomy", $term_id, $tt_id );
3628 /** This filter is documented in wp-includes/taxonomy.php */
3629 $term_id = apply_filters( 'term_id_filter', $term_id, $tt_id );
3631 clean_term_cache($term_id, $taxonomy);
3634 * Fires after a term has been updated, and the term cache has been cleaned.
3638 * @param int $term_id Term ID.
3639 * @param int $tt_id Term taxonomy ID.
3640 * @param string $taxonomy Taxonomy slug.
3642 do_action( "edited_term", $term_id, $tt_id, $taxonomy );
3645 * Fires after a term for a specific taxonomy has been updated, and the term
3646 * cache has been cleaned.
3648 * The dynamic portion of the hook name, `$taxonomy`, refers to the taxonomy slug.
3652 * @param int $term_id Term ID.
3653 * @param int $tt_id Term taxonomy ID.
3655 do_action( "edited_$taxonomy", $term_id, $tt_id );
3657 return array('term_id' => $term_id, 'term_taxonomy_id' => $tt_id);
3661 * Enable or disable term counting.
3665 * @staticvar bool $_defer
3667 * @param bool $defer Optional. Enable if true, disable if false.
3668 * @return bool Whether term counting is enabled or disabled.
3670 function wp_defer_term_counting($defer=null) {
3671 static $_defer = false;
3673 if ( is_bool($defer) ) {
3675 // flush any deferred counts
3677 wp_update_term_count( null, null, true );
3684 * Updates the amount of terms in taxonomy.
3686 * If there is a taxonomy callback applied, then it will be called for updating
3689 * The default action is to count what the amount of terms have the relationship
3690 * of term ID. Once that is done, then update the database.
3694 * @staticvar array $_deferred
3696 * @param int|array $terms The term_taxonomy_id of the terms.
3697 * @param string $taxonomy The context of the term.
3698 * @return bool If no terms will return false, and if successful will return true.
3700 function wp_update_term_count( $terms, $taxonomy, $do_deferred=false ) {
3701 static $_deferred = array();
3703 if ( $do_deferred ) {
3704 foreach ( (array) array_keys($_deferred) as $tax ) {
3705 wp_update_term_count_now( $_deferred[$tax], $tax );
3706 unset( $_deferred[$tax] );
3710 if ( empty($terms) )
3713 if ( !is_array($terms) )
3714 $terms = array($terms);
3716 if ( wp_defer_term_counting() ) {
3717 if ( !isset($_deferred[$taxonomy]) )
3718 $_deferred[$taxonomy] = array();
3719 $_deferred[$taxonomy] = array_unique( array_merge($_deferred[$taxonomy], $terms) );
3723 return wp_update_term_count_now( $terms, $taxonomy );
3727 * Perform term count update immediately.
3731 * @param array $terms The term_taxonomy_id of terms to update.
3732 * @param string $taxonomy The context of the term.
3733 * @return true Always true when complete.
3735 function wp_update_term_count_now( $terms, $taxonomy ) {
3736 $terms = array_map('intval', $terms);
3738 $taxonomy = get_taxonomy($taxonomy);
3739 if ( !empty($taxonomy->update_count_callback) ) {
3740 call_user_func($taxonomy->update_count_callback, $terms, $taxonomy);
3742 $object_types = (array) $taxonomy->object_type;
3743 foreach ( $object_types as &$object_type ) {
3744 if ( 0 === strpos( $object_type, 'attachment:' ) )
3745 list( $object_type ) = explode( ':', $object_type );
3748 if ( $object_types == array_filter( $object_types, 'post_type_exists' ) ) {
3749 // Only post types are attached to this taxonomy
3750 _update_post_term_count( $terms, $taxonomy );
3752 // Default count updater
3753 _update_generic_term_count( $terms, $taxonomy );
3757 clean_term_cache($terms, '', false);
3767 * Removes the taxonomy relationship to terms from the cache.
3769 * Will remove the entire taxonomy relationship containing term `$object_id`. The
3770 * term IDs have to exist within the taxonomy `$object_type` for the deletion to
3775 * @see get_object_taxonomies() for more on $object_type.
3777 * @param int|array $object_ids Single or list of term object ID(s).
3778 * @param array|string $object_type The taxonomy object type.
3780 function clean_object_term_cache($object_ids, $object_type) {
3781 if ( !is_array($object_ids) )
3782 $object_ids = array($object_ids);
3784 $taxonomies = get_object_taxonomies( $object_type );
3786 foreach ( $object_ids as $id ) {
3787 foreach ( $taxonomies as $taxonomy ) {
3788 wp_cache_delete($id, "{$taxonomy}_relationships");
3793 * Fires after the object term cache has been cleaned.
3797 * @param array $object_ids An array of object IDs.
3798 * @param string $objet_type Object type.
3800 do_action( 'clean_object_term_cache', $object_ids, $object_type );
3804 * Will remove all of the term ids from the cache.
3808 * @global wpdb $wpdb WordPress database abstraction object.
3809 * @global bool $_wp_suspend_cache_invalidation
3811 * @param int|array $ids Single or list of Term IDs.
3812 * @param string $taxonomy Optional. Can be empty and will assume `tt_ids`, else will use for context.
3814 * @param bool $clean_taxonomy Optional. Whether to clean taxonomy wide caches (true), or just individual
3815 * term object caches (false). Default true.
3817 function clean_term_cache($ids, $taxonomy = '', $clean_taxonomy = true) {
3818 global $wpdb, $_wp_suspend_cache_invalidation;
3820 if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
3824 if ( !is_array($ids) )
3827 $taxonomies = array();
3828 // If no taxonomy, assume tt_ids.
3829 if ( empty($taxonomy) ) {
3830 $tt_ids = array_map('intval', $ids);
3831 $tt_ids = implode(', ', $tt_ids);
3832 $terms = $wpdb->get_results("SELECT term_id, taxonomy FROM $wpdb->term_taxonomy WHERE term_taxonomy_id IN ($tt_ids)");
3834 foreach ( (array) $terms as $term ) {
3835 $taxonomies[] = $term->taxonomy;
3836 $ids[] = $term->term_id;
3837 wp_cache_delete($term->term_id, $term->taxonomy);
3839 $taxonomies = array_unique($taxonomies);
3841 $taxonomies = array($taxonomy);
3842 foreach ( $taxonomies as $taxonomy ) {
3843 foreach ( $ids as $id ) {
3844 wp_cache_delete($id, $taxonomy);
3849 foreach ( $taxonomies as $taxonomy ) {
3850 if ( $clean_taxonomy ) {
3851 wp_cache_delete('all_ids', $taxonomy);
3852 wp_cache_delete('get', $taxonomy);
3853 delete_option("{$taxonomy}_children");
3854 // Regenerate {$taxonomy}_children
3855 _get_term_hierarchy($taxonomy);
3859 * Fires once after each taxonomy's term cache has been cleaned.
3863 * @param array $ids An array of term IDs.
3864 * @param string $taxonomy Taxonomy slug.
3866 do_action( 'clean_term_cache', $ids, $taxonomy );
3869 wp_cache_set( 'last_changed', microtime(), 'terms' );
3873 * Retrieves the taxonomy relationship to the term object id.
3877 * @param int $id Term object ID.
3878 * @param string $taxonomy Taxonomy name.
3879 * @return bool|mixed Empty array if $terms found, but not `$taxonomy`. False if nothing is in cache
3880 * for `$taxonomy` and `$id`.
3882 function get_object_term_cache( $id, $taxonomy ) {
3883 return wp_cache_get( $id, "{$taxonomy}_relationships" );
3887 * Updates the cache for the given term object ID(s).
3889 * Note: Due to performance concerns, great care should be taken to only update
3890 * term caches when necessary. Processing time can increase exponentially depending
3891 * on both the number of passed term IDs and the number of taxonomies those terms
3894 * Caches will only be updated for terms not already cached.
3898 * @param string|array $object_ids Comma-separated list or array of term object IDs.
3899 * @param array|string $object_type The taxonomy object type.
3900 * @return void|false False if all of the terms in `$object_ids` are already cached.
3902 function update_object_term_cache($object_ids, $object_type) {
3903 if ( empty($object_ids) )
3906 if ( !is_array($object_ids) )
3907 $object_ids = explode(',', $object_ids);
3909 $object_ids = array_map('intval', $object_ids);
3911 $taxonomies = get_object_taxonomies($object_type);
3914 foreach ( (array) $object_ids as $id ) {
3915 foreach ( $taxonomies as $taxonomy ) {
3916 if ( false === wp_cache_get($id, "{$taxonomy}_relationships") ) {
3923 if ( empty( $ids ) )
3926 $terms = wp_get_object_terms($ids, $taxonomies, array('fields' => 'all_with_object_id'));
3928 $object_terms = array();
3929 foreach ( (array) $terms as $term )
3930 $object_terms[$term->object_id][$term->taxonomy][] = $term;
3932 foreach ( $ids as $id ) {
3933 foreach ( $taxonomies as $taxonomy ) {
3934 if ( ! isset($object_terms[$id][$taxonomy]) ) {
3935 if ( !isset($object_terms[$id]) )
3936 $object_terms[$id] = array();
3937 $object_terms[$id][$taxonomy] = array();
3942 foreach ( $object_terms as $id => $value ) {
3943 foreach ( $value as $taxonomy => $terms ) {
3944 wp_cache_add( $id, $terms, "{$taxonomy}_relationships" );
3950 * Updates Terms to Taxonomy in cache.
3954 * @param array $terms List of term objects to change.
3955 * @param string $taxonomy Optional. Update Term to this taxonomy in cache. Default empty.
3957 function update_term_cache( $terms, $taxonomy = '' ) {
3958 foreach ( (array) $terms as $term ) {
3959 $term_taxonomy = $taxonomy;
3960 if ( empty($term_taxonomy) )
3961 $term_taxonomy = $term->taxonomy;
3963 wp_cache_add( $term->term_id, $term, $term_taxonomy );
3972 * Retrieves children of taxonomy as Term IDs.
3977 * @param string $taxonomy Taxonomy name.
3978 * @return array Empty if $taxonomy isn't hierarchical or returns children as Term IDs.
3980 function _get_term_hierarchy( $taxonomy ) {
3981 if ( !is_taxonomy_hierarchical($taxonomy) )
3983 $children = get_option("{$taxonomy}_children");
3985 if ( is_array($children) )
3987 $children = array();
3988 $terms = get_terms($taxonomy, array('get' => 'all', 'orderby' => 'id', 'fields' => 'id=>parent'));
3989 foreach ( $terms as $term_id => $parent ) {
3991 $children[$parent][] = $term_id;
3993 update_option("{$taxonomy}_children", $children);
3999 * Get the subset of $terms that are descendants of $term_id.
4001 * If `$terms` is an array of objects, then _get_term_children() returns an array of objects.
4002 * If `$terms` is an array of IDs, then _get_term_children() returns an array of IDs.
4007 * @param int $term_id The ancestor term: all returned terms should be descendants of `$term_id`.
4008 * @param array $terms The set of terms - either an array of term objects or term IDs - from which those that
4009 * are descendants of $term_id will be chosen.
4010 * @param string $taxonomy The taxonomy which determines the hierarchy of the terms.
4011 * @param array $ancestors Optional. Term ancestors that have already been identified. Passed by reference, to keep
4012 * track of found terms when recursing the hierarchy. The array of located ancestors is used
4013 * to prevent infinite recursion loops. For performance, `term_ids` are used as array keys,
4014 * with 1 as value. Default empty array.
4015 * @return array|WP_Error The subset of $terms that are descendants of $term_id.
4017 function _get_term_children( $term_id, $terms, $taxonomy, &$ancestors = array() ) {
4018 $empty_array = array();
4019 if ( empty($terms) )
4020 return $empty_array;
4022 $term_list = array();
4023 $has_children = _get_term_hierarchy($taxonomy);
4025 if ( ( 0 != $term_id ) && ! isset($has_children[$term_id]) )
4026 return $empty_array;
4028 // Include the term itself in the ancestors array, so we can properly detect when a loop has occurred.
4029 if ( empty( $ancestors ) ) {
4030 $ancestors[ $term_id ] = 1;
4033 foreach ( (array) $terms as $term ) {
4035 if ( !is_object($term) ) {
4036 $term = get_term($term, $taxonomy);
4037 if ( is_wp_error( $term ) )
4042 // Don't recurse if we've already identified the term as a child - this indicates a loop.
4043 if ( isset( $ancestors[ $term->term_id ] ) ) {
4047 if ( $term->parent == $term_id ) {
4049 $term_list[] = $term->term_id;
4051 $term_list[] = $term;
4053 if ( !isset($has_children[$term->term_id]) )
4056 $ancestors[ $term->term_id ] = 1;
4058 if ( $children = _get_term_children( $term->term_id, $terms, $taxonomy, $ancestors) )
4059 $term_list = array_merge($term_list, $children);
4067 * Add count of children to parent count.
4069 * Recalculates term counts by including items from child terms. Assumes all
4070 * relevant children are already in the $terms argument.
4075 * @global wpdb $wpdb WordPress database abstraction object.
4077 * @param array $terms List of term IDs, passed by reference.
4078 * @param string $taxonomy Term context.
4080 function _pad_term_counts( &$terms, $taxonomy ) {
4083 // This function only works for hierarchical taxonomies like post categories.
4084 if ( !is_taxonomy_hierarchical( $taxonomy ) )
4087 $term_hier = _get_term_hierarchy($taxonomy);
4089 if ( empty($term_hier) )
4092 $term_items = array();
4093 $terms_by_id = array();
4094 $term_ids = array();
4096 foreach ( (array) $terms as $key => $term ) {
4097 $terms_by_id[$term->term_id] = & $terms[$key];
4098 $term_ids[$term->term_taxonomy_id] = $term->term_id;
4101 // Get the object and term ids and stick them in a lookup table.
4102 $tax_obj = get_taxonomy($taxonomy);
4103 $object_types = esc_sql($tax_obj->object_type);
4104 $results = $wpdb->get_results("SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships INNER JOIN $wpdb->posts ON object_id = ID WHERE term_taxonomy_id IN (" . implode(',', array_keys($term_ids)) . ") AND post_type IN ('" . implode("', '", $object_types) . "') AND post_status = 'publish'");
4105 foreach ( $results as $row ) {
4106 $id = $term_ids[$row->term_taxonomy_id];
4107 $term_items[$id][$row->object_id] = isset($term_items[$id][$row->object_id]) ? ++$term_items[$id][$row->object_id] : 1;
4110 // Touch every ancestor's lookup row for each post in each term.
4111 foreach ( $term_ids as $term_id ) {
4113 $ancestors = array();
4114 while ( !empty( $terms_by_id[$child] ) && $parent = $terms_by_id[$child]->parent ) {
4115 $ancestors[] = $child;
4116 if ( !empty( $term_items[$term_id] ) )
4117 foreach ( $term_items[$term_id] as $item_id => $touches ) {
4118 $term_items[$parent][$item_id] = isset($term_items[$parent][$item_id]) ? ++$term_items[$parent][$item_id]: 1;
4122 if ( in_array( $parent, $ancestors ) ) {
4128 // Transfer the touched cells.
4129 foreach ( (array) $term_items as $id => $items )
4130 if ( isset($terms_by_id[$id]) )
4131 $terms_by_id[$id]->count = count($items);
4135 // Default callbacks
4139 * Will update term count based on object types of the current taxonomy.
4141 * Private function for the default callback for post_tag and category
4147 * @global wpdb $wpdb WordPress database abstraction object.
4149 * @param array $terms List of Term taxonomy IDs.
4150 * @param object $taxonomy Current taxonomy object of terms.
4152 function _update_post_term_count( $terms, $taxonomy ) {
4155 $object_types = (array) $taxonomy->object_type;
4157 foreach ( $object_types as &$object_type )
4158 list( $object_type ) = explode( ':', $object_type );
4160 $object_types = array_unique( $object_types );
4162 if ( false !== ( $check_attachments = array_search( 'attachment', $object_types ) ) ) {
4163 unset( $object_types[ $check_attachments ] );
4164 $check_attachments = true;
4167 if ( $object_types )
4168 $object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) );
4170 foreach ( (array) $terms as $term ) {
4173 // Attachments can be 'inherit' status, we need to base count off the parent's status if so.
4174 if ( $check_attachments )
4175 $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts p1 WHERE p1.ID = $wpdb->term_relationships.object_id AND ( post_status = 'publish' OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) = 'publish' ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term ) );
4177 if ( $object_types )
4178 $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 ) );
4180 /** This action is documented in wp-includes/taxonomy.php */
4181 do_action( 'edit_term_taxonomy', $term, $taxonomy->name );
4182 $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
4184 /** This action is documented in wp-includes/taxonomy.php */
4185 do_action( 'edited_term_taxonomy', $term, $taxonomy->name );
4190 * Will update term count based on number of objects.
4192 * Default callback for the 'link_category' taxonomy.
4196 * @global wpdb $wpdb WordPress database abstraction object.
4198 * @param array $terms List of term taxonomy IDs.
4199 * @param object $taxonomy Current taxonomy object of terms.
4201 function _update_generic_term_count( $terms, $taxonomy ) {
4204 foreach ( (array) $terms as $term ) {
4205 $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) );
4207 /** This action is documented in wp-includes/taxonomy.php */
4208 do_action( 'edit_term_taxonomy', $term, $taxonomy->name );
4209 $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
4211 /** This action is documented in wp-includes/taxonomy.php */
4212 do_action( 'edited_term_taxonomy', $term, $taxonomy->name );
4217 * Create a new term for a term_taxonomy item that currently shares its term
4218 * with another term_taxonomy.
4222 * @since 4.3.0 Introduced `$record` parameter. Also, `$term_id` and
4223 * `$term_taxonomy_id` can now accept objects.
4225 * @global wpdb $wpdb
4227 * @param int|object $term_id ID of the shared term, or the shared term object.
4228 * @param int|object $term_taxonomy_id ID of the term_taxonomy item to receive a new term, or the term_taxonomy object
4229 * (corresponding to a row from the term_taxonomy table).
4230 * @param bool $record Whether to record data about the split term in the options table. The recording
4231 * process has the potential to be resource-intensive, so during batch operations
4232 * it can be beneficial to skip inline recording and do it just once, after the
4233 * batch is processed. Only set this to `false` if you know what you are doing.
4235 * @return int|WP_Error When the current term does not need to be split (or cannot be split on the current
4236 * database schema), `$term_id` is returned. When the term is successfully split, the
4237 * new term_id is returned. A WP_Error is returned for miscellaneous errors.
4239 function _split_shared_term( $term_id, $term_taxonomy_id, $record = true ) {
4242 if ( is_object( $term_id ) ) {
4243 $shared_term = $term_id;
4244 $term_id = intval( $shared_term->term_id );
4247 if ( is_object( $term_taxonomy_id ) ) {
4248 $term_taxonomy = $term_taxonomy_id;
4249 $term_taxonomy_id = intval( $term_taxonomy->term_taxonomy_id );
4252 // If there are no shared term_taxonomy rows, there's nothing to do here.
4253 $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 ) );
4255 if ( ! $shared_tt_count ) {
4260 * Verify that the term_taxonomy_id passed to the function is actually associated with the term_id.
4261 * If there's a mismatch, it may mean that the term is already split. Return the actual term_id from the db.
4263 $check_term_id = $wpdb->get_var( $wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $term_taxonomy_id ) );
4264 if ( $check_term_id != $term_id ) {
4265 return $check_term_id;
4268 // Pull up data about the currently shared slug, which we'll use to populate the new one.
4269 if ( empty( $shared_term ) ) {
4270 $shared_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.* FROM $wpdb->terms t WHERE t.term_id = %d", $term_id ) );
4273 $new_term_data = array(
4274 'name' => $shared_term->name,
4275 'slug' => $shared_term->slug,
4276 'term_group' => $shared_term->term_group,
4279 if ( false === $wpdb->insert( $wpdb->terms, $new_term_data ) ) {
4280 return new WP_Error( 'db_insert_error', __( 'Could not split shared term.' ), $wpdb->last_error );
4283 $new_term_id = (int) $wpdb->insert_id;
4285 // Update the existing term_taxonomy to point to the newly created term.
4286 $wpdb->update( $wpdb->term_taxonomy,
4287 array( 'term_id' => $new_term_id ),
4288 array( 'term_taxonomy_id' => $term_taxonomy_id )
4291 // Reassign child terms to the new parent.
4292 if ( empty( $term_taxonomy ) ) {
4293 $term_taxonomy = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $wpdb->term_taxonomy WHERE term_taxonomy_id = %d", $term_taxonomy_id ) );
4296 $children_tt_ids = $wpdb->get_col( $wpdb->prepare( "SELECT term_taxonomy_id FROM $wpdb->term_taxonomy WHERE parent = %d AND taxonomy = %s", $term_id, $term_taxonomy->taxonomy ) );
4297 if ( ! empty( $children_tt_ids ) ) {
4298 foreach ( $children_tt_ids as $child_tt_id ) {
4299 $wpdb->update( $wpdb->term_taxonomy,
4300 array( 'parent' => $new_term_id ),
4301 array( 'term_taxonomy_id' => $child_tt_id )
4303 clean_term_cache( $term_id, $term_taxonomy->taxonomy );
4306 // If the term has no children, we must force its taxonomy cache to be rebuilt separately.
4307 clean_term_cache( $new_term_id, $term_taxonomy->taxonomy );
4310 // Clean the cache for term taxonomies formerly shared with the current term.
4311 $shared_term_taxonomies = $wpdb->get_row( $wpdb->prepare( "SELECT taxonomy FROM $wpdb->term_taxonomy WHERE term_id = %d", $term_id ) );
4312 if ( $shared_term_taxonomies ) {
4313 foreach ( $shared_term_taxonomies as $shared_term_taxonomy ) {
4314 clean_term_cache( $term_id, $shared_term_taxonomy );
4318 // Keep a record of term_ids that have been split, keyed by old term_id. See {@see wp_get_split_term()}.
4320 $split_term_data = get_option( '_split_terms', array() );
4321 if ( ! isset( $split_term_data[ $term_id ] ) ) {
4322 $split_term_data[ $term_id ] = array();
4325 $split_term_data[ $term_id ][ $term_taxonomy->taxonomy ] = $new_term_id;
4326 update_option( '_split_terms', $split_term_data );
4330 * Fires after a previously shared taxonomy term is split into two separate terms.
4334 * @param int $term_id ID of the formerly shared term.
4335 * @param int $new_term_id ID of the new term created for the $term_taxonomy_id.
4336 * @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split.
4337 * @param string $taxonomy Taxonomy for the split term.
4339 do_action( 'split_shared_term', $term_id, $new_term_id, $term_taxonomy_id, $term_taxonomy->taxonomy );
4341 return $new_term_id;
4345 * Splits a batch of shared taxonomy terms.
4349 * @global wpdb $wpdb WordPress database abstraction object.
4351 function _wp_batch_split_terms() {
4354 $lock_name = 'term_split.lock';
4357 $lock_result = $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO `$wpdb->options` ( `option_name`, `option_value`, `autoload` ) VALUES (%s, %s, 'no') /* LOCK */", $lock_name, time() ) );
4359 if ( ! $lock_result ) {
4360 $lock_result = get_option( $lock_name );
4362 // Bail if we were unable to create a lock, or if the existing lock is still valid.
4363 if ( ! $lock_result || ( $lock_result > ( time() - HOUR_IN_SECONDS ) ) ) {
4364 wp_schedule_single_event( time() + ( 5 * MINUTE_IN_SECONDS ), 'wp_split_shared_term_batch' );
4369 // Update the lock, as by this point we've definitely got a lock, just need to fire the actions.
4370 update_option( $lock_name, time() );
4372 // Get a list of shared terms (those with more than one associated row in term_taxonomy).
4373 $shared_terms = $wpdb->get_results(
4374 "SELECT tt.term_id, t.*, count(*) as term_tt_count FROM {$wpdb->term_taxonomy} tt
4375 LEFT JOIN {$wpdb->terms} t ON t.term_id = tt.term_id
4377 HAVING term_tt_count > 1
4381 // No more terms, we're done here.
4382 if ( ! $shared_terms ) {
4383 update_option( 'finished_splitting_shared_terms', true );
4384 delete_option( $lock_name );
4388 // Shared terms found? We'll need to run this script again.
4389 wp_schedule_single_event( time() + ( 2 * MINUTE_IN_SECONDS ), 'wp_split_shared_term_batch' );
4391 // Rekey shared term array for faster lookups.
4392 $_shared_terms = array();
4393 foreach ( $shared_terms as $shared_term ) {
4394 $term_id = intval( $shared_term->term_id );
4395 $_shared_terms[ $term_id ] = $shared_term;
4397 $shared_terms = $_shared_terms;
4399 // Get term taxonomy data for all shared terms.
4400 $shared_term_ids = implode( ',', array_keys( $shared_terms ) );
4401 $shared_tts = $wpdb->get_results( "SELECT * FROM {$wpdb->term_taxonomy} WHERE `term_id` IN ({$shared_term_ids})" );
4403 // Split term data recording is slow, so we do it just once, outside the loop.
4404 $split_term_data = get_option( '_split_terms', array() );
4405 $skipped_first_term = $taxonomies = array();
4406 foreach ( $shared_tts as $shared_tt ) {
4407 $term_id = intval( $shared_tt->term_id );
4409 // Don't split the first tt belonging to a given term_id.
4410 if ( ! isset( $skipped_first_term[ $term_id ] ) ) {
4411 $skipped_first_term[ $term_id ] = 1;
4415 if ( ! isset( $split_term_data[ $term_id ] ) ) {
4416 $split_term_data[ $term_id ] = array();
4419 // Keep track of taxonomies whose hierarchies need flushing.
4420 if ( ! isset( $taxonomies[ $shared_tt->taxonomy ] ) ) {
4421 $taxonomies[ $shared_tt->taxonomy ] = 1;
4425 $split_term_data[ $term_id ][ $shared_tt->taxonomy ] = _split_shared_term( $shared_terms[ $term_id ], $shared_tt, false );
4428 // Rebuild the cached hierarchy for each affected taxonomy.
4429 foreach ( array_keys( $taxonomies ) as $tax ) {
4430 delete_option( "{$tax}_children" );
4431 _get_term_hierarchy( $tax );
4434 update_option( '_split_terms', $split_term_data );
4436 delete_option( $lock_name );
4440 * In order to avoid the wp_batch_split_terms() job being accidentally removed,
4441 * check that it's still scheduled while we haven't finished splitting terms.
4446 function _wp_check_for_scheduled_split_terms() {
4447 if ( ! get_option( 'finished_splitting_shared_terms' ) && ! wp_next_scheduled( 'wp_batch_split_terms' ) ) {
4448 wp_schedule_single_event( 'wp_batch_split_terms', time() + MINUTE_IN_SECONDS );
4453 * Check default categories when a term gets split to see if any of them need to be updated.
4458 * @param int $term_id ID of the formerly shared term.
4459 * @param int $new_term_id ID of the new term created for the $term_taxonomy_id.
4460 * @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split.
4461 * @param string $taxonomy Taxonomy for the split term.
4463 function _wp_check_split_default_terms( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
4464 if ( 'category' != $taxonomy ) {
4468 foreach ( array( 'default_category', 'default_link_category', 'default_email_category' ) as $option ) {
4469 if ( $term_id == get_option( $option, -1 ) ) {
4470 update_option( $option, $new_term_id );
4476 * Check menu items when a term gets split to see if any of them need to be updated.
4481 * @global wpdb $wpdb
4483 * @param int $term_id ID of the formerly shared term.
4484 * @param int $new_term_id ID of the new term created for the $term_taxonomy_id.
4485 * @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split.
4486 * @param string $taxonomy Taxonomy for the split term.
4488 function _wp_check_split_terms_in_menus( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
4490 $post_ids = $wpdb->get_col( $wpdb->prepare(
4492 FROM {$wpdb->postmeta} AS m1
4493 INNER JOIN {$wpdb->postmeta} AS m2 ON ( m2.post_id = m1.post_id )
4494 INNER JOIN {$wpdb->postmeta} AS m3 ON ( m3.post_id = m1.post_id )
4495 WHERE ( m1.meta_key = '_menu_item_type' AND m1.meta_value = 'taxonomy' )
4496 AND ( m2.meta_key = '_menu_item_object' AND m2.meta_value = '%s' )
4497 AND ( m3.meta_key = '_menu_item_object_id' AND m3.meta_value = %d )",
4503 foreach ( $post_ids as $post_id ) {
4504 update_post_meta( $post_id, '_menu_item_object_id', $new_term_id, $term_id );
4510 * If the term being split is a nav_menu, change associations.
4515 * @global wpdb $wpdb
4517 * @param int $term_id ID of the formerly shared term.
4518 * @param int $new_term_id ID of the new term created for the $term_taxonomy_id.
4519 * @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split.
4520 * @param string $taxonomy Taxonomy for the split term.
4522 function _wp_check_split_nav_menu_terms( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
4523 if ( 'nav_menu' !== $taxonomy ) {
4527 // Update menu locations.
4528 $locations = get_nav_menu_locations();
4529 foreach ( $locations as $location => $menu_id ) {
4530 if ( $term_id == $menu_id ) {
4531 $locations[ $location ] = $new_term_id;
4534 set_theme_mod( 'nav_menu_locations', $locations );
4538 * Get data about terms that previously shared a single term_id, but have since been split.
4542 * @param int $old_term_id Term ID. This is the old, pre-split term ID.
4543 * @return array Array of new term IDs, keyed by taxonomy.
4545 function wp_get_split_terms( $old_term_id ) {
4546 $split_terms = get_option( '_split_terms', array() );
4549 if ( isset( $split_terms[ $old_term_id ] ) ) {
4550 $terms = $split_terms[ $old_term_id ];
4557 * Get the new term ID corresponding to a previously split term.
4561 * @param int $old_term_id Term ID. This is the old, pre-split term ID.
4562 * @param string $taxonomy Taxonomy that the term belongs to.
4563 * @return int|false If a previously split term is found corresponding to the old term_id and taxonomy,
4564 * the new term_id will be returned. If no previously split term is found matching
4565 * the parameters, returns false.
4567 function wp_get_split_term( $old_term_id, $taxonomy ) {
4568 $split_terms = wp_get_split_terms( $old_term_id );
4571 if ( isset( $split_terms[ $taxonomy ] ) ) {
4572 $term_id = (int) $split_terms[ $taxonomy ];
4579 * Generate a permalink for a taxonomy term archive.
4583 * @global WP_Rewrite $wp_rewrite
4585 * @param object|int|string $term The term object, ID, or slug whose link will be retrieved.
4586 * @param string $taxonomy Optional. Taxonomy. Default empty.
4587 * @return string|WP_Error HTML link to taxonomy term archive on success, WP_Error if term does not exist.
4589 function get_term_link( $term, $taxonomy = '' ) {
4592 if ( !is_object($term) ) {
4593 if ( is_int( $term ) ) {
4594 $term = get_term( $term, $taxonomy );
4596 $term = get_term_by( 'slug', $term, $taxonomy );
4600 if ( !is_object($term) )
4601 $term = new WP_Error('invalid_term', __('Empty Term'));
4603 if ( is_wp_error( $term ) )
4606 $taxonomy = $term->taxonomy;
4608 $termlink = $wp_rewrite->get_extra_permastruct($taxonomy);
4610 $slug = $term->slug;
4611 $t = get_taxonomy($taxonomy);
4613 if ( empty($termlink) ) {
4614 if ( 'category' == $taxonomy )
4615 $termlink = '?cat=' . $term->term_id;
4616 elseif ( $t->query_var )
4617 $termlink = "?$t->query_var=$slug";
4619 $termlink = "?taxonomy=$taxonomy&term=$slug";
4620 $termlink = home_url($termlink);
4622 if ( $t->rewrite['hierarchical'] ) {
4623 $hierarchical_slugs = array();
4624 $ancestors = get_ancestors( $term->term_id, $taxonomy, 'taxonomy' );
4625 foreach ( (array)$ancestors as $ancestor ) {
4626 $ancestor_term = get_term($ancestor, $taxonomy);
4627 $hierarchical_slugs[] = $ancestor_term->slug;
4629 $hierarchical_slugs = array_reverse($hierarchical_slugs);
4630 $hierarchical_slugs[] = $slug;
4631 $termlink = str_replace("%$taxonomy%", implode('/', $hierarchical_slugs), $termlink);
4633 $termlink = str_replace("%$taxonomy%", $slug, $termlink);
4635 $termlink = home_url( user_trailingslashit($termlink, 'category') );
4637 // Back Compat filters.
4638 if ( 'post_tag' == $taxonomy ) {
4641 * Filter the tag link.
4644 * @deprecated 2.5.0 Use 'term_link' instead.
4646 * @param string $termlink Tag link URL.
4647 * @param int $term_id Term ID.
4649 $termlink = apply_filters( 'tag_link', $termlink, $term->term_id );
4650 } elseif ( 'category' == $taxonomy ) {
4653 * Filter the category link.
4656 * @deprecated 2.5.0 Use 'term_link' instead.
4658 * @param string $termlink Category link URL.
4659 * @param int $term_id Term ID.
4661 $termlink = apply_filters( 'category_link', $termlink, $term->term_id );
4665 * Filter the term link.
4669 * @param string $termlink Term link URL.
4670 * @param object $term Term object.
4671 * @param string $taxonomy Taxonomy slug.
4673 return apply_filters( 'term_link', $termlink, $term, $taxonomy );
4677 * Display the taxonomies of a post with available options.
4679 * This function can be used within the loop to display the taxonomies for a
4680 * post without specifying the Post ID. You can also use it outside the Loop to
4681 * display the taxonomies for a specific post.
4685 * @param array $args {
4686 * Arguments about which post to use and how to format the output. Shares all of the arguments
4687 * supported by get_the_taxonomies(), in addition to the following.
4689 * @type int|WP_Post $post Post ID or object to get taxonomies of. Default current post.
4690 * @type string $before Displays before the taxonomies. Default empty string.
4691 * @type string $sep Separates each taxonomy. Default is a space.
4692 * @type string $after Displays after the taxonomies. Default empty string.
4694 * @param array $args See {@link get_the_taxonomies()} for a description of arguments and their defaults.
4696 function the_taxonomies( $args = array() ) {
4704 $r = wp_parse_args( $args, $defaults );
4706 echo $r['before'] . join( $r['sep'], get_the_taxonomies( $r['post'], $r ) ) . $r['after'];
4710 * Retrieve all taxonomies associated with a post.
4712 * This function can be used within the loop. It will also return an array of
4713 * the taxonomies with links to the taxonomy and name.
4717 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
4718 * @param array $args {
4719 * Optional. Arguments about how to format the list of taxonomies. Default empty array.
4721 * @type string $template Template for displaying a taxonomy label and list of terms.
4722 * Default is "Label: Terms."
4723 * @type string $term_template Template for displaying a single term in the list. Default is the term name
4724 * linked to its archive.
4726 * @return array List of taxonomies.
4728 function get_the_taxonomies( $post = 0, $args = array() ) {
4729 $post = get_post( $post );
4731 $args = wp_parse_args( $args, array(
4732 /* translators: %s: taxonomy label, %l: list of terms formatted as per $term_template */
4733 'template' => __( '%s: %l.' ),
4734 'term_template' => '<a href="%1$s">%2$s</a>',
4737 $taxonomies = array();
4743 foreach ( get_object_taxonomies( $post ) as $taxonomy ) {
4744 $t = (array) get_taxonomy( $taxonomy );
4745 if ( empty( $t['label'] ) ) {
4746 $t['label'] = $taxonomy;
4748 if ( empty( $t['args'] ) ) {
4749 $t['args'] = array();
4751 if ( empty( $t['template'] ) ) {
4752 $t['template'] = $args['template'];
4754 if ( empty( $t['term_template'] ) ) {
4755 $t['term_template'] = $args['term_template'];
4758 $terms = get_object_term_cache( $post->ID, $taxonomy );
4759 if ( false === $terms ) {
4760 $terms = wp_get_object_terms( $post->ID, $taxonomy, $t['args'] );
4764 foreach ( $terms as $term ) {
4765 $links[] = wp_sprintf( $t['term_template'], esc_attr( get_term_link( $term ) ), $term->name );
4768 $taxonomies[$taxonomy] = wp_sprintf( $t['template'], $t['label'], $links, $terms );
4775 * Retrieve all taxonomies of a post with just the names.
4779 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
4782 function get_post_taxonomies( $post = 0 ) {
4783 $post = get_post( $post );
4785 return get_object_taxonomies($post);
4789 * Determine if the given object is associated with any of the given terms.
4791 * The given terms are checked against the object's terms' term_ids, names and slugs.
4792 * Terms given as integers will only be checked against the object's terms' term_ids.
4793 * If no terms are given, determines if object is associated with any terms in the given taxonomy.
4797 * @param int $object_id ID of the object (post ID, link ID, ...).
4798 * @param string $taxonomy Single taxonomy name.
4799 * @param int|string|array $terms Optional. Term term_id, name, slug or array of said. Default null.
4800 * @return bool|WP_Error WP_Error on input error.
4802 function is_object_in_term( $object_id, $taxonomy, $terms = null ) {
4803 if ( !$object_id = (int) $object_id )
4804 return new WP_Error( 'invalid_object', __( 'Invalid object ID' ) );
4806 $object_terms = get_object_term_cache( $object_id, $taxonomy );
4807 if ( false === $object_terms )
4808 $object_terms = wp_get_object_terms( $object_id, $taxonomy );
4810 if ( is_wp_error( $object_terms ) )
4811 return $object_terms;
4812 if ( empty( $object_terms ) )
4814 if ( empty( $terms ) )
4815 return ( !empty( $object_terms ) );
4817 $terms = (array) $terms;
4819 if ( $ints = array_filter( $terms, 'is_int' ) )
4820 $strs = array_diff( $terms, $ints );
4824 foreach ( $object_terms as $object_term ) {
4825 // If term is an int, check against term_ids only.
4826 if ( $ints && in_array( $object_term->term_id, $ints ) ) {
4831 // Only check numeric strings against term_id, to avoid false matches due to type juggling.
4832 $numeric_strs = array_map( 'intval', array_filter( $strs, 'is_numeric' ) );
4833 if ( in_array( $object_term->term_id, $numeric_strs, true ) ) {
4837 if ( in_array( $object_term->name, $strs ) ) return true;
4838 if ( in_array( $object_term->slug, $strs ) ) return true;
4846 * Determine if the given object type is associated with the given taxonomy.
4850 * @param string $object_type Object type string.
4851 * @param string $taxonomy Single taxonomy name.
4852 * @return bool True if object is associated with the taxonomy, otherwise false.
4854 function is_object_in_taxonomy( $object_type, $taxonomy ) {
4855 $taxonomies = get_object_taxonomies( $object_type );
4856 if ( empty( $taxonomies ) ) {
4859 return in_array( $taxonomy, $taxonomies );
4863 * Get an array of ancestor IDs for a given object.
4866 * @since 4.1.0 Introduced the `$resource_type` argument.
4868 * @param int $object_id Optional. The ID of the object. Default 0.
4869 * @param string $object_type Optional. The type of object for which we'll be retrieving
4870 * ancestors. Accepts a post type or a taxonomy name. Default empty.
4871 * @param string $resource_type Optional. Type of resource $object_type is. Accepts 'post_type'
4872 * or 'taxonomy'. Default empty.
4873 * @return array An array of ancestors from lowest to highest in the hierarchy.
4875 function get_ancestors( $object_id = 0, $object_type = '', $resource_type = '' ) {
4876 $object_id = (int) $object_id;
4878 $ancestors = array();
4880 if ( empty( $object_id ) ) {
4882 /** This filter is documented in wp-includes/taxonomy.php */
4883 return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type, $resource_type );
4886 if ( ! $resource_type ) {
4887 if ( is_taxonomy_hierarchical( $object_type ) ) {
4888 $resource_type = 'taxonomy';
4889 } elseif ( post_type_exists( $object_type ) ) {
4890 $resource_type = 'post_type';
4894 if ( 'taxonomy' === $resource_type ) {
4895 $term = get_term($object_id, $object_type);
4896 while ( ! is_wp_error($term) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) {
4897 $ancestors[] = (int) $term->parent;
4898 $term = get_term($term->parent, $object_type);
4900 } elseif ( 'post_type' === $resource_type ) {
4901 $ancestors = get_post_ancestors($object_id);
4905 * Filter a given object's ancestors.
4908 * @since 4.1.1 Introduced the `$resource_type` parameter.
4910 * @param array $ancestors An array of object ancestors.
4911 * @param int $object_id Object ID.
4912 * @param string $object_type Type of object.
4913 * @param string $resource_type Type of resource $object_type is.
4915 return apply_filters( 'get_ancestors', $ancestors, $object_id, $object_type, $resource_type );
4919 * Returns the term's parent's term_ID.
4923 * @param int $term_id Term ID.
4924 * @param string $taxonomy Taxonomy name.
4925 * @return int|false False on error.
4927 function wp_get_term_taxonomy_parent_id( $term_id, $taxonomy ) {
4928 $term = get_term( $term_id, $taxonomy );
4929 if ( ! $term || is_wp_error( $term ) ) {
4932 return (int) $term->parent;
4936 * Checks the given subset of the term hierarchy for hierarchy loops.
4937 * Prevents loops from forming and breaks those that it finds.
4939 * Attached to the {@see 'wp_update_term_parent'} filter.
4943 * @param int $parent `term_id` of the parent for the term we're checking.
4944 * @param int $term_id The term we're checking.
4945 * @param string $taxonomy The taxonomy of the term we're checking.
4947 * @return int The new parent for the term.
4949 function wp_check_term_hierarchy_for_loops( $parent, $term_id, $taxonomy ) {
4950 // Nothing fancy here - bail
4954 // Can't be its own parent.
4955 if ( $parent == $term_id )
4958 // Now look for larger loops.
4959 if ( !$loop = wp_find_hierarchy_loop( 'wp_get_term_taxonomy_parent_id', $term_id, $parent, array( $taxonomy ) ) )
4960 return $parent; // No loop
4962 // Setting $parent to the given value causes a loop.
4963 if ( isset( $loop[$term_id] ) )
4966 // There's a loop, but it doesn't contain $term_id. Break the loop.
4967 foreach ( array_keys( $loop ) as $loop_member )
4968 wp_update_term( $loop_member, $taxonomy, array( 'parent' => 0 ) );