Last active
December 23, 2016 11:21
-
-
Save remcokalf/f3b57ca4e6b528c8337c17aa6826a819 to your computer and use it in GitHub Desktop.
Get padded term count in term search results with SearchWP and Term Archive Priority
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Code in search loop for term item: | |
$tax = $post->term->taxonomy; | |
$termid = $post->term->term_id; | |
$termchildren = get_term_children( $termid, $tax); | |
if ( !empty( $termchildren ) && !is_wp_error( $termchildren ) ){ | |
$termchildren[] = $termid; //add parent to the array of children | |
//get all terms in the taxonomy of this term, but only from parent down, plus add parent | |
$terms = get_terms($tax, array( | |
'include' => $termchildren, //only parent and children, needed for pad_counts | |
'pad_counts' => true | |
) ); | |
$item = null; // can be error | |
foreach($terms as $struct) { | |
if ($termid == $struct->term_id) {//find th current term again to retrieve its padded count | |
$item = $struct; | |
$postcount = $item->count; | |
break; | |
} | |
} | |
} else { | |
$postcount = $post->term->count; //we already had the non-padded count in the post object. | |
} | |
echo $postcount; | |
//https://developer.wordpress.org/reference/functions/_pad_term_counts/ | |
//This says:"Assumes all relevant children are already in the $terms argument." | |
//So we need parent and child terms to calculate pad_counts | |
// Related:http://wordpress.stackexchange.com/questions/48106/category-pad-counts-parent-conflict | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment