Skip to content

Instantly share code, notes, and snippets.

@drewbaker
Last active June 2, 2022 00:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drewbaker/8857aeb113aa972121948f7246e5a287 to your computer and use it in GitHub Desktop.
Save drewbaker/8857aeb113aa972121948f7246e5a287 to your computer and use it in GitHub Desktop.
Create custom post types for different regions
<?php
/*
* Setup Custom Post Types
*/
function create_custom_posts() {
// Create a loop of all the post types we need
$types = array(
[
'name' => 'US Region',
'slug' => 'us',
'single_name' => 'UsPage',
],
[
'name' => 'UK Region',
'slug' => 'uk',
'single_name' => 'UkPage',
],
[
'name' => 'JP Region',
'slug' => 'jp',
'single_name' => 'JpPage',
],
);
// Loop through above types and setup
foreach($types as $index => $type) {
// Setup CPT
$labels = array(
'name' => $type['name'],
'all_items' => 'All '.$type['name'].' Pages',
'singular_name' => $type['name'].' Page',
'add_new' => 'Add New '.$type['name'].' Page',
'add_new_item' => 'Add New '.$type['name'].' Page',
'edit' => 'Edit',
'edit_item' => 'Edit '.$type['name'].' Page',
'new_item' => 'New Page',
'view' => 'View Page',
'view_item' => 'View Pages',
'search_items' => 'Search Pages',
'not_found' => 'No pages found',
'not_found_in_trash' => 'No pages found in Trash'
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'show_in_menu' => true,
'query_var' => true,
'capability_type' => 'page',
'has_archive' => false,
'menu_icon' => 'dashicons-building',
'hierarchical' => true,
'menu_position' => 20+$index,
'supports' => array(
'title',
'editor',
'thumbnail',
'page-attributes',
'excerpt',
'revisions',
'author',
'custom-fields'
),
'rewrite' => array(
'slug' => $type['slug'],
'with_front' => false,
),
'taxonomies' => array('post_tag'),
'show_in_graphql' => true,
'show_in_rest' => true,
'graphql_single_name' => $type['single_name'],
'graphql_plural_name' => $type['single_name'] . 's'
);
register_post_type( $type['slug'], $args );
}
}
add_action( 'init', 'create_custom_posts', 5 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment