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