Skip to content

Commit

Permalink
Count only published posts when updating term counts. Fire term count…
Browse files Browse the repository at this point in the history
… updates on transition_post_status. Props joehoyle. see #17548

git-svn-id: http://svn.automattic.com/wordpress/trunk@18932 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
ryan committed Oct 10, 2011
1 parent 4dfdd9e commit 0370bda
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
1 change: 1 addition & 0 deletions wp-includes/default-filters.php
Expand Up @@ -254,6 +254,7 @@
add_action( 'publish_post', '_publish_post_hook', 5, 1 );
add_action( 'save_post', '_save_post_hook', 5, 2 );
add_action( 'transition_post_status', '_transition_post_status', 5, 3 );
add_action( 'transition_post_status', '_update_term_count_on_transition_post_status', 10, 3 );
add_action( 'comment_form', 'wp_comment_form_unfiltered_html_nonce' );
add_action( 'wp_scheduled_delete', 'wp_scheduled_delete' );
add_action( 'admin_init', 'send_frame_options_header', 10, 0 );
Expand Down
22 changes: 16 additions & 6 deletions wp-includes/post.php
Expand Up @@ -2731,12 +2731,6 @@ function wp_publish_post($post_id) {
$post->post_status = 'publish';
wp_transition_post_status('publish', $old_status, $post);

// Update counts for the post's terms.
foreach ( (array) get_object_taxonomies('post') as $taxonomy ) {
$tt_ids = wp_get_object_terms($post_id, $taxonomy, array('fields' => 'tt_ids'));
wp_update_term_count($tt_ids, $taxonomy);
}

do_action('edit_post', $post_id, $post);
do_action('save_post', $post_id, $post);
do_action('wp_insert_post', $post_id, $post);
Expand Down Expand Up @@ -5312,4 +5306,20 @@ function _post_format_wp_get_object_terms( $terms ) {
}
add_filter( 'wp_get_object_terms', '_post_format_wp_get_object_terms' );

/**
* Update the custom taxonomies' term counts when a post's status is changed. For example, default posts term counts (for custom taxonomies) don't include private / draft posts.
*
* @access private
* @param string $new_status
* @param string $old_status
* @param object $post
* @since 3.3.0
*/
function _update_term_count_on_transition_post_status( $new_status, $old_status, $post ) {
// Update counts for the post's terms.
foreach ( (array) get_object_taxonomies( $post->post_type ) as $taxonomy ) {
$tt_ids = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'tt_ids' ) );
wp_update_term_count( $tt_ids, $taxonomy );
}
}
?>
21 changes: 13 additions & 8 deletions wp-includes/taxonomy.php
Expand Up @@ -2447,13 +2447,7 @@ function wp_update_term_count_now( $terms, $taxonomy ) {
call_user_func($taxonomy->update_count_callback, $terms, $taxonomy);
} else {
// Default count updater
foreach ( (array) $terms as $term) {
$count = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships WHERE term_taxonomy_id = %d", $term) );
do_action( 'edit_term_taxonomy', $term, $taxonomy );
$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
do_action( 'edited_term_taxonomy', $term, $taxonomy );
}

_update_post_term_count( $terms, $taxonomy );
}

clean_term_cache($terms, '', false);
Expand Down Expand Up @@ -2846,10 +2840,21 @@ function _update_post_term_count( $terms, $taxonomy ) {
global $wpdb;

$object_types = is_array($taxonomy->object_type) ? $taxonomy->object_type : array($taxonomy->object_type);

foreach ( $object_types as &$object_type )
list( $object_type ) = explode( ':', $object_type );

$object_types = array_unique( $object_types );
$object_types = esc_sql($object_types);

foreach ( (array) $terms as $term ) {
$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 ) );

// Attachments can be 'inherit' status, we need to base count off the parent's staus if so
if ( in_array( 'attachment', $object_types ) )
$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 ) );
else
$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 ) );

do_action( 'edit_term_taxonomy', $term, $taxonomy );
$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
do_action( 'edited_term_taxonomy', $term, $taxonomy );
Expand Down

0 comments on commit 0370bda

Please sign in to comment.