WordPress.org

Ready to get started?Download WordPress

Forums

Multiple Custom Post Types? (4 posts)

  1. Buca
    Member
    Posted 3 years ago #

    Is it possible to have more than one custom post-type?

    // Custom Post Types
    
    add_action( 'init', 'create_post_types' ); // Create New Post Type
    
    function create_post_types() {
    
    	$labels = array(
    		'name' => __( 'Books' ),
    		'singular_name' => __( 'Book' ),
    		'add_new' => __( 'Add New Book' ),
    		'add_new_item' => __( 'Add New Book' ),
    		'edit' => __( 'Edit' ),
    		'edit_item' => __( 'Edit Book' ),
    		'new_item' => __( 'New Book' ),
    		'view' => __( 'View Book' ),
    		'view_item' => __( 'View Book' ),
    		'search_items' => __( 'Search Books' ),
    		'not_found' => __( 'No books found' ),
    		'not_found_in_trash' => __( 'No books found in Trash' ),
    		'parent' => __( 'Parent Book' ),
    	);
    
    	$args = array(
    		'labels' => $labels,
    		'public' => true,
    		'publicly_queryable' => true,
    		'show_ui' => true,
    		'query_var' => true,
    		'rewrite' => true,
    		'capability_type' => 'post',
    		'hierarchical' => false,
    		'menu_position' => 20,
    		'supports' => array('title','editor','thumbnail')
    	); 	
    
    	register_post_type( 'books' , $args );
    	flush_rewrite_rules( false );
    }

    Every time I try and add additional post types to my current code, I get all kinds of error messages. Any ideas?

  2. Jeremy Pry
    Member
    Posted 3 years ago #

    Yes, you can certainly have more than one custom post type. You just can't try adding more by the same name. Using your example code, where you create a post type of "books", you can't try creating a second post type by the name of "books".

    Also, there's probably no need for the flush_rewrite_rules() function just by creating a custom post type. According to the register_post_type | Flushing Rewrite on Activation page, this is needed for getting permalinks to work when a plugin is activated. Try removing that function call from your code.

    If you're still having problems, it would be helpful to post the exact error messages you're receiving, as well as more code examples. Just make sure to use the Pastebin for lengthy pieces of code, so that this thread does not become cluttered (never mind the fact that moderators will remove long code anyway).

  3. cruxwireweb
    Member
    Posted 2 years ago #

    I have this same questions - how to properly register multiple custom post types. Does anyone have some sample code?

  4. Jeremy Pry
    Member
    Posted 2 years ago #

    @Cruxwireweb: Please start your own new thread, instead of posting a new message to an old thread.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags