On this page
wp_popular_terms_checklist( string $taxonomy, int $default_term, int $number = 10, bool $display = true ): int[]
Retrieves a list of the most popular terms from the specified taxonomy.
Description
If the $display argument is true then the elements for a list of checkbox <input> elements labelled with the names of the selected terms is output.
If the $post_ID global is not empty then the terms associated with that post will be marked as checked.
Parameters
$taxonomystring Required-
Taxonomy to retrieve terms from.
$default_termint Optional-
Not used.
$numberint Optional-
Number of terms to retrieve.
Default:
10 $displaybool Optional-
Whether to display the list as well.
Default:
true
Return
int[] Array of popular term IDs.
Source
File: wp-admin/includes/template.php. View all references
function wp_popular_terms_checklist( $taxonomy, $default_term = 0, $number = 10, $display = true ) {
$post = get_post();
if ( $post && $post->ID ) {
$checked_terms = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
} else {
$checked_terms = array();
}
$terms = get_terms(
array(
'taxonomy' => $taxonomy,
'orderby' => 'count',
'order' => 'DESC',
'number' => $number,
'hierarchical' => false,
)
);
$tax = get_taxonomy( $taxonomy );
$popular_ids = array();
foreach ( (array) $terms as $term ) {
$popular_ids[] = $term->term_id;
if ( ! $display ) { // Hack for Ajax use.
continue;
}
$id = "popular-$taxonomy-$term->term_id";
$checked = in_array( $term->term_id, $checked_terms, true ) ? 'checked="checked"' : '';
?>
<li id="<?php echo $id; ?>" class="popular-category">
<label class="selectit">
<input id="in-<?php echo $id; ?>" type="checkbox" <?php echo $checked; ?> value="<?php echo (int) $term->term_id; ?>" <?php disabled( ! current_user_can( $tax->cap->assign_terms ) ); ?> />
<?php
/** This filter is documented in wp-includes/category-template.php */
echo esc_html( apply_filters( 'the_category', $term->name, '', '' ) );
?>
</label>
</li>
<?php
}
return $popular_ids;
}
Hooks
- apply_filters( 'the_category',
string $thelist ,string $separator ,string $parents ) -
Filters the category or list of categories.
Related
Uses
| Uses | Description |
|---|---|
| disabled() wp-includes/general-template.php | Outputs the HTML disabled attribute. |
| wp_get_object_terms() wp-includes/taxonomy.php | Retrieves the terms associated with the given object(s), in the supplied taxonomies. |
| get_terms() wp-includes/taxonomy.php | Retrieves the terms in a given taxonomy or list of taxonomies. |
| current_user_can() wp-includes/capabilities.php | Returns whether the current user has the specified capability. |
| esc_html() wp-includes/formatting.php | Escaping for HTML blocks. |
| get_taxonomy() wp-includes/taxonomy.php | Retrieves the taxonomy object of $taxonomy. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
Used By
| Used By | Description |
|---|---|
| _wp_ajax_add_hierarchical_term() wp-admin/includes/ajax-actions.php | Ajax handler for adding a hierarchical term. |
| link_categories_meta_box() wp-admin/includes/meta-boxes.php | Displays link categories form fields. |
| post_categories_meta_box() wp-admin/includes/meta-boxes.php | Displays post categories form fields. |
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_popular_terms_checklist