WordPress.org

Support

Support » Plugins and Hacks » Page Tagger » [Resolved] [Plugin: Page Tagger] Bug with custom post types which have tag support

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

  • 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/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Update for the fix, as it turned out 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

    @randomaniac

    Thanks a lot!

    Plugin Author hiddentao

    @randomaniac

    I’ve bundled your patch into 0.3.7. Also, the source code is now on Github too -> https://github.com/hiddentao/page-tagger.

    Ov3rfly

    @ov3rfly

    Version 0.3.7 works fine, thanks for credit.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] [Plugin: Page Tagger] Bug with custom post types which have tag support’ is closed to new replies.
Skip to toolbar