• Resolved Guido

    (@guido07111975)


    Hi,

    I have a plugin that uses a custom post type called ‘event’.
    I want to add custom event categories so users can list events by categorie.

    So I should use register_taxonomy for this.

    This default code creates a custom TAG page:

    function my_plugin_taxonomy() {
    	register_taxonomy( 'my_custom_taxonomy', 'event'  );
    }
    add_action( 'init', 'my_plugin_taxonomy' );

    How can I create a custom category page in stead?

    Note: I know how to create a default category page (via register_post_type) but I don’t want to use the default (post) categories.

    Guido

Viewing 9 replies - 1 through 9 (of 9 total)
  • Where you have created your custom post type all you need to do is define categories as a taxonomy. Just add this into your custom post type function, it’s probably already in there with ‘tags’ in the array, just add ‘category’ instead.

    taxonomies' => array('category'),

    Thread Starter Guido

    (@guido07111975)

    Hi,

    Thanks but I know this already, in this case I get my regular post categories too. I don’t want those post categories. I only want (custom) event categories there.

    Guido

    You will need to define a new taxonomy and call it something other than ‘Category’ then as it’s looking at the built in WordPress categories.

    add_action( 'init', 'create_newcat_taxonomy' );
    
    function create_newcat_taxonomy() {
    	register_taxonomy(
    		'newcat',
    		'post',
    		array(
    			'label' => 'New category',
    			'hierarchical' => true,
    		)
    	);
    }

    Then in your taxonomy array add in:

    'taxonomies' => array('newcat'),

    Thread Starter Guido

    (@guido07111975)

    Nice, that’s what I’m looking for for many hours now…
    Did not know about the part changing the ‘Category’ into my own..

    Thanks!

    Guido

    No worries, glad I could help!

    Thread Starter Guido

    (@guido07111975)

    Hi,

    Unfortunately another question, I now have the categories in admin but when I list my events in frontend using WP_query it does not list any events when adding ‘cat’ => 17 (my event category has ID 17).

    new WP_Query( array('post_type' => 'event',  'cat' => 17 ) );

    When removing ‘cat’ => 17 all events are listed.
    Am I missing something again?

    Guido

    Moderator bcworkz

    (@bcworkz)

    Hi Guido,

    I hope you don’t mind me jumping in even though you found someone else to talk to ??

    You can no longer use ‘cat’ for your category, that’s only for the original category. The new query var to use instead of ‘cat’ is your taxonomy slug, or you can specify a shorter one with the ‘query_var’ argument when you register your taxonomy.

    Thread Starter Guido

    (@guido07111975)

    Hi again,

    Do not mind at all ??

    So in previous example from graphicscove the slug will be ‘newcat’?
    And with rewrite I can change that into something else:

    'rewrite' => array( 'slug' => 'event_cat' ),

    Guido

    Thread Starter Guido

    (@guido07111975)

    Hi again again,

    I decided to use the taxonomy slug (newcat).
    So the rewrite part is no longer relevant.

    Thanks guys! This is the plugin where I needed this info for.

    Guido

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Add custom post type categories’ is closed to new replies.