admin管理员组文章数量:1391999
Can I get the term by its id without knowing to which taxonomy it belongs?
I have a meta field that stores term ids, but doesn't store the taxonomy. However, all the get_term
function have taxonomy parameter marked as required.
Maybe I can get the taxonomy of the term by its (term's) id somehow?
Can I get the term by its id without knowing to which taxonomy it belongs?
I have a meta field that stores term ids, but doesn't store the taxonomy. However, all the get_term
function have taxonomy parameter marked as required.
Maybe I can get the taxonomy of the term by its (term's) id somehow?
Share Improve this question edited Mar 19, 2020 at 11:34 frnhr asked Jul 20, 2011 at 16:24 frnhrfrnhr 2372 silver badges15 bronze badges3 Answers
Reset to default 3Yes, but you need you make your own SQL query.
A modified version of WP's get_term():
function &get_term_by_id_only($term, $output = OBJECT, $filter = 'raw') {
global $wpdb;
$null = null;
if ( empty($term) ) {
$error = new WP_Error('invalid_term', __('Empty Term'));
return $error;
}
if ( is_object($term) && empty($term->filter) ) {
wp_cache_add($term->term_id, $term, 'my_custom_queries');
$_term = $term;
} else {
if ( is_object($term) )
$term = $term->term_id;
$term = (int) $term;
if ( ! $_term = wp_cache_get($term, 'my_custom_queries') ) {
$_term = $wpdb->get_row( $wpdb->prepare( "SELECT t.* FROM $wpdb->terms AS t WHERE t.term_id = %s LIMIT 1", $term) );
if ( ! $_term )
return $null;
wp_cache_add($term, $_term, 'my_custom_queries');
}
}
if ( $output == OBJECT ) {
return $_term;
} elseif ( $output == ARRAY_A ) {
$__term = get_object_vars($_term);
return $__term;
} elseif ( $output == ARRAY_N ) {
$__term = array_values(get_object_vars($_term));
return $__term;
} else {
return $_term;
}
}
Probably not a good idea. Try to handle the tax too in your meta fields...
I accepted One Trick Pony's answer because it got me on the right track and it does answer my question. However, in the end I am using this, it returns full term object with it's taxonomy fields. Though it's a bit hacky...
/**
* Get ther without khowing it's taxonomy. Not very nice, though.
*
* @uses type $wpdb
* @uses get_term()
* @param int|object $term
* @param string $output
* @param string $filter
*/
function &get_term_by_id($term, $output = OBJECT, $filter = 'raw') {
global $wpdb;
$null = null;
if ( empty($term) ) {
$error = new WP_Error('invalid_term', __('Empty Term'));
return $error;
}
$_tax = $wpdb->get_row( $wpdb->prepare( "SELECT t.* FROM $wpdb->term_taxonomy AS t WHERE t.term_id = %s LIMIT 1", $term) );
$taxonomy = $_tax->taxonomy;
return get_term($term, $taxonomy, $output, $filter);
}
This is a very old question, but I'm leaving the answer using only a WP method. It's possible to do it with [get_terms( WP_Term_Query_array )][1]
using the include
param (an array of the terms ids):
$terms_ids = [ 0, 1, 2 ];
$terms = get_terms( array( 'include' => $terms_ids ) );
The method returns an array of WP Term objects.
More information about the WP_Term_Query object and its params.
本文标签: get term by id without taxonomy
版权声明:本文标题:get term by id without taxonomy 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744657663a2618064.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论