admin管理员组文章数量:1026329
I have a hierarchical taxonomy filter
that contains locations and genres for posts with the custom type artist
:
- Genre
-- Hip Hop
-- Trap
-- Rap
- Location
-- Europe
--- Germany
--- Sweden
--- Austria
-- Asia
--- China
--- Japan
--- Taiwan
Now I would like to use get_terms() to only get the Countries (children of 'Location' without children of their own). I thought this should work:
$location_parent = get_term(123, 'filter');
$countries = get_terms(array(
'taxonomy' => $location_parent->taxonomy,
'hide_empty' => false,
'child_of' => $location_parent->term_id,
'childless' => true
));
...but it somehow doesn't. child_of
and childless
seem to get in each other's way. Any ideas?
I have a hierarchical taxonomy filter
that contains locations and genres for posts with the custom type artist
:
- Genre
-- Hip Hop
-- Trap
-- Rap
- Location
-- Europe
--- Germany
--- Sweden
--- Austria
-- Asia
--- China
--- Japan
--- Taiwan
Now I would like to use get_terms() to only get the Countries (children of 'Location' without children of their own). I thought this should work:
$location_parent = get_term(123, 'filter');
$countries = get_terms(array(
'taxonomy' => $location_parent->taxonomy,
'hide_empty' => false,
'child_of' => $location_parent->term_id,
'childless' => true
));
...but it somehow doesn't. child_of
and childless
seem to get in each other's way. Any ideas?
2 Answers
Reset to default 2OK, so this is what I came up with:
function get_childless_term_children( $parent_id, $taxonomy ) {
// get all childless $terms of this $taxonomy
$terms = get_terms(array(
'taxonomy' => $taxonomy,
'childless' => true,
));
foreach( $terms as $key => $term ) {
// remove $terms that aren't descendants (= children) of $parent_id
if( !in_array( $parent_id, get_ancestors( $term->term_id, $taxonomy ) ) ) {
unset( $terms[$key] );
}
}
return $terms;
}
What this does: Get all childless terms that are children of $parent_id
.
childless means:
(boolean) Returns terms that have no children if taxonomy is hierarchical, all terms if taxonomy is not hierarchical
Reference: https://codex.wordpress/es:Function_Reference/get_terms
I think you're finding something like this:
/**
* Recursively get taxonomy hierarchy
*
* @source http://www.daggerhart/wordpress-get-taxonomy-hierarchy-including-children/
* @param string $taxonomy
* @param int $parent - parent term id
*
* @return array
*/
function get_taxonomy_hierarchy( $taxonomy, $parent = 0 ) {
// only 1 taxonomy
$taxonomy = is_array( $taxonomy ) ? array_shift( $taxonomy ) : $taxonomy;
// get all direct decendents of the $parent
$terms = get_terms( $taxonomy, array('parent' => $parent) );
// prepare a new array. these are the children of $parent
// we'll ultimately copy all the $terms into this new array, but only after they
// find their own children
$children = array();
// go through all the direct decendents of $parent, and gather their children
foreach( $terms as $term ) {
// recurse to get the direct decendents of "this" term
$term->children = get_taxonomy_hierarchy( $taxonomy, $term->term_id );
// add the term to our new array
$children[ $term->term_id ] = $term;
}
// send the results back to the caller
return $children;
}
It should return all the child from Location
We are agree?
I have a hierarchical taxonomy filter
that contains locations and genres for posts with the custom type artist
:
- Genre
-- Hip Hop
-- Trap
-- Rap
- Location
-- Europe
--- Germany
--- Sweden
--- Austria
-- Asia
--- China
--- Japan
--- Taiwan
Now I would like to use get_terms() to only get the Countries (children of 'Location' without children of their own). I thought this should work:
$location_parent = get_term(123, 'filter');
$countries = get_terms(array(
'taxonomy' => $location_parent->taxonomy,
'hide_empty' => false,
'child_of' => $location_parent->term_id,
'childless' => true
));
...but it somehow doesn't. child_of
and childless
seem to get in each other's way. Any ideas?
I have a hierarchical taxonomy filter
that contains locations and genres for posts with the custom type artist
:
- Genre
-- Hip Hop
-- Trap
-- Rap
- Location
-- Europe
--- Germany
--- Sweden
--- Austria
-- Asia
--- China
--- Japan
--- Taiwan
Now I would like to use get_terms() to only get the Countries (children of 'Location' without children of their own). I thought this should work:
$location_parent = get_term(123, 'filter');
$countries = get_terms(array(
'taxonomy' => $location_parent->taxonomy,
'hide_empty' => false,
'child_of' => $location_parent->term_id,
'childless' => true
));
...but it somehow doesn't. child_of
and childless
seem to get in each other's way. Any ideas?
-
2
Are you sure having a single filter taxonomy is wise? A
genre
andlocation
taxonomy would be better, looking at your hierarchy though, could you just tell it not to recurse down and ignore children? Or use more than one request? This is probably one of those situations were the real problem is that you're asking too much of 1 function call and you're in need of multiple steps – Tom J Nowell ♦ Commented May 29, 2018 at 12:52 - You are wrong with your thoughts: - Taxonomy is not "Location", Taxonomy is the name of your Custom Tax like category, post_tag, ... What's the name of your taxonomy? - What's is $location var? – IvanMunoz Commented May 29, 2018 at 13:58
-
@TomJNowell, yes, usually that would be right. But in this particular case I can't use multiple taxonomies. Just expected
childless
andchild_of
to work together, don't you think? – rassoh Commented May 29, 2018 at 20:05 -
@IvanMunoz The taxonomy is
filter
, notlocation
. I will edit my question so this becomes clearer. – rassoh Commented May 29, 2018 at 20:05 - @TomJNowell can you explain a bit further what you mean by "just tell it not to recurse down and ignore children"? – rassoh Commented May 29, 2018 at 20:12
2 Answers
Reset to default 2OK, so this is what I came up with:
function get_childless_term_children( $parent_id, $taxonomy ) {
// get all childless $terms of this $taxonomy
$terms = get_terms(array(
'taxonomy' => $taxonomy,
'childless' => true,
));
foreach( $terms as $key => $term ) {
// remove $terms that aren't descendants (= children) of $parent_id
if( !in_array( $parent_id, get_ancestors( $term->term_id, $taxonomy ) ) ) {
unset( $terms[$key] );
}
}
return $terms;
}
What this does: Get all childless terms that are children of $parent_id
.
childless means:
(boolean) Returns terms that have no children if taxonomy is hierarchical, all terms if taxonomy is not hierarchical
Reference: https://codex.wordpress/es:Function_Reference/get_terms
I think you're finding something like this:
/**
* Recursively get taxonomy hierarchy
*
* @source http://www.daggerhart/wordpress-get-taxonomy-hierarchy-including-children/
* @param string $taxonomy
* @param int $parent - parent term id
*
* @return array
*/
function get_taxonomy_hierarchy( $taxonomy, $parent = 0 ) {
// only 1 taxonomy
$taxonomy = is_array( $taxonomy ) ? array_shift( $taxonomy ) : $taxonomy;
// get all direct decendents of the $parent
$terms = get_terms( $taxonomy, array('parent' => $parent) );
// prepare a new array. these are the children of $parent
// we'll ultimately copy all the $terms into this new array, but only after they
// find their own children
$children = array();
// go through all the direct decendents of $parent, and gather their children
foreach( $terms as $term ) {
// recurse to get the direct decendents of "this" term
$term->children = get_taxonomy_hierarchy( $taxonomy, $term->term_id );
// add the term to our new array
$children[ $term->term_id ] = $term;
}
// send the results back to the caller
return $children;
}
It should return all the child from Location
We are agree?
本文标签: taxonomygetterms() with childof and childless combined
版权声明:本文标题:taxonomy - `get_terms()` with `child_of` and `childless` combined 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745626016a2159865.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
genre
andlocation
taxonomy would be better, looking at your hierarchy though, could you just tell it not to recurse down and ignore children? Or use more than one request? This is probably one of those situations were the real problem is that you're asking too much of 1 function call and you're in need of multiple steps – Tom J Nowell ♦ Commented May 29, 2018 at 12:52childless
andchild_of
to work together, don't you think? – rassoh Commented May 29, 2018 at 20:05filter
, notlocation
. I will edit my question so this becomes clearer. – rassoh Commented May 29, 2018 at 20:05