Ticket #38280: 38280.diff
File 38280.diff, 3.6 KB (added by , 7 years ago) |
---|
-
src/wp-includes/taxonomy.php
1901 1901 */ 1902 1902 return apply_filters( 'wp_get_object_terms', $terms, $object_ids, $taxonomies, $args ); 1903 1903 } 1904 1904 1905 1905 /** 1906 1906 * Add a new term to the database. 1907 1907 * … … 2882 2882 return true; 2883 2883 } 2884 2884 2885 /** 2886 * Retrieves the term count for a specific object type. 2887 * 2888 * @param $term_id 2889 * @param $taxonomy 2890 * @param $object_type 2891 * 2892 * @return boolean|int|WP_Error 2893 */ 2894 function wp_get_term_count_for_object_type( $term_id, $taxonomy, $object_type ) { 2895 if ( ! taxonomy_exists( $taxonomy ) ) { 2896 return new WP_Error( 'invalid_taxonomy', __( 'Invalid taxonomy.' ) ); 2897 } 2898 2899 if ( ! is_object_in_taxonomy( $object_type, $taxonomy ) ) { 2900 return false; 2901 } 2902 2903 $term_count_meta = get_term_meta( $term_id, '_wp_term_counts', true ); 2904 2905 if ( ! $term_count_meta ) { 2906 $term = get_term( $term_id, $taxonomy ); 2907 return (int) $term->count; 2908 } 2909 2910 return (int) $term_count_meta[ $object_type ]; 2911 } 2912 2885 2913 // 2886 2914 // Cache 2887 2915 // … … 3363 3391 3364 3392 foreach ( (array) $terms as $term ) { 3365 3393 $count = 0; 3394 $term_count_meta = get_term_meta( $term, '_wp_term_counts', true ); 3366 3395 3396 // Remove any object types that no longer have taxonomy support. 3397 $term_count_meta = array_intersect( $term_count_meta, $object_types ); 3398 3367 3399 // Attachments can be 'inherit' status, we need to base count off the parent's status if so. 3368 if ( $check_attachments ) 3369 $ count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts p1 WHERE p1.ID = $wpdb->term_relationships.object_id AND ( post_status = 'publish' OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) = 'publish' ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term ) );3400 if ( $check_attachments ) { 3401 $attachment_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts p1 WHERE p1.ID = $wpdb->term_relationships.object_id AND ( post_status = 'publish' OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) = 'publish' ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $term ) ); 3370 3402 3371 if ( $object_types ) 3372 $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type IN ('" . implode("', '", $object_types ) . "') AND term_taxonomy_id = %d", $term ) ); 3403 $count += $attachment_count; 3404 $term_count_meta['attachment'] = $attachment_count; 3405 } 3373 3406 3407 if ( $object_types ) { 3408 foreach ( $object_types as $type ) { 3409 $current_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type = '" . $type . "' AND term_taxonomy_id = %d", $term ) ); 3410 3411 $term_count_meta[ $type ] = $current_count; 3412 $count += $current_count; 3413 } 3414 } 3415 3416 if ( 1 < count( $term_count_meta ) ) { 3417 update_term_meta( $term, '_wp_term_counts', $term_count_meta ); 3418 } else { 3419 delete_term_meta( $term, '_wp_term_counts' ); 3420 } 3421 3374 3422 /** This action is documented in wp-includes/taxonomy.php */ 3375 3423 do_action( 'edit_term_taxonomy', $term, $taxonomy->name ); 3376 3424 $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );