• I’m not sure if this is a bug in core or if I’m doing something wrong.

    I have some code which generates a post-type called “Books” and I’m adding support for the post_tag taxonomy via register_taxonomy_for_object_type().

    <?php
    
    function test() {
    	$args = array(
    		'public' => true,
    		'label'  => 'Books',
    	);
    	register_post_type( 'book', $args );
    
    	register_taxonomy_for_object_type( 'post_tag', 'book' );
    }
    add_action( 'init', 'test' );

    There are three posts in the book post-type:
    https://uploads.ryanhellyer.net/random/2014/10/books-tags.png

    Viewing the tags in the book post-type shows up all of the tags, including those from the posts post-type (note that the “Asten” tag indicates it has one post):
    https://uploads.ryanhellyer.net/random/2014/10/books-tag.png

    If I click on the Asten post tag, it displays no posts at all.
    https://uploads.ryanhellyer.net/random/2014/10/books-tag-good.png

    If I go to the posts post-type, the Asten post tag does show a post as expected:
    https://uploads.ryanhellyer.net/random/2014/10/posts-tag.png

    Question: Why does the “Asten” post tag display “1” when I’m on the book post-type? There is no post for the book post-type with a tag of “Asten” and so this should surely say “0” right? Clicking something which indicates there is one post, but it displaying zero does not make sense to me.

Viewing 5 replies - 1 through 5 (of 5 total)
  • John Blackbourn

    (@johnbillion)

    WordPress Core Developer

    This is a known issue in core. The root cause is that the count is simply the value of the count column in the term_taxonomy table (a cached value for performance reasons), which doesn’t account for differing post types (or indeed, differing object types).

    I can’t immediately find an open ticket on Trac for the issue, but there are several closed ones closely related: #14084, #17209, #19192. Also #22558.

    My understanding is that the post count for a shared taxonomy doesn’t distinguish between post types, just the total number of posts. If you really want separate taxonomies for your custom post type, why not create a custom one?

    Obviously was still typing while John posted his reply ??

    Thread Starter Ryan Hellyer

    (@ryanhellyer)

    Thanks!

    If you really want separate taxonomies for your custom post type, why not create a custom one?

    Because there are some uses in sharing the same taxonomy. For example, you could have a tag cloud across multiple post-types.

    Based on what John said, it looks like this won’t work very well though, so I’ll either need to split them up or deal with some weirdness I guess.

    Getting rid of default tags and registering my own custom tax’. The first two bits of code are done on the “init” hook.

    //IMPORTANT de-register the default post tag taxonomy
    	register_taxonomy( 'post_tag', array() );
    
    	//register our new post tag taxonomy
    	$custom_tag_args = array(
    		'label' => 'My Tags',
    		'public' => TRUE,
    		'hierarchical' => FALSE,
    		'rewrite' => array(
    			'slug' => 'tag'
    			)
    		);
    	register_taxonomy( 'my_tags', NULL, $custom_tag_args );

    Attaching the new tax’ to certain post types

    //register to both posts and press releases
    	register_taxonomy_for_object_type( 'my_tags', 'press_release' );
    	register_taxonomy_for_object_type( 'my_tags', 'post' );

    Adding a new column I can modify and removing the default column. I did this because I couldn’t find a hook for the default count column.

    function rd_custom_column( $column )
    {
    	//error_log( print_r( $column, 1 ) );
    
    	//add our new post_count
    	$column[ 'post_count' ] = 'Post Count';
    
    	//remove the old post count
    	unset( $column[ 'posts' ] );
    
    	return $column;
    }
    add_filter( 'manage_edit-my_tags_columns', 'rd_custom_column', 10, 1 );

    This code gets the real post count and makes the link.

    function rd_filter_custom_taxonomy_columns( $return, $column, $term_id )
    {
    
    	//coloumn name = post_count
    	if( $column != 'post_count' )
    		return $return;
    
    	//get the current screen for additional information
    	$screen = get_current_screen();
    
    	$post_type = $screen->post_type;
    	$taxonomy = $screen->taxonomy;
    
    	$query = "SELECT COUNT( posts.ID ) FROM wp_posts posts INNER JOIN wp_term_relationships rel ON posts.ID = rel.object_id INNER JOIN wp_term_taxonomy tax ON rel.term_taxonomy_id = tax.term_taxonomy_id WHERE tax.term_id = " . $term_id . " AND posts.post_type = '" . $post_type . "'";
    
    	global $wpdb;
    
    	$count = $wpdb->get_results( $query, 'ARRAY_N' );
    
    	$term_object = get_term_by( 'id', $term_id, 'my_tags' );
    
    	//var_dump( $term_object );
    
    	$args = array( 'taxonomy' => $taxonomy, 'term' => $term_object->slug, 'post_type' => $post_type );
    
    	//build the link with query string
    	echo "<a href='" . esc_url ( add_query_arg( $args, 'edit.php' ) ) . "'>" . $count[0][0] . "</a>";
    
    }
    add_filter( 'manage_my_tags_custom_column', 'rd_filter_custom_taxonomy_columns', 10, 3 );

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Taxonomy bug’ is closed to new replies.