wp_get_object_terms [ WordPress Function ]
| Parameters: |
|
| Uses: |
|
| Returns: |
|
| Defined at: |
|
Retrieves the terms associated with the given object(s), in the supplied taxonomies.
The following information has to do the $args parameter and for what can be contained in the string or array of that parameter, if it exists.
The first argument is called, 'orderby' and has the default value of 'name'. The other value that is supported is 'count'.
The second argument is called, 'order' and has the default value of 'ASC'. The only other value that will be acceptable is 'DESC'.
The final argument supported is called, 'fields' and has the default value of 'all'. There are multiple other options that can be used instead. Supported values are as follows: 'all', 'ids', 'names', and finally 'all_with_object_id'.
The fields argument also decides what will be returned. If 'all' or 'all_with_object_id' is chosen or the default kept intact, then all matching terms objects will be returned. If either 'ids' or 'names' is used, then an array of all matching term ids or term names will be returned respectively.
Source
<?php
function wp_get_object_terms($object_ids, $taxonomies, $args = array()) {
global $wpdb;
if ( empty( $object_ids ) || empty( $taxonomies ) )
return array();
if ( !is_array($taxonomies) )
$taxonomies = array($taxonomies);
foreach ( (array) $taxonomies as $taxonomy ) {
if ( ! taxonomy_exists($taxonomy) )
return new WP_Error('invalid_taxonomy', __('Invalid Taxonomy'));
}
if ( !is_array($object_ids) )
$object_ids = array($object_ids);
$object_ids = array_map('intval', $object_ids);
$defaults = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'all');
$args = wp_parse_args( $args, $defaults );
$terms = array();
if ( count($taxonomies) > 1 ) {
foreach ( $taxonomies as $index => $taxonomy ) {
$t = get_taxonomy($taxonomy);
if ( isset($t->args) && is_array($t->args) && $args != array_merge($args, $t->args) ) {
unset($taxonomies[$index]);
$terms = array_merge($terms, wp_get_object_terms($object_ids, $taxonomy, array_merge($args, $t->args)));
}
}
} else {
$t = get_taxonomy($taxonomies[0]);
if ( isset($t->args) && is_array($t->args) )
$args = array_merge($args, $t->args);
}
extract($args, EXTR_SKIP);
if ( 'count' == $orderby )
$orderby = 'tt.count';
else if ( 'name' == $orderby )
$orderby = 't.name';
else if ( 'slug' == $orderby )
$orderby = 't.slug';
else if ( 'term_group' == $orderby )
$orderby = 't.term_group';
else if ( 'term_order' == $orderby )
$orderby = 'tr.term_order';
else if ( 'none' == $orderby ) {
$orderby = '';
$order = '';
} else {
$orderby = 't.term_id';
}
// tt_ids queries can only be none or tr.term_taxonomy_id
if ( ('tt_ids' == $fields) && !empty($orderby) )
$orderby = 'tr.term_taxonomy_id';
if ( !empty($orderby) )
$orderby = "ORDER BY $orderby";
$taxonomies = "'" . implode("', '", $taxonomies) . "'";
$object_ids = implode(', ', $object_ids);
$select_this = '';
if ( 'all' == $fields )
$select_this = 't.*, tt.*';
else if ( 'ids' == $fields )
$select_this = 't.term_id';
else if ( 'names' == $fields )
$select_this = 't.name';
else if ( 'slugs' == $fields )
$select_this = 't.slug';
else if ( 'all_with_object_id' == $fields )
$select_this = 't.*, tt.*, tr.object_id';
$query = "SELECT $select_this FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy AS tt ON tt.term_id = t.term_id INNER JOIN $wpdb->term_relationships AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy IN ($taxonomies) AND tr.object_id IN ($object_ids) $orderby $order";
if ( 'all' == $fields || 'all_with_object_id' == $fields ) {
$terms = array_merge($terms, $wpdb->get_results($query));
update_term_cache($terms);
} else if ( 'ids' == $fields || 'names' == $fields || 'slugs' == $fields ) {
$terms = array_merge($terms, $wpdb->get_col($query));
} else if ( 'tt_ids' == $fields ) {
$terms = $wpdb->get_col("SELECT tr.term_taxonomy_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tr.object_id IN ($object_ids) AND tt.taxonomy IN ($taxonomies) $orderby $order");
}
if ( ! $terms )
$terms = array();
return apply_filters('wp_get_object_terms', $terms, $object_ids, $taxonomies, $args);
}
?>
Examples [ wp-snippets.com ]
Top Google zoekresultaten
- Function Reference/wp get object terms « WordPress Codex
Description. Retrieves the terms associated with the given object(s), in the supplied taxonomies. Usage. <?php wp_get_object_terms( $object_ids, $ taxonomies, ...
codex.wordpress.org - custom taxonomy - Proper use of wp_get_object_terms - WordPress
Apr 8, 2011 ... How can I get the name and the permalink of the object returned by wp_get_object_terms()? Detailled: I created a custom post type called ...
wordpress.stackexchange.com - wp_get_object_terms() | Rachel Carden
Usage. wp_get_object_terms( string|array $object_ids, string|array $cpt_onomies , string|array $args );. Parameters. $object_ids (string|array) (required) Default: ...
rachelcarden.com - php - Exclude term from wp_get_object_terms - Stack Overflow
I'm going to assume 'worktype' is the taxamony you want to exclude. step 1. get all taxamonies excluding 'worktype' with get_terms (you know how to ...
stackoverflow.com
Gebruikersdiscussies [ wordpress.org ]
- auluke02 on "Custom Taxonomy List that Excludes the Parent"
- auluke02 on "Custom Taxonomy List that Excludes the Parent"
- keesiemeijer on "Related posts using wp_get_object_terms"
- nickhempsey on "Related posts using wp_get_object_terms"
- keesiemeijer on "Related posts using wp_get_object_terms"
- nickhempsey on "Related posts using wp_get_object_terms"
- Francis Crossen on "Can't Get the Terms from My Posts!"
- magblogapi on "Can't Get the Terms from My Posts!"
- Francis Crossen on "Can't Get the Terms from My Posts!"
- magblogapi on "Can't Get the Terms from My Posts!"