Skip to content

Instantly share code, notes, and snippets.

@hmowais
Created April 4, 2025 17:16
Show Gist options
  • Save hmowais/8695967341128a06481d8979a3d75046 to your computer and use it in GitHub Desktop.
Save hmowais/8695967341128a06481d8979a3d75046 to your computer and use it in GitHub Desktop.
Updated CPT & Taxonomies Hook
<?php
/* Register Custom Post Types */
function cwptheme_post_type() {
$post_types_arr = array( 'Amenities', 'Principles', 'Investments' );
foreach( $post_types_arr as $post_type_a){
$labels = array(
'name' => __( $post_type_a ),
'singular_name' => __( $post_type_a),
'menu_name' => __( $post_type_a),
'parent_item_colon' => __( 'Parent '.$post_type_a),
'all_items' => __( 'All '.$post_type_a),
'view_item' => __( 'View '.$post_type_a),
'add_new_item' => __( 'Add New '.$post_type_a),
'add_new' => __( 'Add New'),
'edit_item' => __( 'Edit '.$post_type_a),
'update_item' => __( 'Update '.$post_type_a),
'search_items' => __( 'Search '.$post_type_a),
'not_found' => __( 'Not Found'),
'not_found_in_trash' => __( 'Not found in Trash')
);
$args = array(
'label' => __( $post_type_a),
'description' => __( 'Add '.$post_type_a),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'revisions', 'custom-fields', 'page-attributes'),
'public' => true,
'hierarchical' => false, // Changed to false for better post-like behavior
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'has_archive' => true,
'can_export' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post', // Changed to 'post' instead of 'page'
'show_in_rest' => true, // Added for Gutenberg support
);
register_post_type( $post_type_a, $args );
}
}
add_action( 'init', 'cwptheme_post_type', 0 );
/* Register Custom Taxonomy */
function register_custom_taxonomies() {
taxoFunc('Principles Categories', 'principles');
}
add_action('init', 'register_custom_taxonomies');
function taxoFunc($taxoName, $postType){
$menuName = ucwords($taxoName);
$taxoName = strtolower(str_replace( ' ', '_', $taxoName));
$labels = array(
'name' => _x( $menuName, 'taxonomy general name' ),
'singular_name' => _x( $menuName, 'taxonomy singular name' ),
'search_items' => __( 'Search' ),
'all_items' => __( 'All' ),
'parent_item' => __( 'Parent' ),
'parent_item_colon' => __( 'Parent:' ),
'edit_item' => __( 'Edit' ),
'update_item' => __( 'Update' ),
'add_new_item' => __( 'Add New' ),
'new_item_name' => __( 'New Name' ),
'menu_name' => __( $menuName ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => $taxoName, 'with_front' => false ),
'show_in_rest' => true, // Added for Gutenberg support
);
register_taxonomy( $taxoName, $postType, $args );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment