Skip to content

Instantly share code, notes, and snippets.

@mattboon
Created August 3, 2012 18:20
Show Gist options
  • Save mattboon/3250150 to your computer and use it in GitHub Desktop.
Save mattboon/3250150 to your computer and use it in GitHub Desktop.
WordPress - Events and Projects post types each with categories
<?
// EVENTS
add_action('init', '_init_event_post_type');
function _init_event_post_type() {
// Create taxonomy
register_taxonomy(
'event_category',
array( 'event' ),
array(
'labels' => array(
'name' => __( 'Event Categories' ),
'singular_name' => __( 'Event Category' ),
'search_items' => __( 'Search Categories' ),
'popular_items' => __( 'Popular Categories' ),
'all_items' => __( 'All Categories' ),
'parent_item' => __( 'Parent Category' ),
'parent_item_colon' => __( 'Parent Categories:' ),
'edit_item' => __( 'Edit Category' ),
'update_item' => __( 'Update Category' ),
'add_new_item' => __( 'Add New Category' ),
'new_item_name' => __( 'New Category' ),
),
'public' => true,
'show_in_nav_menus' => true,
'show_ui' => true,
'publicly_queryable' => false,
'exclude_from_search' => false,
'hierarchical' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'events/filter', 'with_front' => false ),
)
);
// Create post type
register_post_type( 'event',
array(
'capability_type' => 'post',
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'menu_position' => 30,
'has_archive' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'events/%event_category%', 'with_front' => false ),
'supports' => array('title', 'editor', 'excerpt', 'thumbnail'),
'labels' => array(
'name' => __( 'Events' ),
'singular_name' => __( 'Event' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add New Event' ),
'edit' => __( 'Edit' ),
'edit_item' => __( 'Edit Event' ),
'new_item' => __( 'New Event' ),
'view' => __( 'View Event' ),
'view_item' => __( 'View Event' ),
'search_items' => __( 'Search Event' ),
'not_found' => __( 'No Events found' ),
'not_found_in_trash' => __( 'No Events found in Trash' )
)
)
);
// Make permalinks for events pretty (add category to URL)
add_filter('post_type_link', 'event_permalink_filter_function', 1, 3);
function event_permalink_filter_function( $post_link, $id = 0, $leavename = FALSE ) {
if ( strpos('%event_category%', $post_link) === 'FALSE' ) {
return $post_link;
}
$post = get_post($id);
if ( !is_object($post) || $post->post_type != 'event' ) {
return $post_link;
}
$terms = wp_get_object_terms($post->ID, 'event_category');
if ( !$terms ) {
return str_replace('events/%event_category%/', '', $post_link);
}
return str_replace('%event_category%', $terms[0]->slug, $post_link);
}
}
// PROJECTS
add_action('init', '_init_project_post_type');
function _init_project_post_type() {
// Create taxonomy
register_taxonomy(
'project_category',
array( 'project' ),
array(
'labels' => array(
'name' => __( 'Project Categories' ),
'singular_name' => __( 'Project Category' ),
'search_items' => __( 'Search Categories' ),
'popular_items' => __( 'Popular Categories' ),
'all_items' => __( 'All Categories' ),
'parent_item' => __( 'Parent Category' ),
'parent_item_colon' => __( 'Parent Categories:' ),
'edit_item' => __( 'Edit Category' ),
'update_item' => __( 'Update Category' ),
'add_new_item' => __( 'Add New Category' ),
'new_item_name' => __( 'New Category' ),
),
'public' => true,
'show_in_nav_menus' => true,
'show_ui' => true,
'publicly_queryable' => false,
'exclude_from_search' => false,
'hierarchical' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'projects/filter', 'with_front' => false ),
)
);
// Create post type
register_post_type( 'project',
array(
'capability_type' => 'post',
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'menu_position' => 30,
'has_archive' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'projects/%project_category%', 'with_front' => false ),
'supports' => array('title', 'editor', 'excerpt', 'thumbnail'),
'labels' => array(
'name' => __( 'Projects' ),
'singular_name' => __( 'Project' ),
'add_new' => __( 'Add New' ),
'add_new_item' => __( 'Add New Project' ),
'edit' => __( 'Edit' ),
'edit_item' => __( 'Edit Project' ),
'new_item' => __( 'New Project' ),
'view' => __( 'View Project' ),
'view_item' => __( 'View Project' ),
'search_items' => __( 'Search Project' ),
'not_found' => __( 'No Projects found' ),
'not_found_in_trash' => __( 'No Projects found in Trash' )
)
)
);
// Make permalinks for projects pretty (add category to URL)
add_filter('post_type_link', 'project_permalink_filter_function', 1, 3);
function project_permalink_filter_function( $post_link, $id = 0, $leavename = FALSE ) {
if ( strpos('%project_category%', $post_link) === 'FALSE' ) {
return $post_link;
}
$post = get_post($id);
if ( !is_object($post) || $post->post_type != 'project' ) {
return $post_link;
}
$terms = wp_get_object_terms($post->ID, 'project_category');
if ( !$terms ) {
return str_replace('projects/%project_category%/', '', $post_link);
}
return str_replace('%project_category%', $terms[0]->slug, $post_link);
}
// Make permalinks work! (must come after your last CPT)
flush_rewrite_rules();
}
// Fixes /page/2 bug for archives
// http://wordpress.stackexchange.com/a/16929/9244
add_action( 'init', 'wpse16902_init' );
function wpse16902_init() {
$GLOBALS['wp_rewrite']->use_verbose_page_rules = true;
}
add_filter( 'page_rewrite_rules', 'wpse16902_collect_page_rewrite_rules' );
function wpse16902_collect_page_rewrite_rules( $page_rewrite_rules )
{
$GLOBALS['wpse16902_page_rewrite_rules'] = $page_rewrite_rules;
return array();
}
add_filter( 'rewrite_rules_array', 'wspe16902_prepend_page_rewrite_rules' );
function wspe16902_prepend_page_rewrite_rules( $rewrite_rules )
{
return $GLOBALS['wpse16902_page_rewrite_rules'] + $rewrite_rules;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment