With the wp_update_term_count_now, how can I use the update_count_callback to specify my own count. I need to include counts from another database that the current database category is able to display. The current categories may not have any posts in them, but are able to be viewed showing posts from the same category in another database.
Im not entirely sure how to pass the call back to the wp_update_term_count_now nor what is should contain – does it just need a new count?
I think I’ve worked this out. First of all, you need to define your taxonomy. I’m pulling this code directly from the codex; however, I’ve added one parameter
update_count_callback
. I’ve set this to the cleverly titledmy_update_count_callback
. This just specifies that when a post of typepost
(this will be whatever CPTs you associate the taxonomy with) is added or updated, this function will be executed instead of the normal routine for updating the count. The taxonomy is registered with:That’s the straight forward part. Next, I defined the callback. Note that this callback will take two parameters
terms
(the term ids associated with the CPT) andtaxonomy
(the name of the taxonomy). The normal update function is defined in taxonomy.php around line 2435. If you have a specified callback, it will run that routine and not the normal one. For my function below, I have simply modified the normal code.All you need to do is write your routines for getting your count and then update the count. Note that I left in the two
do_action
calls as the normal function allows for code to be injected here. I think it’s important to leave those in there so that your plugin won’t cause other ones to malfunction.