wp_popular_terms_checklist

Retrieve a list of the most popular terms from the specified taxonomy.

Description

(array) wp_popular_terms_checklist( (string) $taxonomy, (int) $default = 0, (int) $number = 10, (bool) $echo = true ); 

If the $echo argument is true then the elements for a list of checkbox '' elements labelled with the names of the selected terms is output. If the $post_ID global isn't empty then the terms associated with that post will be marked as checked.

Returns (array)

List of popular term IDs.

Parameters (4)

0. $taxonomy (string)
Taxonomy to retrieve terms from.
1. $default — Optional. (int)
The default.
2. $number — Optional. (int) => 10
Number of terms to retrieve. Defaults to 10.
3. $echo — Optional. (bool) => true
Optionally output the list as well. Defaults to true.

Usage

  1. if ( !function_exists( 'wp_popular_terms_checklist' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/template.php'; 
  3.  
  4. // Taxonomy to retrieve terms from. 
  5. $taxonomy = ''; 
  6.  
  7. // The default. 
  8. $default = -1; 
  9.  
  10. // Number of terms to retrieve. Defaults to 10. 
  11. $number = 10; 
  12.  
  13. // Optionally output the list as well. Defaults to true. 
  14. $echo = true; 
  15.  
  16. // NOTICE! Understand what this does before running. 
  17. $result = wp_popular_terms_checklist($taxonomy, $default, $number, $echo); 
  18.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/template.php  
  1. function wp_popular_terms_checklist( $taxonomy, $default = 0, $number = 10, $echo = true ) { 
  2. $post = get_post(); 
  3.  
  4. if ( $post && $post->ID ) 
  5. $checked_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields'=>'ids')); 
  6. else 
  7. $checked_terms = array(); 
  8.  
  9. $terms = get_terms( $taxonomy, array( 'orderby' => 'count', 'order' => 'DESC', 'number' => $number, 'hierarchical' => false ) ); 
  10.  
  11. $tax = get_taxonomy($taxonomy); 
  12.  
  13. $popular_ids = array(); 
  14. foreach ( (array) $terms as $term ) { 
  15. $popular_ids[] = $term->term_id; 
  16. if ( !$echo ) // Hack for Ajax use. 
  17. continue; 
  18. $id = "popular-$taxonomy-$term->term_id"; 
  19. $checked = in_array( $term->term_id, $checked_terms ) ? 'checked="checked"' : ''; 
  20. ?> 
  21.  
  22. <li id="<?php echo $id; ?>" class="popular-category"> 
  23. <label class="selectit"> 
  24. <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 ) ); ?> /> 
  25. <?php 
  26. /** This filter is documented in wp-includes/category-template.php */ 
  27. echo esc_html( apply_filters( 'the_category', $term->name ) ); 
  28. ?> 
  29. </label> 
  30. </li> 
  31.  
  32. <?php 
  33. return $popular_ids;