I have taxonomy with 3 levels of children. I need to get all level of children by a parent tid.
taxonomy_get_children($tid) - gives a direct children of that particular $tid only. But not the all grand children.
How do I process this?
Thanks,
function taxonomy_get_children_all($tid, $vid = 0, $key = 'tid'){
$c = taxonomy_get_children($tid, $vid, $key);
$result = array();
foreach ($c as $t => $d){
$result[$t] = $d;
$below = taxonomy_get_children_all($t, $vid, $key);
if (!empty($below)) {
foreach ($below as $nt => $nd){
$result[$nt] = $nd;
}
}
}
return $result;
}
Reference: https://drupal.org/node/381952
Looking on https://api.drupal.org/api/drupal/modules%21taxonomy%21taxonomy.module/function/taxonomy_get_children/7
and
https://api.drupal.org/api/drupal/modules%21taxonomy%21taxonomy.module/function/taxonomy_get_children/6
You should get multiple results. What Drupal do you use ?
$tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('MACHINE_VOCABULARY_NAME');
foreach ($tree as $term) {
$termID[] = array(
'termId' => $term->tid,
'parent'=> $term->parents,
'depth' => $term->depth
);
}
dump($termID);
Related
It's rendering taxonmy terms. I need only published terms. There are two languages in site. Publish status can be translated. I need help with condition. I don't work with php. Could anyone help?
foreach ($child_terms as $term) {
$taxonomy_term = \Drupal\taxonomy\Entity\Term::load($term->tid);
$taxonomy_term_trans = \Drupal::service('entity.repository')->getTranslationFromContext($taxonomy_term, $curr_langcode);
if (????) {
$child_term_options[$term->tid] = $taxonomy_term_trans->getName();
}
}
Assuming you pretend to loop terms that are children of some given term.
Assuming you know the term id of the parent term.
<?php
$status = TRUE;
$lang_code = \Drupal::languageManager()
->getCurrentLanguage()
->getId();
$child_terms = \Drupal::entityTypeManager()
->getStorage('taxonomy_term')
->loadByProperties([
'parent' => $parent_tid,
'status' => $status,
'langcode' => $lang_code,
]);
foreach ($child_terms as $term) {
$name = $term->getName();
$tid = $term->id();
}
I would like to get all the taxonomy of one post (in a loop), hierarchically. Example I have those taxonomies, and the ID of the tax in bracket.
Tax1(1)
-Tax2(3)
--Tax3(2)
I would like to gather them, in an array maybe, in this order. Right now I manage to get an array of those 3, but the order is wrong. I can't order it by id, since the ID are not ordered at first. I can't also order it by name and slug. (Names of my current taxonomies are not Tax1, Tax2...)
The code I have at the moment is
$args = array('orderby' => 'term_order', 'order' => 'ASC', 'fields' => 'all');
$productcategories = wp_get_object_terms($post->ID, 'guide_type', $args);
Use "Wordpress" Walker class to create a hierarchy of the taxonomy
<?php
class Walker_Quickstart extends Walker {
// Tell Walker where to inherit it's parent and id values
var $db_fields = array(
'parent' => 'parent',
'id' => 'term_id'
);
/**
* At the start of each element, output a <p> tag structure.
*/
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$output .= sprintf( "\n<p>%s %s (%s)</p>\n",
str_repeat('‐', $depth),
$item->name,
$item->term_id
);
}
}?>
This class will create a hierarchy of elements. Use this class with your returned elements like this :
$args = array('orderby' => 'term_order', 'order' => 'ASC', 'fields' => 'all');
$productcategories = wp_get_object_terms($post->ID, 'guide_type', $args);
$walk = new Walker_Quickstart();
echo $walk->walk($productcategories, 0);
Was about to get something with this function that I made, But Vikash Kumar gave me a better answer, thanks !
function get_term_top_most_parent($post_id, $taxonomy){
$return = array();
$registeredcat = 0;
$newparent = '';
$catcount = 0;
$firstlevels = wp_get_object_terms( $post_id, $taxonomy); //post id, taxo, args
foreach ($firstlevels as $key => $value){
if($value->parent == 0 ){
//$firstlevel = $value->term_id; //23
$newparent = $value->term_id;
array_push($return, $value);
$registeredcat += 1;
}
$catcount += 1;
}
return $return;
}
How can i get the no of posts inside a term of a custom taxonomy ? (including the posts that are attached to a child term).
For example i have:
term (2 posts)
-child term (2 posts)
--child child term (1 post)
Right now i'm, doing it like this:
$categories = get_terms($taxonomy,$args);
foreach ($categories as $categ) {
print $categ->name.' / '.$categ->count;
}
But for "term" i get only 2 posts when i really need to show 5( 2 from "term" and 3 from it's children).
Thanks
There is an easier way to do this: do a standard WP_Query with taxonomy parameters:
$args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'your_custom_taxonomy',
'field' => 'slug',
'terms' => 'your-parent-term-name',
),
),
);
$query = new WP_Query( $args );
// Get the count
$count = $query->post_count;
If you don't know the name(s) of terms within a taxonomy, you can do a query and pass their IDs as an array to WP_Query. See this post on WPSE for more info.
This is something I run into few times, and I changed the code from PatchRanger on bountify to make it work with taxonomies (so please note this solution is based on his work):
function wp_get_term_postcount($term) {
$count = (int) $term->count;
$tax_terms = get_terms($term->taxonomy, array('child_of' => $term->term_id));
foreach ($tax_terms as $tax_term) {
$count += wp_get_term_postcount_recursive($tax_term->term_id);
}
return $count;
}
function wp_get_term_postcount_recursive($term_id, $excludes = array()) {
$count = 0;
foreach ($tax_terms as $tax_term) {
$tax_term_terms = get_terms($tax_term->name, array(
'child_of' => $tax_term->term_id,
'exclude' => $excludes,
));
$count += $tax_term->count;
$excludes[] = $tax_term->term_id;
$count += wp_get_term_postcount_recursive($tax_term->term_id, $excludes);
}
return $count;
}
The recursive function is there to prevent double counting of childs. You can add those two functions inside functions.php.
Then update your code to use it:
$categories = get_terms($taxonomy, $args);
foreach($categories as $categ) {
print $categ->name.' / 'wp_get_term_postcount($categ);
}
I need to show list of all nodes using EntityFieldQuery() with pagination. And i have applied all condition to get my result and it is working fine. But some how pagination not working. I search on google and applied all solutions but its not working. I am getting the result but pagination not displaying. Please Help.
Here is my Code.
$output = array();
$query = new EntityFieldQuery();
$query->entityCondition('entity_type','node')
->entityCondition('bundle', 'article')
->pager(1);
$results = $query->execute();
if (isset($results['nodes'])) {
$content = node_view_multiple(node_load_multiple(array_keys($results['nodes'])));
$output = array(
'content'=> $content,
'pager'=> array(
'#markup' => theme('pager'),
'#weight' => 10
)
);
}
print render($output);
Thanks,
Sunil
I think the isset() should be done on 'node' instead of 'nodes'. Same goes for your $content var. Instead of print render($output); use it as the return value. Like return $output;
$query = new EntityFieldQuery();
$query->entityCondition('entity_type','node')
->entityCondition('bundle','article')
->pager(1);
$results = $query->execute();
$output = array();
if (isset($results['node'])) {
$content = node_view_multiple(node_load_multiple(array_keys($results['node'])));
$output = array(
'content'=> $content,
'pager'=> array(
'#markup' => theme('pager'),
'#weight' => 10
)
);
}
return $output;
I have this piece of code to count the number of posts in a custom taxonomy term and its child terms:
function wp_get_postcount($id)
{
$count = 0;
$taxonomy = 'productcategories';
$args = array(
'child_of' => $id
);
$tax_terms = get_terms($taxonomy,$args);
var_dump($tax_terms);
foreach ($tax_terms as $tax_term) {
$count +=$tax_term->count;
}
return $count;
}
The problem is, it returns as a null for the actual term that contains the post, because that has no child terms. I'd like to know if its possible for the query to include the term with the $id and its children as well?
Thank you!
Why not use 'pad_counts' in get_terms.
$terms=get_terms('my_taxonomy',array('pad_counts'=>1));