WordPress Taxonomy based off of WordPress revision 8782.
since | 2.3.0 |
---|---|
package | WordPress |
WP_Taxonomy($db)
__construct(\unknown_type $db)
\unknown_type
_get_term_children(integer $term_id, array $terms, string $taxonomy) : array
If $terms is an array of objects, then _get_term_children returns an array of objects. If $terms is an array of IDs, then _get_term_children returns an array of IDs.
package | WordPress |
---|---|
subpackage | Taxonomy |
access | private |
since | 2.3.0 |
integer
The ancestor term: all returned terms should be descendants of $term_id.
array
The set of terms---either an array of term objects or term IDs---from which those that are descendants of $term_id will be chosen.
string
The taxonomy which determines the hierarchy of the terms.
array
The subset of $terms that are descendants of $term_id._get_term_hierarchy(string $taxonomy) : array
package | WordPress |
---|---|
subpackage | Taxonomy |
access | private |
since | 2.3.0 |
uses | Stores all of the children in "$taxonomy_children" option. That is the name of the taxonomy, immediately followed by '_children'. |
string
Taxonomy Name
array
Empty if $taxonomy isn't hierarachical or returns children as Term IDs._pad_term_counts(array $terms, string $taxonomy) : null
Recalculates term counts by including items from child terms. Assumes all relevant children are already in the $terms argument.
package | WordPress |
---|---|
subpackage | Taxonomy |
access | private |
since | 2.3 |
array
List of Term IDs
string
Term Context
null
Will break from function if conditions are not met.clean_object_term_cache(integer|array $object_ids, string $object_type)
Will remove the entire taxonomy relationship containing term $object_id. The term IDs have to exist within the taxonomy $object_type for the deletion to take place.
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3 |
see | for more on $object_type |
uses | Will call action hook named, 'clean_object_term_cache' after completion. Passes, function params in same order. |
integer
array
Single or list of term object ID(s)
string
The taxonomy object type
clean_term_cache(integer|array $ids, string $taxonomy = ''
)
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
integer
array
Single or list of Term IDs
string
Can be empty and will assume tt_ids, else will use for context.
count_terms(string $taxonomy, array|string $args = array()
) : integer
Default $args is 'ignore_empty' which can be 'ignore_empty=true'
or array('ignore_empty' => true);
.
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
uses | Turns strings into arrays and merges defaults into an array. |
string
Taxonomy name
array
string
Overwrite defaults
integer
How many terms are in $taxonomydefer_term_counting(boolean $defer = NULL
) : boolean
since | 2.5.0 |
---|
boolean
Optional. Enable if true, disable if false.
boolean
Whether term counting is enabled or disabled.delete_children_cache($taxonomy)
delete_object_term_relationships(integer $object_id, $taxonomies)
Will remove the term's relationship to the taxonomy, not the term or taxonomy itself. The term and taxonomy will still exist. Will require the term's object ID to perform the operation.
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
integer
The term Object Id that refers to the term
delete_term(integer $term, string $taxonomy, array|string $args = array()
) : boolean | \WP_Error
If the term is a parent of other terms, then the children will be updated to that term's parent.
The $args 'default' will only override the terms found, if there is only one term found. Any other and the found terms are used.
The $args 'force_default' will force the term supplied as default to be assigned even if the object was not going to be termless
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
uses | Calls both 'delete_term' and 'delete_$taxonomy' action hooks, passing term object, term id. 'delete_term' gets an additional parameter with the $taxonomy parameter. |
integer
Term ID
string
Taxonomy Name
array
string
Optional. Change 'default' term id and override found term ids.
boolean
\WP_Error
Returns false if not term; true if completes delete action.format_to_edit($text)
get_children_cache($taxonomy)
get_object_taxonomies(array|string|object $object_type) : array
It appears that this function can be used to find all of the names inside of $this->taxonomies variable.
<?php $taxonomies = $this->get_object_taxonomies('post'); ?>
Should
result in Array('category', 'post_tag')
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
uses |
array
string
object
Name of the type of taxonomy object, or an object (row from posts)
array
The names of all taxonomy of $object_type.get_object_term_cache(integer|array $id, string $taxonomy) : boolean | array
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
uses | Retrieves taxonomy relationship from cache |
integer
array
Term object ID
string
Taxonomy Name
boolean
array
Empty array if $terms found, but not $taxonomy. False if nothing is in cache for $taxonomy and $id.get_object_terms($object_ids, string|array $taxonomies, array|string $args = array()
) : array | \WP_Error
The following information has to do the $args parameter and for what can be contained in the string or array of that parameter, if it exists.
The first argument is called, 'orderby' and has the default value of 'name'. The other value that is supported is 'count'.
The second argument is called, 'order' and has the default value of 'ASC'. The only other value that will be acceptable is 'DESC'.
The final argument supported is called, 'fields' and has the default value of 'all'. There are multiple other options that can be used instead. Supported values are as follows: 'all', 'ids', 'names', and finally 'all_with_object_id'.
The fields argument also decides what will be returned. If 'all' or 'all_with_object_id' is choosen or the default kept intact, then all matching terms objects will be returned. If either 'ids' or 'names' is used, then an array of all matching term ids or term names will be returned respectively.
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
string
array
The taxonomies to retrieve terms from.
array
string
Change what is returned
array
\WP_Error
The requested term data or empty array if no terms found. WP_Error if $taxonomy does not exist.get_objects_in_term(string|array $terms, string|array $taxonomies, array|string $args = null
) : \WP_Error | array
The strings of $taxonomies must exist before this function will continue. On failure of finding a valid taxonomy, it will return an WP_Error class, kind of like Exceptions in PHP 5, except you can't catch them. Even so, you can still test for the WP_Error class and get the error message.
The $terms aren't checked the same as $taxonomies, but still need to exist for $object_ids to be returned.
It is possible to change the order that object_ids is returned by either using PHP sort family functions or using the database by using $args with either ASC or DESC array. The value should be in the key named 'order'.
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
uses | Creates an array from string $args. |
string
array
String of term or array of string values of terms that will be used
string
array
String of taxonomy name or Array of string values of taxonomy names
array
string
Change the order of the object_ids, either ASC or DESC
\WP_Error
array
If the taxonomy does not exist, then WP_Error will be returned. On success
the array can be empty meaning that there are no $object_ids found or it will return the $object_ids found.get_taxonomy(string $taxonomy) : object | boolean
The get_taxonomy function will first check that the parameter string given is a taxonomy object and if it is, it will return it.
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
uses | |
uses | Checks whether taxonomy exists |
string
Name of taxonomy object to return
object
boolean
The Taxonomy Object or false if $taxonomy doesn't existget_term(integer|object $term, string $taxonomy, string $output= OBJECT
, string $filter= 'raw'
) : mixed | null | \WP_Error
The usage of the get_term function is to apply filters to a term object. It is possible to get a term object from the database before applying the filters.
$term ID must be part of $taxonomy, to get from the database. Failure, might be able to be captured by the hooks. Failure would be the same value as $this->db returns for the get_row method.
There are two hooks, one is specifically for each term, named 'getterm', and the second is for the taxonomy name, 'term$taxonomy'. Both hooks gets the term object, and the taxonomy name as parameters. Both hooks are expected to return a Term object.
'get_term' hook - Takes two parameters the term Object and the taxonomy name. Must return term object. Used in get_term() as a catch-all filter for every $term.
'get_$taxonomy' hook - Takes two parameters the term Object and the taxonomy name. Must return term object. $taxonomy will be the taxonomy name, so for example, if 'category', it would be 'get_category' as the filter name. Useful for custom taxonomies or plugging into default taxonomies.
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
uses | Cleanses the term based on $filter context before returning. |
see | The $context param lists the available values for get_term_by() $filter param. |
integer
object
If integer, will get from database. If object will apply filters and return $term.
string
Taxonomy name that $term is part of.
string
Constant OBJECT, ARRAY_A, or ARRAY_N
string
Optional, default is raw or no WordPress defined filter will applied.
mixed
null
\WP_Error
Term Row from database. Will return null if $term is empty. If taxonomy does not
exist then WP_Error will be returned.get_term_by(string $field, string|integer $value, string $taxonomy, string $output= OBJECT
, string $filter= 'raw'
) : mixed
Warning: $value is not escaped for 'name' $field. You must do it yourself, if required.
The default $field is 'id', therefore it is possible to also use null for field, but not recommended that you do so.
If $value does not exist, the return value will be false. If $taxonomy exists and $field and $value combinations exist, the Term will be returned.
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
uses | Cleanses the term based on $filter context before returning. |
see | The $context param lists the available values for get_term_by() $filter param. |
string
Either 'slug', 'name', 'id', or 'tt_id'
string
integer
Search for this term value
string
Taxonomy Name
string
Constant OBJECT, ARRAY_A, or ARRAY_N
string
Optional, default is raw or no WordPress defined filter will applied.
mixed
Term Row from database. Will return false if $taxonomy does not exist or $term was not found.get_term_children($term_id, string $taxonomy) : array | \WP_Error
This recursive function will merge all of the children of $term into the same array of term IDs. Only useful for taxonomies which are hierarchical.
Will return an empty array if $term does not exist in $taxonomy.
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
uses | |
uses | Used to get the children of both $taxonomy and the parent $term |
string
Taxonomy Name
array
\WP_Error
List of Term Objects. WP_Error returned if $taxonomy does not existget_term_field(string $field, integer $term, string $taxonomy, string $context = 'display'
) : mixed
Does checks for $term, based on the $taxonomy. The function is for contextual reasons and for simplicity of usage. See sanitize_term_field() for more information.
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
uses | Passes the return value in sanitize_term_field on success. |
string
Term field to fetch
integer
Term ID
string
Taxonomy Name
string
Optional, default is display. Look at sanitize_term_field() for available options.
mixed
Will return an empty string if $term is not an object or if $field is not set in $term.get_term_to_edit(integer|object $id, string $taxonomy) : mixed | null | \WP_Error
Return value is sanitize_term() and usage is for sanitizing the term for editing. Function is for contextual and simplicity.
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
uses | Passes the return value on success |
integer
object
Term ID or Object
string
Taxonomy Name
mixed
null
\WP_Error
Will return empty string if $term is not an object.get_terms(string | array $taxonomies, string|array $args = ''
) : array | \WP_Error
You can fully inject any customizations to the query before it is sent, as well as control the output with a filter.
The 'get_terms' filter will be called when the cache has the term and will pass the found term along with the array of $taxonomies and array of $args. This filter is also called before the array of terms is passed and will pass the array of terms, along with the $taxonomies and $args.
The 'list_terms_exclusions' filter passes the compiled exclusions along with the $args.
The 'get_terms_orderby' filter passes the ORDER BY clause for the query along with the $args array.
The 'get_terms_fields' filter passes the fields for the SELECT query along with the $args array.
The list of arguments that $args can contain, which will overwrite the defaults:
orderby - Default is 'name'. Can be name, count, term_group, slug or nothing (will use term_id), Passing a custom value other than these will cause it to order based on the custom value.
order - Default is ASC. Can use DESC.
hide_empty - Default is true. Will not return empty terms, which means terms whose count is 0 according to the given taxonomy.
exclude - Default is an empty string. A comma- or space-delimited string of term ids to exclude from the return array. If 'include' is non-empty, 'exclude' is ignored.
include - Default is an empty string. A comma- or space-delimited string of term ids to include in the return array.
number - The maximum number of terms to return. Default is empty.
offset - The number by which to offset the terms query.
fields - Default is 'all', which returns an array of term objects. If 'fields' is 'ids' or 'names', returns an array of integers or strings, respectively.
slug - Returns terms whose "slug" matches this value. Default is empty string.
hierarchical - Whether to include terms that have non-empty descendants (even if 'hide_empty' is set to true).
search - Returned terms' names will contain the value of 'search', case-insensitive. Default is an empty string.
namelike - Returned terms' names will begin with the value of 'namelike', case-insensitive. Default is empty string.
The argument 'pad_counts', if set to true will include the quantity of a term's children in the quantity of each term's "count" object variable.
The 'get' argument, if set to 'all' instead of its default empty string, returns terms regardless of ancestry or whether the terms are empty.
The 'child_of' argument, when used, should be set to the integer of a term ID. Its default is 0. If set to a non-zero value, all returned terms will be descendants of that term according to the given taxonomy. Hence 'child_of' is set to 0 if more than one taxonomy is passed in $taxonomies, because multiple taxonomies make term ancestry ambiguous.
The 'parent' argument, when used, should be set to the integer of a term ID. Its default is the empty string '', which has a different meaning from the integer 0. If set to an integer value, all returned terms will have as an immediate ancestor the term whose ID is specified by that integer according to the given taxonomy. The 'parent' argument is different from 'child_of' in that a term X is considered a 'parent' of term Y only if term X is the father of term Y, not its grandfather or great-grandfather, etc.
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
uses | Merges the defaults with those defined by $args and allows for strings. |
string
array
Taxonomy name or list of Taxonomy names
string
array
The values of what to search for when returning terms
array
\WP_Error
List of Term Objects and their children. Will return WP_Error, if any of $taxonomies do not exist.insert_term(integer|string $term, string $taxonomy, array|string $args = array()
) : array | \WP_Error
Error handling is assigned for the nonexistance of the $taxonomy and $term parameters before inserting. If both the term id and taxonomy exist previously, then an array will be returned that contains the term id and the contents of what is returned. The keys of the array are 'term_id' and 'term_taxonomy_id' containing numeric values.
It is assumed that the term does not yet exist or the above will apply. The term will be first added to the term table and then related to the taxonomy if everything is well. If everything is correct, then several actions will be run prior to a filter and then several actions will be run after the filter is run.
The arguments decide how the term is handled based on the $args parameter. The following is a list of the available overrides and the defaults.
'alias_of'. There is no default, but if added, expected is the slug that the term will be an alias of. Expected to be a string.
'description'. There is no default. If exists, will be added to the database along with the term. Expected to be a string.
'parent'. Expected to be numeric and default is 0 (zero). Will assign value of 'parent' to the term.
'slug'. Expected to be a string. There is no default.
If 'slug' argument exists then the slug will be checked to see if it is not a valid term. If that check succeeds (it is not a valid term), then it is added and the term id is given. If it fails, then a check is made to whether the taxonomy is hierarchical and the parent argument is not empty. If the second check succeeds, the term will be inserted and the term id will be given.
integer
string
The term to add or update.
string
The taxonomy to which to add the term
array
string
Change the values of the inserted term
array
\WP_Error
The Term ID and Term Taxonomy IDis_object_in_term($object_id, $taxonomy, integer|string|array $terms = null
) : boolean | \WP_Error.
The given terms are checked against the object's terms' term_ids, names and slugs. Terms given as integers will only be checked against the object's terms' term_ids. If no terms are given, determines if object is associated with any terms in the given taxonomy.
since | 2.7.0 |
---|---|
uses | |
uses |
integer
string
array
Optional. Term term_id, name, slug or array of said
boolean
\WP_Error.
WP_Error on input error.is_taxonomy(string $taxonomy) : boolean
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
uses |
string
Name of taxonomy object
boolean
Whether the taxonomy exists or not.is_taxonomy_hierarchical(string $taxonomy) : boolean
Checks to make sure that the taxonomy is an object first. Then Gets the object, and finally returns the hierarchical value in the object.
A false return value might also mean that the taxonomy does not exist.
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
uses | Checks whether taxonomy exists |
uses | Used to get the taxonomy object |
string
Name of taxonomy object
boolean
Whether the taxonomy is hierarchicalis_term(integer|string $term, string $taxonomy= ''
, integer $parent= 0
) : mixed
Returns the index of a defined term, or 0 (false) if the term doesn't exist.
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
integer
string
The term to check
string
The taxonomy name to use
integer
ID of parent term under which to confine the exists search.
mixed
Get the term id or Term Object, if exists.register_taxonomy(string $taxonomy, string $object_type, array|string $args = array()
)
A simple function for creating or modifying a taxonomy object based on the parameters given. The function will accept an array (third optional parameter), along with strings for the taxonomy name and another string for the object type.
The function keeps a default set, allowing for the $args to be optional but allow the other functions to still work. It is possible to overwrite the default set, which contains two keys: hierarchical and update_count_callback.
Nothing is returned, so expect error maybe or use is_taxonomy() to check whether taxonomy exists.
Optional $args contents:
hierarachical - has some defined purpose at other parts of the API and is a boolean value.
update_count_callback - works much like a hook, in that it will be called when the count is updated.
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
uses | Inserts new taxonomy object into the list |
string
Name of taxonomy object
string
Name of the object type for the taxonomy object.
array
string
See above description for the two keys values.
sanitize_term(array|object $term, string $taxonomy, string $context = 'display'
) : array | object
Relies on sanitize_term_field() to sanitize the term. The difference is that this function will sanitize all fields. The context is based on sanitize_term_field().
The $term is expected to be either an array or an object.
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
uses | Used to sanitize all fields in a term |
array
object
The term to check
string
The taxonomy name to use
string
Default is 'display'.
array
object
Term with all fields sanitizedsanitize_term_field(string $field, string $value, integer $term_id, string $taxonomy, string $context) : mixed
Passing a term field value through the function should be assumed to have cleansed the value for whatever context the term field is going to be used.
If no context or an unsupported context is given, then default filters will be applied.
There are enough filters for each context to support a custom filtering without creating your own filter function. Simply create a function that hooks into the filter you need.
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
string
Term field to sanitize
string
Search for this term value
integer
Term ID
string
Taxonomy Name
string
Either edit, db, display, attribute, or js.
mixed
sanitized fieldsanitize_term_slug($title, $taxonomy= ''
, $term_id= 0
)
set_children_cache($taxonomy, $children)
set_object_terms(integer $object_id, $terms, array|string $taxonomy, boolean $append = false
) : array | \WP_Error
Relates an object (post, link etc) to a term and taxonomy type. Creates the term and taxonomy relationship if it doesn't already exist. Creates a term if it doesn't exist (using the slug).
A relationship means that the term is grouped in or belongs to the taxonomy. A term has no meaning until it is given context by defining which taxonomy it exists under.
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
integer
The object to relate to.
array
string
The context in which to relate the term to the object.
boolean
If false will delete difference of terms.
array
\WP_Error
Affected Term IDsunique_term_slug(string $slug, object $term) : string
The $slug has to be unique global to every taxonomy, meaning that one taxonomy term can't have a matching slug with another taxonomy term. Each slug has to be globally unique for every taxonomy.
The way this works is that if the taxonomy that the term belongs to is heirarchical and has a parent, it will append that parent to the $slug.
If that still doesn't return an unique slug, then it try to append a number until it finds a number that is truely unique.
The only purpose for $term is for appending a parent, if one exists.
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
string
The string that will be tried for a unique slug
object
The term object that the $slug will belong too
string
Will return a true unique slug.update_object_term_cache(string|array $object_ids, string $object_type) : null | boolean
Will only update the cache for terms not already cached.
The $object_ids expects that the ids be separated by commas, if it is a string.
It should be noted that update_object_term_cache() is very time extensive. It is advised that the function is not called very often or at least not for a lot of terms that exist in a lot of taxonomies. The amount of time increases for each term and it also increases for each taxonomy the term belongs to.
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
uses | Used to get terms from the database to update |
string
array
Single or list of term object ID(s)
string
The taxonomy object type
null
boolean
Null value is given with empty $object_ids. False ifupdate_term(integer $term_id, string $taxonomy, array|string $args = array()
) : array | \WP_Error
The $args will indiscriminately override all values with the same field name. Care must be taken to not override important information need to update or update will fail (or perhaps create a new term, neither would be acceptable).
Defaults will set 'alias_of', 'description', 'parent', and 'slug' if not defined in $args already.
'alias_of' will create a term group, if it doesn't already exist, and update it for the $term.
If the 'slug' argument in $args is missing, then the 'name' in $args will be used. It should also be noted that if you set 'slug' and it isn't unique then a WP_Error will be passed back. If you don't pass any slug, then a unique one will be created for you.
For what can be overrode in $args, check the term scheme can contain and stay away from the term keys.
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
uses | Will call both 'edit_term' and 'edit_$taxonomy' twice. |
uses | Will call the 'term_id_filter' filter and pass the term id and taxonomy id. |
integer
The ID of the term
string
The context in which to relate the term to the object.
array
string
Overwrite term field values
array
\WP_Error
Returns Term ID and Taxonomy Term IDupdate_term_cache(array $terms, string $taxonomy = ''
)
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
array
List of Term objects to change
string
Optional. Update Term to this taxonomy in cache
update_term_count(integer|array $terms, string $taxonomy, $do_deferred = false
) : boolean
If there is a taxonomy callback applied, then it will be called for updating the count.
The default action is to count what the amount of terms have the relationship of term ID. Once that is done, then update the database.
package | WordPress |
---|---|
subpackage | Taxonomy |
since | 2.3.0 |
uses |
integer
array
The ID of the terms
string
The context of the term.
boolean
If no terms will return false, and if successful will return true.update_term_count_now(array $terms, string $taxonomy) : boolean
since | 2.5.0 |
---|
array
The term_taxonomy_id of terms to update.
string
The context of the term.
boolean
Always true when complete.$db : \unknown_type
$taxonomies
array()