Last active
February 18, 2021 22:50
-
-
Save SamuelHadsall/b5afd35bce1e358807d4871248e6a010 to your computer and use it in GitHub Desktop.
Taxonomy and Post Type and setting Post type link and rewrites
This file contains hidden or 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 | |
function bctv_register_episode_post_type_taxes() { | |
/** | |
* Taxonomy: Series Typess. | |
*/ | |
$labels = array( | |
"name" => __( "Series Types", "bctv-post-type-ui" ), | |
"singular_name" => __( "Series Types", "bctv-post-type-ui" ), | |
"menu_name" => __( "Series Types", "bctv-post-type-ui" ), | |
"all_items" => __( "Series Types", "bctv-post-type-ui" ), | |
"edit_item" => __( "Edit Types", "bctv-post-type-ui" ), | |
"view_item" => __( "View Types", "bctv-post-type-ui" ), | |
"update_item" => __( "Update Series Type", "bctv-post-type-ui" ), | |
"add_new_item" => __( "Add New Type", "bctv-post-type-ui" ), | |
"new_item_name" => __( "New Series Type", "bctv-post-type-ui" ), | |
"parent_item" => __( "Parent Type", "bctv-post-type-ui" ), | |
"parent_item_colon" => __( "Parent Type:", "bctv-post-type-ui" ), | |
"search_items" => __( "Search Types", "bctv-post-type-ui" ), | |
"popular_items" => __( "Popular Series Type", "bctv-post-type-ui" ), | |
"add_or_remove_items" => __( "Add or Remove Series Type", "bctv-post-type-ui" ), | |
"choose_from_most_used" => __( "Choose From most used Types", "bctv-post-type-ui" ), | |
"not_found" => __( "Type not found", "bctv-post-type-ui" ), | |
"no_terms" => __( "No Types", "bctv-post-type-ui" ), | |
"items_list_navigation" => __( "Series Type", "bctv-post-type-ui" ), | |
"items_list" => __( "Series Type List", "bctv-post-type-ui" ), | |
); | |
$args = array( | |
"label" => __( "Series Type", "bctv-post-type-ui" ), | |
"labels" => $labels, | |
"public" => true, | |
"publicly_queryable" => true, | |
"hierarchical" => false, | |
"show_ui" => true, | |
"show_in_menu" => true, | |
"show_in_nav_menus" => true, | |
"query_var" => true, | |
"rewrite" => array( 'slug' => 'bctv/series', 'with_front' => false ), | |
"show_admin_column" => true, | |
"show_in_rest" => true, | |
"rest_base" => "series_type", | |
"rest_controller_class" => "WP_REST_Terms_Controller", | |
"show_in_quick_edit" => false, | |
); | |
register_taxonomy( "series_type", array( "episodes" ), $args ); | |
flush_rewrite_rules(); | |
/** | |
* Taxonomy: Series. | |
*/ | |
$labels = array( | |
"name" => __( "Series", "bctv-post-type-ui" ), | |
"singular_name" => __( "Series", "bctv-post-type-ui" ), | |
"menu_name" => __( "Series", "bctv-post-type-ui" ), | |
"all_items" => __( "Series", "bctv-post-type-ui" ), | |
"edit_item" => __( "Edit Series", "bctv-post-type-ui" ), | |
"view_item" => __( "View Series", "bctv-post-type-ui" ), | |
"update_item" => __( "Update Series", "bctv-post-type-ui" ), | |
"add_new_item" => __( "Add New Series", "bctv-post-type-ui" ), | |
"new_item_name" => __( "New Series", "bctv-post-type-ui" ), | |
"parent_item" => __( "Parent Series", "bctv-post-type-ui" ), | |
"parent_item_colon" => __( "Parent Series:", "bctv-post-type-ui" ), | |
"search_items" => __( "Search Series", "bctv-post-type-ui" ), | |
"popular_items" => __( "Popular Series", "bctv-post-type-ui" ), | |
"separate_items_with_commas" => __( "Popular Series:", "bctv-post-type-ui" ), | |
"add_or_remove_items" => __( "Add or Remove Series", "bctv-post-type-ui" ), | |
"choose_from_most_used" => __( "Choose From most used Series", "bctv-post-type-ui" ), | |
"not_found" => __( "Series not found", "bctv-post-type-ui" ), | |
"no_terms" => __( "No Series", "bctv-post-type-ui" ), | |
"items_list_navigation" => __( "Series", "bctv-post-type-ui" ), | |
"items_list" => __( "Series Type List", "bctv-post-type-ui" ), | |
); | |
$args = array( | |
"label" => __( "Series", "bctv-post-type-ui" ), | |
"labels" => $labels, | |
"public" => true, | |
"publicly_queryable" => true, | |
"hierarchical" => false, | |
"show_ui" => true, | |
"show_in_menu" => true, | |
"show_in_nav_menus" => true, | |
"query_var" => true, | |
"rewrite" => array( 'slug' => 'bctv', 'with_front' => false), | |
"show_admin_column" => true, | |
"show_in_rest" => true, | |
"rest_base" => "series", | |
"rest_controller_class" => "WP_REST_Terms_Controller", | |
"show_in_quick_edit" => false, | |
); | |
register_taxonomy( "series", array( "episodes" ), $args ); | |
flush_rewrite_rules(); | |
/** | |
* Taxonomy: Actors. | |
*/ | |
$labels = array( | |
"name" => __( "Actors", "bctv-post-type-ui" ), | |
"singular_name" => __( "Actor", "bctv-post-type-ui" ), | |
"menu_name" => __( "Actors", "bctv-post-type-ui" ), | |
"all_items" => __( "All Actors", "bctv-post-type-ui" ), | |
"edit_item" => __( "Edit Actor", "bctv-post-type-ui" ), | |
"view_item" => __( "View Actor", "bctv-post-type-ui" ), | |
"update_item" => __( "Update Actor", "bctv-post-type-ui" ), | |
"add_new_item" => __( "Add New Actor", "bctv-post-type-ui" ), | |
"new_item_name" => __( "New Actor", "bctv-post-type-ui" ), | |
"search_items" => __( "Search Actors", "bctv-post-type-ui" ), | |
"popular_items" => __( "Popular Actors", "bctv-post-type-ui" ), | |
"add_or_remove_items" => __( "Add or Remove Actors", "bctv-post-type-ui" ), | |
"choose_from_most_used" => __( "Choose from the most used Actors", "bctv-post-type-ui" ), | |
"not_found" => __( "Actor Not Found", "bctv-post-type-ui" ), | |
"no_terms" => __( "No Actors", "bctv-post-type-ui" ), | |
"items_list" => __( "Actors List", "bctv-post-type-ui" ), | |
); | |
$args = array( | |
"label" => __( "Actors", "bctv-post-type-ui" ), | |
"labels" => $labels, | |
"public" => true, | |
"publicly_queryable" => true, | |
"hierarchical" => true, | |
"show_ui" => true, | |
"show_in_menu" => true, | |
"show_in_nav_menus" => true, | |
"query_var" => true, | |
"rewrite" => array( 'slug' => 'actors', 'with_front' => false ), | |
"show_admin_column" => true, | |
"show_in_rest" => true, | |
"rest_base" => "actor", | |
"rest_controller_class" => "WP_REST_Terms_Controller", | |
"show_in_quick_edit" => false, | |
); | |
register_taxonomy( "actor", array( "episodes" ), $args ); | |
flush_rewrite_rules(); | |
} | |
add_action( 'init', 'bctv_register_episode_post_type_taxes' ); | |
function bctv_register_episode_post_type() { | |
/** | |
* Post Type: Episodes. | |
*/ | |
$labels = array( | |
"name" => __( "Episodes", "bctv-post-type-ui" ), | |
"singular_name" => __( "Episode", "bctv-post-type-ui" ), | |
"menu_name" => __( "Episodes", "bctv-post-type-ui" ), | |
"all_items" => __( "All Episodes", "bctv-post-type-ui" ), | |
"add_new" => __( "Add New", "bctv-post-type-ui" ), | |
"add_new_item" => __( "Add New Episode", "bctv-post-type-ui" ), | |
"edit_item" => __( "Edit Episode", "bctv-post-type-ui" ), | |
"new_item" => __( "New Episode", "bctv-post-type-ui" ), | |
"view_item" => __( "View Episode", "bctv-post-type-ui" ), | |
"view_items" => __( "View Episodes", "bctv-post-type-ui" ), | |
"search_items" => __( "Search Episodes", "bctv-post-type-ui" ), | |
"not_found" => __( "No Episode Found", "bctv-post-type-ui" ), | |
"not_found_in_trash" => __( "No Episodes Found in Trash", "bctv-post-type-ui" ), | |
"parent_item_colon" => __( "Parent Episode:", "bctv-post-type-ui" ), | |
"featured_image" => __( "Featured Episode Image", "bctv-post-type-ui" ), | |
"set_featured_image" => __( "Set Featured Image", "bctv-post-type-ui" ), | |
"remove_featured_image" => __( "Remove Featured Image", "bctv-post-type-ui" ), | |
"use_featured_image" => __( "Use Featured Image", "bctv-post-type-ui" ), | |
"archives" => __( "Episode Archive", "bctv-post-type-ui" ), | |
"insert_into_item" => __( "Insert into Episode", "bctv-post-type-ui" ), | |
"uploaded_to_this_item" => __( "Upload to this Episode", "bctv-post-type-ui" ), | |
"filter_items_list" => __( "Filter Episode List", "bctv-post-type-ui" ), | |
"items_list_navigation" => __( "Episode List Navigation", "bctv-post-type-ui" ), | |
"items_list" => __( "Episode List", "bctv-post-type-ui" ), | |
"attributes" => __( "Episode Attributes", "bctv-post-type-ui" ), | |
"name_admin_bar" => __( "Episodes", "bctv-post-type-ui" ), | |
"parent_item_colon" => __( "Parent Episode:", "bctv-post-type-ui" ), | |
); | |
$args = array( | |
"label" => __( "Episodes", "bctv-post-type-ui" ), | |
"labels" => $labels, | |
"description" => "This will be a list of Episodes (oldest to newest) for the BCTV Original Series the user is viewing.", | |
"public" => true, | |
"publicly_queryable" => true, | |
"show_ui" => true, | |
"delete_with_user" => false, | |
"show_in_rest" => true, | |
"rest_base" => "", | |
"rest_controller_class" => "WP_REST_Posts_Controller", | |
"has_archive" => true, | |
"show_in_menu" => true, | |
"show_in_nav_menus" => true, | |
"exclude_from_search" => false, | |
"capability_type" => "page", | |
"map_meta_cap" => true, | |
"hierarchical" => true, | |
"query_var" => true, | |
"rewrite" => array( 'slug' => 'bctv', 'with_front' => false ), | |
"menu_position" => 9, | |
"menu_icon" => "dashicons dashicons-video-alt2", | |
"supports" => array( "title", "editor", "thumbnail", "page-attributes" ), | |
"taxonomies" => array( "series", "series_type", "actor", "post_tag" ), | |
); | |
register_post_type( "episodes", $args ); | |
flush_rewrite_rules(); | |
} | |
add_action( 'init', 'bctv_register_episode_post_type' ); | |
// add link to bctv home page in wp top bar | |
function bctv_toolbar_link($wp_admin_bar) { | |
$args = array( | |
'id' => 'bctv-episode-link', | |
'title' => 'Get BCTV', | |
'href' => '/bctv', | |
'meta' => array( | |
'class' => 'bctv-home-link', | |
'title' => 'Get BCTV' | |
) | |
); | |
$wp_admin_bar->add_node($args); | |
} | |
add_action('admin_bar_menu', 'bctv_toolbar_link', 999); |
This file contains hidden or 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 | |
function episodes_series_type_cpt_generating_rule($wp_rewrite) { | |
$series_type_rules = array(); | |
$series_type_terms = get_terms( array( | |
'taxonomy' => 'series_type', | |
'hide_empty' => false, | |
) ); | |
$post_type = 'episodes'; | |
foreach ($series_type_terms as $series_type_term) { | |
$series_type_rules['bctv/series/' . $series_type_term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&' . $post_type. '=$matches[1]&name=$matches[1]'; | |
} | |
// merge with global rules | |
$wp_rewrite->rules = $series_type_rules + $wp_rewrite->rules; | |
} | |
add_filter('generate_rewrite_rules', 'episodes_series_type_cpt_generating_rule'); | |
function series_type_change_link( $permalink, $post ) { | |
if( $post->post_type == 'episodes' ) { | |
$episode_type_terms = get_the_terms( $post, 'series_type' ); | |
$term_type_slug; | |
if( ! empty( $episode_type_terms ) ) { | |
foreach ( $episode_type_terms as $term_type ) { | |
$term_type_slug = $term_type->slug; | |
} | |
} | |
$permalink = get_home_url() ."/bctv/series/" . $term_type_slug . '/' . $post->post_name; | |
} | |
return $permalink; | |
} | |
add_filter('post_type_link', 'series_type_change_link', 10, 2); | |
function episodes_series_cpt_generating_rule($wp_rewrite) { | |
$series_rules = array(); | |
$series_terms = get_terms( array( | |
'taxonomy' => 'series', | |
'hide_empty' => false, | |
) ); | |
$post_type = 'episodes'; | |
//This will create a series type > series relationship | |
foreach ($series_terms as $series_term) { | |
// Is it a child term? | |
/* if( $series_term->parent ) { | |
$parent_term = get_terms( array( | |
'taxonomy' => 'series_type', | |
'hide_empty' => false, | |
) ); | |
foreach ($parent_term as $parent) { | |
if ($parent->term_id == $series_term->parent) { | |
$series_rules['bctv/series/' . $parent->slug . '/' . $series_term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&' . $post_type . '=$matches[1]&name=$matches[1]'; | |
} | |
} | |
} else { | |
$series_rules['bctv/'.$series_term->slug.'/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&' . $post_type . '=$matches[1]&name=$matches[1]'; | |
} */ | |
$parent_term = get_field('parent_series_type', 'term_' . $series_term->term_id); | |
// Is it a child term? | |
if ($parent_term) { | |
$series_rules['bctv/series/' . $parent_term->slug . '/' . $series_term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&' . $post_type . '=$matches[1]&name=$matches[1]'; | |
//$term_url = get_home_url() ."/bctv/series/" . $parent->slug . '/' . $term->slug . '/' . $post->post_name; | |
} else { | |
$series_rules['bctv/'.$series_term->slug.'/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&' . $post_type . '=$matches[1]&name=$matches[1]'; | |
//$term_url = get_home_url() ."/bctv/" . $term->slug . '/' . $post->post_name; | |
} | |
// merge with global rules | |
$wp_rewrite->rules = $series_rules + $wp_rewrite->rules; | |
//print_r($wp_rewrite->rules); | |
} | |
} | |
add_filter('generate_rewrite_rules', 'episodes_series_cpt_generating_rule'); | |
//Updates the View Episode link in admin | |
function series_change_link( $permalink, $post ) { | |
if( $post->post_type == 'episodes' ) { | |
$episode_series_terms = get_the_terms( $post->ID, 'series' ); | |
// Creates a series type series relationship | |
$term_series_slug; | |
if( !empty( $episode_series_terms ) ) { | |
foreach ( $episode_series_terms as $term_series ) { | |
$parent = get_field('parent_series_type', 'term_' . $term_series->term_id); | |
if ($parent) { | |
$term_series_slug = 'series/' . $parent->slug . '/' . $term_series->slug; | |
} else { | |
$term_series_slug = 'series/' . $parent->slug; | |
} | |
} | |
} | |
$permalink = get_home_url() ."/bctv/" . $term_series_slug . '/' . $post->post_name; | |
} | |
return $permalink; | |
} | |
add_filter('post_type_link', 'series_change_link', 10, 2); | |
function episode_posts_groupby($groupby) { | |
global $wpdb; | |
$groupby = "{$wpdb->posts}.ID"; | |
return $groupby; | |
} | |
add_filter( 'posts_groupby', 'episode_posts_groupby' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment