Tagged: custom taxonomy, update_count_callback
- This topic has 0 replies, 1 voice, and was last updated 6 years, 3 months ago by
justin.
Viewing 0 reply threads
-
AuthorPosts
-
-
October 26, 2015 at 4:01 pm #4749
justinMemberI was having issues fixing the taxonomy counts for my custom post types that did not use publish as a state. So I added the following ‘update_count_callback’ function.
It works well except for the fact it prevents me from changing any of my content inside of those custom post types. Do you have any suggestions on how to fix the taxonomy count and still allow updating meta data inside posts.
function piklist_demo_tax_count_callback( $terms, $taxonomy ) { global $wpdb, $post; if ( is_array( $post )){ $posttype =$post['post_type']; }else{ $posttype =$post->post_type; } switch($posttype){ case 'post_mission_trip': $status = 'public'; break; case 'post_jobs': $status = 'open'; break; } $object_types = (array) $taxonomy->object_type; foreach ( $object_types as &$object_type ){ list( $object_type ) = explode( ':', $object_type ); } $object_types = array_unique( $object_types ); if ( false !== ( $check_attachments = array_search( 'attachment', $object_types ) ) ) { unset( $object_types[ $check_attachments ] ); $check_attachments = true; } if ( $object_types ){ $object_types = esc_sql( array_filter( $object_types, 'post_type_exists' ) ); } foreach ( (array) $terms as $term ) { $count = 0; // Attachments can be 'inherit' status, we need to base count off the parent's status if so. if ( $check_attachments ){ $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 = %s OR ( post_status = 'inherit' AND post_parent > 0 AND ( SELECT post_status FROM $wpdb->posts WHERE ID = p1.post_parent ) = %s ) ) AND post_type = 'attachment' AND term_taxonomy_id = %d", $status, $status, $term ) ); } if ( $object_types ){ $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 = %s AND post_type IN ('" . implode("', '", $object_types ) . "') AND term_taxonomy_id = %d", $status, $term ) ); } /** This action is documented in wp-includes/taxonomy.php */ do_action( 'edit_term_taxonomy', $term, $taxonomy->name ); $wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) ); /** This action is documented in wp-includes/taxonomy.php */ do_action( 'edited_term_taxonomy', $term, $taxonomy->name ); } }Here is a link to my file that creates the custom post_types, taxonomy, and the code above. https://drive.google.com/open?id=0BwbidsNuwyPoVEJodHdzeGZkaFU
-
-
AuthorPosts
Viewing 0 reply threads
- You must be logged in to reply to this topic.