Switch language

wpseek.com
A WordPress-centric search engine for devs and theme authors




wp_get_object_terms [ WordPress Function ]

wp_get_object_terms ( $object_ids, $taxonomies, $args = array() )
Parameters:
  • (int|array) $object_ids The ID(s) of the object(s) to retrieve.
  • (string|array) $taxonomies The taxonomies to retrieve terms from.
  • (array|string) $args Change what is returned
Uses:
  • $wpdb
Returns:
  • (array|WP_Error) The requested term data or empty array if no terms found. WP_Error if any of the $taxonomies don't exist.
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) > ) {
        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($termswp_get_object_terms($object_ids$taxonomyarray_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($argsEXTR_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

Meer ...

0 User Note(s)

Nog geen één. Wees de eerste!

Nieuw toevoegen ...



HTML5 Powered with CSS3 / Styling, Performance & Integration, and Semantics