WordPress.org

Forums

Page Tagger
[resolved] Bug with custom post types which have tag support (5 posts)

  1. Ov3rfly
    Member
    Posted 3 years ago #

    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/

  2. Ov3rfly
    Member
    Posted 3 years ago #

    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;
    	}
  3. hiddentao
    Member
    Plugin Author

    Posted 3 years ago #

    Thanks a lot!

  4. hiddentao
    Member
    Plugin Author

    Posted 3 years ago #

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

  5. Ov3rfly
    Member
    Posted 3 years ago #

    Version 0.3.7 works fine, thanks for credit.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Page Tagger
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic