This page redirects to an external site: https://developer.wordpress.org/reference/functions/get_term_by/
Languages: English • Español • term by 日本語 (Add your language)
Get all Term data from database by Term field and data.
Advertencia: $value (valor) no contendrá escapado-HTML (HTML-escape) para el $campo ($field) 'name' (nombre). De necesitarlo, deberá hacerlo usted mismo.
El $field (campo) por defecto es 'id', por lo tanto, es posible que también puedas usar null (nulo) para field (campo), pero recomendamos que esto no se realice.
Si $value (valor) no existe, el valor retornado será false (falso). Si $taxonomy (taxonomía) existe y la combinación de $field (campo) y $value (valor) existe, será retornado el Term (Término).
<code style="color: #000000"><span style="color: #0000BB"><?php get_term_by</span><span style="color: #007700">( </span><span style="color: #0000BB">$field</span><span style="color: #007700">, </span><span style="color: #0000BB">$value</span><span style="color: #007700">, </span><span style="color: #0000BB">$taxonomy</span><span style="color: #007700">, </span><span style="color: #0000BB">$output</span><span style="color: #007700">, </span><span style="color: #0000BB">$filter </span><span style="color: #007700">) </span><span style="color: #0000BB">?></span></code>
Los campos retornados son:
Advertencia: confusión cadena de texto vs. enteros! Los campos con valores, incluyendo term_id son retornados en formato de cadena de texto. Antes de su uso, typecast numeric values to actual integers, otherwise WordPress will mix up term_ids and slugs which happen to have only numeric characters!
Taxonomy_name es el nombre de la taxonomía, no el term_name y es requerida; el id (term_id) es el ID de el term, no post_id;...
Recordar:
↓ Tipo de Taxonomía (e.g. post_tag)
Terms en esta taxonomía:
→ noticias
→ webdev
→ ...
Ejemplo para tomar términos por nombre y tipo de taxonomía (taxonomy_name como categoría, post_tag o taxonomía personalizada).
// Toma el término con nombre ''news'' en la taxonomía Categorías
$category = get_term_by('name', 'news', 'category')
// Toma el término con el nombre ''news'' en la taxonomía Tags
$tag = get_term_by('name', 'news', 'post_tag')
// Toma el término con el nombre ''news'' en una taxonomía personalizada
$term = get_term_by('name', 'news', 'my_custom_taxonomy')
// Get term by name ''Default Menu'' from theme's nav menus.
// (Alternative to using wp_get_nav_menu_items)
$menu = get_term_by('name', 'Default Menu', 'nav_menu');
By id (term_id, not post_id):
// Toma el término por id (''term_id'' en la taxonomía Categorías.
get_term_by('id', 12, 'category')
...
Advertencia: El siguiente ejemplo esta mal (ver en este historial de pagina):
get_term_by( 'id', (int) $post->ID, 'taxonomy_name' ); // retorna null (nulo)
Este ejemplo trata de tomar los términos por ID (term_id) pero usa un ID de un post como instancia de un ID de termino.
Ahora este ejemplo muestra el modo correcto para tomar el/los term(s) para un post usando el post ID:
// get_term_by( 'id', $category_id, 'category' )
global $post;
$my_categories = array();
$post_categories = get_the_category( $post->ID );
foreach ( $post_categories as $post_category ) {
$my_categories[] = get_term_by( 'id', $post_category->cat_ID, 'category' );
}
// O:
$my_category = get_term_by( 'id', $post_categories[0]->cat_ID, 'category' );
Desde: 2.3.0
get_term_by() is located in wp-includes/taxonomy.php.