get_terms_to_edit

Get comma-separated list of terms available to edit for the given post ID.

Description

(string|bool|WP_Error) get_terms_to_edit( (int) $post_id, (string) $taxonomy = 'post_tag' ); 

Returns (string|bool|WP_Error)

Parameters (2)

0. $post_id (int)
The post id.
1. $taxonomy — Optional. (string) => 'post_tag'
The taxonomy for which to retrieve terms. Default post_tag..

Usage

  1. if ( !function_exists( 'get_terms_to_edit' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/taxonomy.php'; 
  3.  
  4. // The post id. 
  5. $post_id = -1; 
  6.  
  7. // Optional. The taxonomy for which to retrieve terms. Default 'post_tag'. 
  8. $taxonomy = 'post_tag'; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_terms_to_edit($post_id, $taxonomy); 
  12.  

Defined (1)

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

/wp-admin/includes/taxonomy.php  
  1. function get_terms_to_edit( $post_id, $taxonomy = 'post_tag' ) { 
  2. $post_id = (int) $post_id; 
  3. if ( !$post_id ) 
  4. return false; 
  5.  
  6. $terms = get_object_term_cache( $post_id, $taxonomy ); 
  7. if ( false === $terms ) { 
  8. $terms = wp_get_object_terms( $post_id, $taxonomy ); 
  9. wp_cache_add( $post_id, wp_list_pluck( $terms, 'term_id' ), $taxonomy . '_relationships' ); 
  10.  
  11. if ( ! $terms ) { 
  12. return false; 
  13. if ( is_wp_error( $terms ) ) { 
  14. return $terms; 
  15. $term_names = array(); 
  16. foreach ( $terms as $term ) { 
  17. $term_names[] = $term->name; 
  18.  
  19. $terms_to_edit = esc_attr( join( ', ', $term_names ) ); 
  20.  
  21. /** 
  22. * Filters the comma-separated list of terms available to edit. 
  23. * @since 2.8.0 
  24. * @see get_terms_to_edit() 
  25. * @param array $terms_to_edit An array of terms. 
  26. * @param string $taxonomy The taxonomy for which to retrieve terms. Default 'post_tag'. 
  27. */ 
  28. $terms_to_edit = apply_filters( 'terms_to_edit', $terms_to_edit, $taxonomy ); 
  29.  
  30. return $terms_to_edit;