Last active
February 2, 2024 23:04
-
-
Save maddisondesigns/7d4b87637b3e73457d49d2b285b094b7 to your computer and use it in GitHub Desktop.
Create WordPress Custom Post Types & Taxonomies, and add custom columns to display CPT data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Add an action when WP Admin initialises to register our Custom Post Type | |
*/ | |
function mdsgns_create_custom_post_types() { | |
$types = array( | |
// Where the magic happens | |
array( | |
'the_type' => 'store', | |
'single' => 'Store', | |
'plural' => 'Stores', | |
'rewrite' => 'stores', | |
'icon' => 'dashicons-store', | |
), | |
array( | |
'the_type' => 'booktitle', | |
'single' => 'Book Title', | |
'plural' => 'Book Titles', | |
'rewrite' => 'book-titles', | |
'icon' => 'dashicons-book' | |
) | |
); | |
foreach ( $types as $type ) { | |
$the_type = $type['the_type']; | |
$single = $type['single']; | |
$plural = $type['plural']; | |
$rewrite = $type['rewrite']; | |
$icon = $type['icon']; | |
$labels = array( | |
'name' => _x( $plural, 'post type general name' ), | |
'singular_name' => _x( $single, 'post type singular name' ), | |
'add_new' => _x( 'Add New', $single ), | |
'add_new_item' => __( 'Add New ' . $single ), | |
'edit_item' => __( 'Edit ' . $single ), | |
'new_item' => __( 'New ' . $single ), | |
'view_item' => __( 'View ' . $single ), | |
'search_items' => __( 'Search ' . $plural ), | |
'not_found' => __( 'No ' . $plural . ' found' ), | |
'not_found_in_trash' => __( 'No ' . $plural . ' found in Trash' ), | |
'parent_item_colon' => '' | |
); | |
$args = array( | |
'labels' => $labels, | |
'public' => true, | |
'has_archive' => true, | |
'publicly_queryable' => true, | |
'show_ui' => true, | |
'query_var' => true, | |
'rewrite' => true, | |
'capability_type' => 'post', | |
'hierarchical' => false, | |
'menu_position' => 5, | |
'block-editor' => false, | |
'show_in_rest' => false, | |
'rewrite' => array( 'slug' => $rewrite ), | |
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'custom-fields', 'excerpt', 'revisions' ), | |
'menu_icon' => $icon | |
); | |
register_post_type( $the_type, $args ); | |
} | |
} | |
add_action( 'init', 'mdsgns_create_custom_post_types' ); | |
/** | |
* Add an action when WP Admin initialises to registers our CPT Taxonomies | |
*/ | |
function mdsgns_create_cpt_taxonomies() { | |
// The name of our Custom Post Type | |
$cptname = "store"; | |
// Our Taxonomy names | |
// hierarchical => true are like categories | |
// hierarchical => false are like tags | |
$types = array( | |
// Where the magic happens | |
array('the_type' => 'store_category', | |
'single' => 'Category', | |
'plural' => 'Categories', | |
'hierarchical' => true, | |
'rewrite' => array( 'slug' => 'category' ), | |
), | |
array('the_type' => 'store_tag', | |
'single' => 'Tag', | |
'plural' => 'Tags', | |
'hierarchical' => false, | |
'rewrite' => array( 'slug' => 'tag' ), | |
) | |
); | |
foreach ($types as $type) { | |
$the_type = $type['the_type']; | |
$single = $type['single']; | |
$plural = $type['plural']; | |
$hierarchical = $type['hierarchical']; | |
$rewrite = $type['rewrite']; | |
$labels = array( | |
'name' => _x( $plural, 'taxonomy general name' ), | |
'singular_name' => _x( $single, 'taxonomy singular name' ), | |
'search_items' => __( 'Search ' . $plural ), | |
'all_items' => __( 'All ' . $plural ), | |
'parent_item' => __( 'Parent ' . $single ), | |
'parent_item_colon' => __( 'Parent ' . $single.':' ), | |
'edit_item' => __( 'Edit ' . $single ), | |
'update_item' => __( 'Update ' . $single ), | |
'add_new_item' => __( 'Add New ' . $single ), | |
'new_item_name' => __( 'New ' . $single ), | |
'menu_name' => __( $single ), | |
); | |
register_taxonomy( $the_type, array( $cptname ), array( | |
'hierarchical' => $hierarchical, | |
'labels' => $labels, | |
'update_count_callback' => '_update_post_term_count', | |
'rewrite' => $rewrite | |
)); | |
} | |
} | |
add_action( 'init', 'mdsgns_create_cpt_taxonomies', 0 ); | |
/** | |
* Flush the Rewrites on plugin activation to make sure the new Permalinks work | |
* Use this if your CPT is within a plugin | |
*/ | |
function ephemeris_plugin_rewrite_flush() { | |
flush_rewrite_rules(); | |
} | |
register_activation_hook( __FILE__, 'ephemeris_plugin_rewrite_flush' ); | |
/** | |
* Flush the Rewrites on theme switch to make sure the new Permalinks work | |
* Use this if your CPT is within a theme | |
*/ | |
function ephemeris_theme_rewrite_flush() { | |
flush_rewrite_rules(); | |
} | |
add_action( 'after_switch_theme', 'ephemeris_theme_rewrite_flush' ); | |
/** | |
* Add an action and filter to customise the display when viewing the list of Custom Posts | |
* The hook name is actually `manage_edit-{$post_type}_columns` so ensure you replace {$post_type} with name of your Custom Post type | |
*/ | |
function mdsgns_store_edit_columns( $columns ){ | |
$columns = array( | |
'cb' => '<input type="checkbox" />', | |
'title' => 'Store Name', | |
'category' => 'Categories', | |
'tag' => 'Tags', | |
'date' => 'Date' | |
); | |
return $columns; | |
} | |
add_filter( 'manage_edit-store_columns', 'mdsgns_store_edit_columns' ); | |
/** | |
* Add an action and filter to add our content to the custom columns | |
* The hook name is actually `manage_{$post_type}_posts_custom_column` so ensure you replace {$post_type} with name of your Custom Post type | |
*/ | |
function mdsgns_store_custom_columns( $column ){ | |
global $post; | |
switch ( $column ) { | |
case 'category': | |
echo get_the_term_list( $post->ID, 'store_category', '', ', ','' ); | |
break; | |
case 'tag': | |
echo get_the_term_list( $post->ID, 'store_tag', '', ', ','' ); | |
break; | |
} | |
} | |
add_action( 'manage_store_posts_custom_column', 'mdsgns_store_custom_columns' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment