Skip to content

Instantly share code, notes, and snippets.

@monkishtypist
Last active September 22, 2018 00:19
Show Gist options
  • Save monkishtypist/9b6db0f647cf163586b106f7005fa2d2 to your computer and use it in GitHub Desktop.
Save monkishtypist/9b6db0f647cf163586b106f7005fa2d2 to your computer and use it in GitHub Desktop.
WordPress `mu-plugin` to create a custom post type for Leadership Team.
<?php
/*
Plugin Name: Leadership Team Custom Post Type
Plugin URI: https://github.com/monkishtypist
Description: This `mu-plugin` class creates a new custom post type for Leadership Team.
Version: 1.0
Author: Tim Spinks @monkishtypist
Author URI: https://github.com/monkishtypist
*/
if ( ! class_exists( 'CPT_LeadershipTeam' ) ) :
class CPT_LeadershipTeam {
private $textdomain = '';
public function __construct() {
/**
* Register team member post type
*/
add_action( 'init', array( $this, 'team_tax_init') );
add_action( 'init', array( $this, 'teammembers_post_type') );
/**
* Alter the query
*/
add_action( 'pre_get_posts', array( $this, 'modify_query' ), 1 );
}
/**
* Register custom taxonomy
*/
public function team_tax_init() {
$labels = array(
'name' => _x( 'Teams', 'cpt_teammembers_post_type', $this->textdomain ),
'singular_name' => _x( 'Team', 'cpt_teammembers_post_type', $this->textdomain ),
'search_items' => __( 'Search Teams', $this->textdomain ),
'popular_items' => __( 'Popular Teams', $this->textdomain ),
'all_items' => __( 'All Teams', $this->textdomain ),
'parent_item' => __( 'Parent Team', $this->textdomain ),
'parent_item_colon' => __( 'Parent Team:', $this->textdomain ),
'edit_item' => __( 'Edit Team', $this->textdomain ),
'view_item' => __( 'View Team', $this->textdomain ),
'update_item' => __( 'Update Team', $this->textdomain ),
'add_new_item' => __( 'Add New Team', $this->textdomain ),
'new_item_name' => __( 'New Position', $this->textdomain ),
'separate_items_with_commas' => __( 'Separate teams with commas', $this->textdomain ),
'add_or_remove_items' => __( 'Add or remove teams', $this->textdomain ),
'choose_from_most_used' => __( 'Choose from the most used teams', $this->textdomain ),
'not_found' => __( 'No teams found', $this->textdomain ),
'no_terms' => __( 'No teams', $this->textdomain ),
'items_list_navigation' => __( 'Items list navigation', $this->textdomain ),
'items_list' => __( 'Items list', $this->textdomain ),
'most_used' => __( 'Most Used', $this->textdomain ),
'back_to_terms' => __( 'Back to teams', $this->textdomain ),
);
$rewrite = array(
'slug' => 'teams',
'with_front' => false,
'hierarchical' => true
);
$args = array(
'labels' => $labels,
'description' => __( 'Team member team', $this->textdomain ),
'public' => true,
'publicly_queryable' => true,
'hierarchical' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_admin_column' => true,
'rewrite' => $rewrite
);
register_taxonomy( 'cpt-teams', 'cpt-team', $args );
}
/**
* Register post type
*/
public function teammembers_post_type() {
$labels = array(
'name' => _x( 'Team Members', 'teammembers_post_type', $this->textdomain ),
'singular_name' => _x( 'Team Member', 'teammembers_post_type', $this->textdomain ),
'menu_name' => _x( 'Team Members', 'teammembers_post_type', $this->textdomain ),
'name_admin_bar' => _x( 'Team Memvers', 'teammembers_post_type', $this->textdomain ),
'archives' => __( 'Item Archives', $this->textdomain ),
'parent_item_colon' => __( 'Parent Item:', $this->textdomain ),
'all_items' => __( 'All Team Members', $this->textdomain ),
'add_new_item' => __( 'Add New Team Member', $this->textdomain ),
'add_new' => __( 'Add New', $this->textdomain ),
'new_item' => __( 'New Team Member', $this->textdomain ),
'edit_item' => __( 'Edit Team Member', $this->textdomain ),
'update_item' => __( 'Update Team Member', $this->textdomain ),
'view_item' => __( 'View Team Member', $this->textdomain ),
'search_items' => __( 'Search Team Members', $this->textdomain ),
'not_found' => __( 'Not found', $this->textdomain ),
'not_found_in_trash' => __( 'Not found in Trash', $this->textdomain ),
'featured_image' => __( 'Featured Image', $this->textdomain ),
'set_featured_image' => __( 'Set featured image', $this->textdomain ),
'remove_featured_image' => __( 'Remove featured image', $this->textdomain ),
'use_featured_image' => __( 'Use as featured image', $this->textdomain ),
'insert_into_item' => __( 'Insert into item', $this->textdomain ),
'uploaded_to_this_item' => __( 'Uploaded to this item', $this->textdomain ),
'items_list' => __( 'Items list', $this->textdomain ),
'items_list_navigation' => __( 'Items list navigation', $this->textdomain ),
'filter_items_list' => __( 'Filter items list', $this->textdomain ),
);
$rewrite = array(
'slug' => 'our-team',
'with_front' => false,
'pages' => true,
'feeds' => false,
);
$args = array(
'label' => _x( 'Team Member', 'cpt_teammembers_post_type', $this->textdomain ),
'description' => __( 'Company Team Member Profile Page', $this->textdomain ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions', 'custom-fields', ),
'taxonomies' => array( 'cpt-teams'),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-nametag',
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'page',
);
register_post_type( 'cpt-team', $args );
}
/**
* Modify the query
*/
public function modify_query( $query ) {
if ( is_admin() || ! $query->is_main_query() )
return;
if ( is_tax( 'cpt-teams' ) ) {
// Use this to modify the custom post type query
// $query->set( 'posts_per_page', 8 ); // Example: change query results to 8 per page
return;
}
}
}
$CPT_LeadershipTeam = new CPT_LeadershipTeam();
endif;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment