Entonces, simplemente sucede que necesitaba algo así para un proyecto en el que estoy trabajando. Simplemente escribí una consulta para seleccionar todas las publicaciones de un tipo personalizado, luego verifico cuáles son los términos reales de mi taxonomía que están usando.
Luego obtuve todos los términos de esa taxonomía utilizando get_terms()
y luego solo usé los que estaban en ambas listas, lo envolví en una función y terminé.
Pero luego necesitaba más que solo las ID: necesitaba los nombres, así que agregué un nuevo argumento llamado $fields
para poder decirle a la función qué devolver. Entonces me di cuenta de que get_terms
acepta muchos argumentos y mi función se limitaba a los términos que utiliza un tipo de publicación, así que agregué una declaración if
más y ya está:
La función:
/* get terms limited to post type
@ $taxonomies - (string|array) (required) The taxonomies to retrieve terms from.
@ $args - (string|array) all Possible Arguments of get_terms http://codex.wordpress.org/Function_Reference/get_terms
@ $post_type - (string|array) of post types to limit the terms to
@ $fields - (string) What to return (default all) accepts ID,name,all,get_terms.
if you want to use get_terms arguments then $fields must be set to 'get_terms'
*/
function get_terms_by_post_type($taxonomies,$args,$post_type,$fields = 'all'){
$args = array(
'post_type' => (array)$post_type,
'posts_per_page' => -1
);
$the_query = new WP_Query( $args );
$terms = array();
while ($the_query->have_posts()){
$the_query->the_post();
$curent_terms = wp_get_object_terms( $post->ID, $taxonomy);
foreach ($curent_terms as $t){
//avoid duplicates
if (!in_array($t,$terms)){
$terms[] = $c;
}
}
}
wp_reset_query();
//return array of term objects
if ($fields == "all")
return $terms;
//return array of term ID's
if ($fields == "ID"){
foreach ($terms as $t){
$re[] = $t->term_id;
}
return $re;
}
//return array of term names
if ($fields == "name"){
foreach ($terms as $t){
$re[] = $t->name;
}
return $re;
}
// get terms with get_terms arguments
if ($fields == "get_terms"){
$terms2 = get_terms( $taxonomies, $args );
foreach ($terms as $t){
if (in_array($t,$terms2)){
$re[] = $t;
}
}
return $re;
}
}
Uso:
Si solo necesita una lista de ID de término, entonces:
$terms = get_terms_by_post_type('tag','','snippet','ID');
Si solo necesita una lista de nombres de términos, entonces:
$terms = get_terms_by_post_type('tag','','snippet','name');
Si solo necesita una lista de objetos de términos, entonces:
$terms = get_terms_by_post_type('tag','','snippet');
Y si necesita utilizar argumentos adicionales de get_terms como: orderby, order, hierarchical ...
$args = array('orderby' => 'count', 'order' => 'DESC', 'hide_empty' => 1);
$terms = get_terms_by_post_type('tag',$args,'snippet','get_terms');
¡Disfruta!
Actualización:
Para corregir el recuento de términos para un cambio de tipo de publicación específico:
foreach ($current_terms as $t){
//avoid duplicates
if (!in_array($t,$terms)){
$terms[] = $t;
}
}
a:
foreach ($current_terms as $t){
//avoid duplicates
if (!in_array($t,$terms)){
$t->count = 1;
$terms[] = $t;
}else{
$key = array_search($t, $terms);
$terms[$key]->count = $terms[$key]->count + 1;
}
}