Ticket #18986: 18986.6.diff
File 18986.6.diff, 4.6 KB (added by , 12 years ago) |
---|
-
wp-includes/taxonomy.php
2449 2449 if ( !empty($taxonomy->update_count_callback) ) { 2450 2450 call_user_func($taxonomy->update_count_callback, $terms, $taxonomy); 2451 2451 } else { 2452 // Default count updater 2453 _update_post_term_count( $terms, $taxonomy ); 2452 $object_types = (array) $taxonomy->object_type; 2453 foreach ( $object_types as &$object_type ) { 2454 if ( 0 === strpos( $object_type, 'attachment:' ) ) 2455 list( $object_type ) = explode( ':', $object_type ); 2456 } 2457 2458 if ( $object_types == array_filter( $object_types, 'post_type_exists' ) ) { 2459 // Only post types are attached to this taxonomy 2460 _update_post_term_count( $terms, $taxonomy ); 2461 } else { 2462 // Default count updater 2463 _update_generic_term_count( $terms, $taxonomy ); 2464 } 2454 2465 } 2455 2466 2456 2467 clean_term_cache($terms, '', false); … … 2842 2853 function _update_post_term_count( $terms, $taxonomy ) { 2843 2854 global $wpdb; 2844 2855 2845 $object_types = is_array($taxonomy->object_type) ? $taxonomy->object_type : array($taxonomy->object_type);2856 $object_types = (array) $taxonomy->object_type; 2846 2857 2847 2858 foreach ( $object_types as &$object_type ) 2848 2859 list( $object_type ) = explode( ':', $object_type ); 2849 2860 2850 2861 $object_types = array_unique( $object_types ); 2851 $object_types = esc_sql($object_types);2852 2862 2863 if ( $check_attachments = array_search( 'attachment', $object_types ) ) 2864 unset( $object_types[ $check_attachments ] ); 2865 $check_attachments = false !== $check_attachments; 2866 2867 if ( $object_types ) 2868 $object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) ); 2869 2853 2870 foreach ( (array) $terms as $term ) { 2871 $count = 0; 2854 2872 2855 2873 // Attachments can be 'inherit' status, we need to base count off the parent's status if so 2856 if ( in_array( 'attachment', $object_types ) ) 2857 $count = $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 IN ('" . implode("', '", $object_types) . "') AND term_taxonomy_id = %d", $term ) ); 2858 elseif ( post_type_exists( $object_type ) ) 2859 $count = $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 ) ); 2860 else 2861 $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) ); 2874 if ( $check_attachments ) 2875 $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 ) ); 2862 2876 2877 if ( $object_types ) 2878 $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 ) ); 2879 2880 2863 2881 do_action( 'edit_term_taxonomy', $term, $taxonomy ); 2864 2882 $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); 2865 2883 do_action( 'edited_term_taxonomy', $term, $taxonomy ); 2866 2884 } 2867 2885 } 2868 2886 2887 /** 2888 * Will update term count based on number of objects. 2889 * 2890 * Default callback for the link_category taxonomy. 2891 * 2892 * @package WordPress 2893 * @subpackage Taxonomy 2894 * @since 3.3.0 2895 * @uses $wpdb 2896 * 2897 * @param array $terms List of Term taxonomy IDs 2898 * @param object $taxonomy Current taxonomy object of terms 2899 */ 2900 function _update_generic_term_count( $terms, $taxonomy ) { 2901 global $wpdb; 2869 2902 2903 foreach ( (array) $terms as $term ) { 2904 $count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term ) ); 2905 2906 do_action( 'edit_term_taxonomy', $term, $taxonomy ); 2907 $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); 2908 do_action( 'edited_term_taxonomy', $term, $taxonomy ); 2909 } 2910 } 2911 2870 2912 /** 2871 2913 * Generates a permalink for a taxonomy term archive. 2872 2914 *