Skip to content

Instantly share code, notes, and snippets.

@nacin
Last active December 11, 2015 03:08
Show Gist options
  • Save nacin/4535206 to your computer and use it in GitHub Desktop.
Save nacin/4535206 to your computer and use it in GitHub Desktop.
<?php
/*
* Ensure that the post_tag taxonomy uses our own special counting logic.
*
* Normally, this would simply be specified by register_taxonomy() with
* the update_count_callback flag. Since we use the core tag taxonomy,
* let's just continue to hijack it.
*/
add_action( 'init', function() {
$GLOBALS['wp_taxonomies']['post_tag']->update_count_callback = 'jquery_update_plugin_tag_count';
} );
/**
* Mostly just _update_post_term_count(), tweaked for post_parent = 0.
*/
function jquery_update_plugin_tag_count( $terms, $taxonomy ) {
global $wpdb;
foreach ( (array) $terms as $term ) {
$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 = 'jquery-plugin' AND post_parent = 0 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 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment