How to Create a Custom Post Type for Portfolio in WordPress

Marek J. Jan 31, 2025 Custom Post Types
How do I make a special section on my website to show off my work?
What are the steps to register a custom post type for a portfolio in WordPress using the functions.php file?
Andy answered Jan 31, 2025

Creating a Custom Post Type for Portfolio

Basic Approach

To create a portfolio section in WordPress, you'll need to register a custom post type. This gives you a dedicated area in the admin panel to manage your portfolio items, similar to how you manage posts and pages.

Here's the basic code to register a portfolio custom post type:

This code goes in your theme's functions.php file or in a custom plugin:

function create_portfolio_post_type() {
    $labels = array(
        'name'               => 'Portfolio',
        'singular_name'      => 'Portfolio Item',
        'menu_name'          => 'Portfolio',
        'add_new'           => 'Add New',
        'add_new_item'      => 'Add New Portfolio Item',
        'edit_item'         => 'Edit Portfolio Item',
        'new_item'          => 'New Portfolio Item',
        'view_item'         => 'View Portfolio Item',
        'search_items'      => 'Search Portfolio',
        'not_found'         => 'No portfolio items found',
        'not_found_in_trash'=> 'No portfolio items found in trash'
    );

    $args = array(
        'labels'              => $labels,
        'public'              => true,
        'has_archive'         => true,
        'publicly_queryable'  => true,
        'show_ui'             => true,
        'show_in_menu'        => true,
        'show_in_rest'        => true, // Enable Gutenberg editor
        'supports'            => array('title', 'editor', 'thumbnail', 'excerpt'),
        'menu_icon'           => 'dashicons-portfolio',
        'rewrite'             => array('slug' => 'portfolio')
    );

    register_post_type('portfolio', $args);
}
add_action('init', 'create_portfolio_post_type');

Adding Custom Categories

To organize your portfolio items, you might want to add categories. Here's how to add a custom taxonomy:

function create_portfolio_taxonomy() {
    $labels = array(
        'name'              => 'Portfolio Categories',
        'singular_name'     => 'Portfolio Category',
        'search_items'      => 'Search Categories',
        'all_items'         => 'All Categories',
        'parent_item'       => 'Parent Category',
        'parent_item_colon' => 'Parent Category:',
        'edit_item'         => 'Edit Category',
        'update_item'       => 'Update Category',
        'add_new_item'      => 'Add New Category',
        'menu_name'         => 'Categories'
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_in_rest'      => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array('slug' => 'portfolio-category')
    );

    register_taxonomy('portfolio_category', 'portfolio', $args);
}
add_action('init', 'create_portfolio_taxonomy');

Best Practices

  1. Flush Permalinks: After adding the custom post type, go to Settings > Permalinks and click "Save Changes" to refresh permalink structure.

  2. Template Files: Create these template files in your theme:

    • single-portfolio.php (for individual portfolio items)
    • archive-portfolio.php (for portfolio listing)
  3. Security: Always validate and sanitize data when saving custom fields.

  4. Backup Functions: Consider adding a function to handle portfolio data during theme switching:

function portfolio_activation() {
    create_portfolio_post_type();
    create_portfolio_taxonomy();
    flush_rewrite_rules();
}
register_activation_hook(__FILE__, 'portfolio_activation');

Common Pitfalls

  1. Don't forget to set 'show_in_rest' => true for Gutenberg compatibility
  2. Always check if the post type name is unique
  3. Remember to update permalinks after registration
  4. Don't use reserved terms for post type names

Plugin Alternatives

If you prefer a ready-made solution:

  1. Portfolio Post Type - Simple and lightweight https://wordpress.org/plugins/portfolio-post-type/

  2. Essential Grid - More advanced with layout options https://essential.tplugins.com/

  3. Jetpack Portfolio - If you're already using Jetpack https://jetpack.com/support/custom-content-types/

Additional Tips

  1. Use featured images for portfolio thumbnails
  2. Consider adding custom fields for project details
  3. Test thoroughly with different themes
  4. Plan your URL structure before implementation

Remember to always back up your site before making these changes, and test in a development environment first.