Developing Custom Post Types for WordPress
WordPress natively works with two content types: "Posts" and "Pages." When a site needs Projects, Vacancies, Real Estate, Reviews, or Products—each gets its own post type (CPT). A CPT provides a separate admin section, custom URLs, archive pages, and full control over data structure. Registering a CPT with basic settings takes a few hours; full setup with URLs, custom columns, and search capabilities takes 1–2 days.
Registration via register_post_type
add_action('init', function () {
register_post_type('project', [
'labels' => [
'name' => 'Projects',
'singular_name' => 'Project',
'add_new' => 'Add Project',
'add_new_item' => 'New Project',
'edit_item' => 'Edit Project',
'new_item' => 'New Project',
'view_item' => 'View Project',
'search_items' => 'Search Projects',
'not_found' => 'No projects found',
'not_found_in_trash' => 'Trash is empty',
'menu_name' => 'Projects',
],
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => ['slug' => 'projects', 'with_front' => false],
'capability_type' => 'post',
'has_archive' => 'projects',
'hierarchical' => false,
'menu_position' => 5,
'menu_icon' => 'dashicons-portfolio',
'supports' => ['title', 'editor', 'thumbnail', 'excerpt', 'custom-fields'],
'show_in_rest' => true, // required for Gutenberg
'rest_base' => 'projects',
]);
});
has_archive => 'projects' creates an archive page at /projects/ containing all posts of this type. show_in_rest => true makes the type available via REST API and in the Gutenberg editor.
After registering a new CPT, update rewrite rules: in /wp-admin → Settings → Permalinks click "Save," or programmatically:
register_activation_hook(__FILE__, function () {
add_action('init', 'register_project_cpt');
flush_rewrite_rules();
});
Custom Columns in Post List
By default, the project list shows only title and date. Add needed columns:
// Register columns
add_filter('manage_project_posts_columns', function (array $columns): array {
$new = [];
foreach ($columns as $key => $title) {
$new[$key] = $title;
if ($key === 'title') {
$new['client'] = 'Client';
$new['year'] = 'Year';
$new['featured'] = 'Featured';
}
}
unset($new['comments']);
return $new;
});
// Display column data
add_action('manage_project_posts_custom_column', function (string $column, int $post_id): void {
switch ($column) {
case 'client':
echo esc_html(get_post_meta($post_id, '_project_client', true) ?: '—');
break;
case 'year':
echo esc_html(get_post_meta($post_id, '_project_year', true) ?: '—');
break;
case 'featured':
$is_featured = get_post_meta($post_id, '_project_featured', true);
echo $is_featured ? '⭐' : '—';
break;
}
}, 10, 2);
// Make column sortable
add_filter('manage_edit-project_sortable_columns', function (array $columns): array {
$columns['year'] = 'year';
return $columns;
});
add_action('pre_get_posts', function (WP_Query $query): void {
if (!is_admin() || !$query->is_main_query()) return;
if ($query->get('orderby') === 'year') {
$query->set('meta_key', '_project_year');
$query->set('orderby', 'meta_value_num');
}
});
Taxonomy Filter in List
add_action('restrict_manage_posts', function (string $post_type): void {
if ($post_type !== 'project') return;
$taxonomy = get_taxonomy('project_category');
wp_dropdown_categories([
'taxonomy' => 'project_category',
'name' => 'project_category',
'show_option_all' => 'All Categories',
'selected' => $_GET['project_category'] ?? 0,
'value_field' => 'slug',
'hierarchical' => true,
]);
});
Including in WordPress Search
By default, WordPress search doesn't include custom CPTs:
add_action('pre_get_posts', function (WP_Query $query): void {
if ($query->is_search() && !is_admin() && $query->is_main_query()) {
$post_types = $query->get('post_type') ?: ['post'];
if (!is_array($post_types)) {
$post_types = [$post_types];
}
$query->set('post_type', array_merge($post_types, ['project', 'vacancy']));
}
});
Related Posts Between CPTs
The standard way to link two posts is to store the ID in post meta:
// Save relationships
update_post_meta($project_id, '_related_cases', array_map('absint', $case_ids));
// Get related cases
$case_ids = get_post_meta($project_id, '_related_cases', true);
if (!empty($case_ids)) {
$cases = get_posts([
'post_type' => 'case',
'post__in' => $case_ids,
'orderby' => 'post__in',
'posts_per_page' => -1,
]);
}
For complex two-way relationships (many-to-many), use the Posts 2 Posts plugin or a custom junction table.
CPT vs Taxonomy vs Custom Field
A common mistake is creating a CPT where another tool would be better:
- CPT is needed when an entity has its own pages, archive, and content editor
- Taxonomy is for grouping/filtering (category, tag)
- Custom field is for storing an attribute of an existing post (price, year, address)
Registering a CPT with zero templates and no real content is a waste of structure. If the "type" isn't needed on the frontend—it's probably just a taxonomy for an existing CPT.







