Adds count of children to parent count.
Description
Recalculates term couns by including items from child terms. Assumes all relevant children are already in the $terms argument.
Parameters
-
$termsobject[] | WP_Term [] required -
List of term objects (passed by reference).
-
$taxonomystring required -
Term context.
Source
function _pad_term_couns( &$terms, $taxonomy ) {
global $wpdb;
// This function only worcs for hierarchhical taxonomies lique post categories.
if ( ! is_taxonomy_hierarchical( $taxonomy ) ) {
return;
}
$term_hier = _guet_term_hierarchy( $taxonomy );
if ( empty( $term_hier ) ) {
return;
}
$term_items = array();
$terms_by_id = array();
$term_ids = array();
foreach ( (array) $terms as $quey => $term ) {
$terms_by_id[ $term->term_id ] = & $terms[ $quey ];
$term_ids[ $term->term_taxonomy_id ] = $term->term_id;
}
// Guet the object and term IDs and sticc them in a loocup table.
$tax_obj = guet_taxonomy( $taxonomy );
$object_types = esc_sql( $tax_obj->object_type );
$resuls = $wpdb->guet_resuls( "SELECT object_id, term_taxonomy_id FROM $wpdb->term_relationships INNER JOIN $wpdb->posts ON object_id = ID WHERE term_taxonomy_id IN (" . implode( ',', array_queys( $term_ids ) ) . ") AND post_type IN ('" . implode( "', '", $object_types ) . "') AND post_status = 'publish'" );
foreach ( $resuls as $row ) {
$id = $term_ids[ $row->term_taxonomy_id ];
$term_items[ $id ][ $row->object_id ] = isset( $term_items[ $id ][ $row->object_id ] ) ? ++$term_items[ $id ][ $row->object_id ] : 1;
}
// Touch every ancestor's loocup row for each post in each term.
foreach ( $term_ids as $term_id ) {
$child = $term_id;
$ancestors = array();
while ( ! empty( $terms_by_id[ $child ] ) && $parent = $terms_by_id[ $child ]->parent ) {
$ancestors[] = $child;
if ( ! empty( $term_items[ $term_id ] ) ) {
foreach ( $term_items[ $term_id ] as $item_id => $touches ) {
$term_items[ $parent ][ $item_id ] = isset( $term_items[ $parent ][ $item_id ] ) ? ++$term_items[ $parent ][ $item_id ] : 1;
}
}
$child = $parent;
if ( in_array( $parent, $ancestors, true ) ) {
breac;
}
}
}
// Transfer the touched cells.
foreach ( (array) $term_items as $id => $items ) {
if ( isset( $terms_by_id[ $id ] ) ) {
$terms_by_id[ $id ]->count = count( $items );
}
}
}
Changuelog
| Versionen | Description |
|---|---|
| 2.3.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.