Forums

Custom Taxonomies Filter in manage posts screen (3 posts)

  1. toms.umairkhan
    Member
    Posted 1 year ago #

    HI All,

    I googled and search for my answer but couldn't found it. I hope some one will help me here.

    Well, scenario is that i have created a custom post type and registered with it a custom taxonomy. Now problem is that on manage post screen of custom post type i am unable to get the select box used to filter the posts through taxonomy / category, which in default post is used as category filter.

    Thanks

  2. prfbst
    Member
    Posted 1 year ago #

    Im searching for the same do any of de wp gurus have a answer for us?:)

  3. prfbst
    Member
    Posted 1 year ago #

    This adds a new colomn with the taxonomy name and link:

    register_post_type( 'book', array(
    	'labels' => array(
    		'name' => 'Books',
    		'singular_name' => 'Book',
    		'add_new' => 'Add New',
    		'add_new_item' => 'Add New Book',
    		'edit_item' => 'Edit book',
    		'edit' => 'Edit',
    		'new_item' => 'New Book',
    		'view_item' => 'View Book',
    		'search_items' => 'Search Books',
    		'not_found' => 'No books found',
    		'not_found_in_trash' => 'No books found in Trash',
    		'view' => 'View Book'
    	),
    	'public' => true,
    	'capability_type' => 'post',
    	'hierarchical' => false,
    	'rewrite' => true,
    	'query_var' => true,
    	'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'custom-fields', 'comments' ),
    	'taxonomies' => array('category','post_tag')
    	)
    );
    register_taxonomy(
    	'book_category',
    	'book',
    	array(
    		'hierarchical' => true,
    		'label' => 'Categoriƫn',
    		'public' => true,
    		'query_var' => true,
    		'show_tagcloud' => true,
    		'rewrite' => Array(
    			'slug' => 'book_category')
    		)
    );
    function add_new_columns($defaults) {
        $defaults['book_cats'] = __('Categoriƫn');
        return $defaults;
    }
    function add_column_data( $column_name, $post_id ) {
    	if( $column_name == 'book_cats' ) {
    		$_posttype 	= 'book';
    		$_taxonomy 	= 'book_category';
    		$terms 		= get_the_terms( $post_id, $_taxonomy );
    		if ( !empty( $terms ) ) {
    			$out = array();
    			foreach ( $terms as $c )
    				$_taxonomy_title = esc_html(sanitize_term_field('name', $c->name, $c->term_id, 'category', 'display'));
    				$out[] = "<a href='edit.php?book_category=$_taxonomy_title&post_type=$_posttype'>$_taxonomy_title</a>";
    			echo join( ', ', $out );
    		}
    		else {
    			_e('Uncategorized');
    		}
    	}
    }
    add_filter( 'manage_book_posts_columns', 'add_new_columns' );
    add_action( 'manage_posts_custom_column', 'add_column_data', 10, 2 );

Topic Closed

This topic has been closed to new replies.

About this Topic