Title: [Plugin: Page Tagger] Bug with custom post types which have tag support
Last modified: August 20, 2016

---

# [Plugin: Page Tagger] Bug with custom post types which have tag support

 *  Resolved [Ov3rfly](https://wordpress.org/support/users/ov3rfly/)
 * (@ov3rfly)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/plugin-page-tagger/)
 * This plugin works fine – if you only use posts and pages.
 * If you also use a custom post type with tag-support (taxonomy: `post_tag`), the
   plugin breaks the process of updating the correct count of tags in database so
   other functionalities, e.g. Tag Clouds, get unexpected and wrong results.
 * The plugin overrides the default tag count update callback `_update_post_term_count`
   of taxonomy `post_tag` with a hardcoded sql-statement with only post types `post`
   and `page` so any other post type will not be updated.
 * Fix for Page Tagger 0.3.6, at end of file _page-tagger-class.php_
 * Old, only 2 hardcoded post_types:
 *     ```
       function _update_post_term_count( $terms )
       	{
       		global $wpdb;
       		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_type = 'post' OR post_type = 'page') AND term_taxonomy_id = %d", $term ) );
       			$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
       		}
       	}
       ```
   
 * New, add also custom post types to SELECT statement:
 *     ```
       function _update_post_term_count( $terms )
       	{
       		global $wpdb;
       		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_type = 'post' OR post_type = 'page'" . $this->_get_custom_post_types_sql() . ") AND term_taxonomy_id = %d", $term ) );
       			$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
       		}
       	}
       /* Hack for custom post types support */
       function _get_custom_post_types_sql() {
       		$sql = '';
       		$cpt_arr = array();
       		$args = array(
       			'public'   => true,
       			'_builtin' => false,
       			'taxonomies' => array('post_tag') // taxonomies actually not used, bug wp 3.2.1
       		);
       		$post_types = get_post_types($args, 'names');
       		foreach ($post_types as $post_type ) {
       			$cpt_arr[] = "post_type = '$post_type'";
       		}
       		if ($cpt_arr) {
       			$sql = ' OR ' . implode(' OR ', $cpt_arr);
       		}
       		return $sql;
       	}
       ```
   
 * [http://wordpress.org/extend/plugins/page-tagger/](http://wordpress.org/extend/plugins/page-tagger/)

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Thread Starter [Ov3rfly](https://wordpress.org/support/users/ov3rfly/)
 * (@ov3rfly)
 * [14 years, 5 months ago](https://wordpress.org/support/topic/plugin-page-tagger/#post-2382844)
 * Update for the fix, as [it turned out](http://core.trac.wordpress.org/ticket/19148)
   that `taxonomies` parameter is not supported for `get_post_types()`, it was a
   documentation error in codex.
 *     ```
       function _update_post_term_count( $terms )
       	{
       		global $wpdb;
       		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_type = 'post' OR post_type = 'page'" . $this->_get_custom_post_types_sql() . ") AND term_taxonomy_id = %d", $term ) );
       			$wpdb->update( $wpdb->term_taxonomy, compact( 'count' ), array( 'term_taxonomy_id' => $term ) );
       		}
       	}
       /* Hack for custom post types support */
       function _get_custom_post_types_sql() {
       		$sql = '';
       		$cpt_arr = array();
       		$args = array(
       			'public'   => true,
       			'_builtin' => false
       		);
       		$post_types = get_post_types($args, 'names');
       		foreach ($post_types as $post_type ) {
       			if (is_object_in_taxonomy($post_type, 'post_tag')) {
       				$cpt_arr[] = "post_type = '$post_type'";
       			}
       		}
       		if ($cpt_arr) {
       			$sql = ' OR ' . implode(' OR ', $cpt_arr);
       		}
       		return $sql;
       	}
       ```
   
 *  Plugin Author [hiddentao](https://wordpress.org/support/users/randomaniac/)
 * (@randomaniac)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-page-tagger/#post-2383019)
 * Thanks a lot!
 *  Plugin Author [hiddentao](https://wordpress.org/support/users/randomaniac/)
 * (@randomaniac)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-page-tagger/#post-2383026)
 * I’ve bundled your patch into 0.3.7. Also, the source code is now on Github too-
   > [https://github.com/hiddentao/page-tagger](https://github.com/hiddentao/page-tagger).
 *  Thread Starter [Ov3rfly](https://wordpress.org/support/users/ov3rfly/)
 * (@ov3rfly)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-page-tagger/#post-2383027)
 * Version 0.3.7 works fine, thanks for credit.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘[Plugin: Page Tagger] Bug with custom post types which have tag support’
is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/page-tagger.svg)
 * [Page Tagger](https://wordpress.org/plugins/page-tagger/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/page-tagger/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/page-tagger/)
 * [Active Topics](https://wordpress.org/support/plugin/page-tagger/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/page-tagger/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/page-tagger/reviews/)

## Tags

 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)

 * 4 replies
 * 2 participants
 * Last reply from: [Ov3rfly](https://wordpress.org/support/users/ov3rfly/)
 * Last activity: [14 years, 4 months ago](https://wordpress.org/support/topic/plugin-page-tagger/#post-2383027)
 * Status: resolved