Last active
November 23, 2023 12:04
-
-
Save simplethemes/c9813cafef799000b04439ffae4a5c50 to your computer and use it in GitHub Desktop.
CPT Rewrite
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
/** | |
* Register Community Custom Post Type | |
*/ | |
function community_post_type() { | |
$labels = array( | |
'name' => 'Communities', | |
'singular_name' => 'Community', | |
'menu_name' => 'Communities', | |
'name_admin_bar' => 'Communities', | |
'archives' => 'Community Archives', | |
'attributes' => 'Community Attributes', | |
'parent_item_colon' => 'Parent Community:', | |
'all_items' => 'All Communities', | |
'add_new_item' => 'Add New Community', | |
'add_new' => 'Add New', | |
'new_item' => 'New Community', | |
'edit_item' => 'Edit Community', | |
'update_item' => 'Update Community', | |
'view_item' => 'View Community', | |
'view_items' => 'View Communities', | |
'search_items' => 'Search Communities', | |
'not_found' => 'Community Not found', | |
'not_found_in_trash' => 'Not found in Trash', | |
'featured_image' => 'Featured Image', | |
'set_featured_image' => 'Set featured image', | |
'remove_featured_image' => 'Remove featured image', | |
'use_featured_image' => 'Use as featured image', | |
'insert_into_item' => 'Insert into item', | |
'uploaded_to_this_item' => 'Uploaded to this Community', | |
'items_list' => 'Communities list', | |
'items_list_navigation' => 'Communities list navigation', | |
'filter_items_list' => 'Filter Communities list', | |
); | |
$rewrite = array( | |
'slug' => '/%category%/communities' | |
); | |
$args = array( | |
'label' => 'Community', | |
'labels' => $labels, | |
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', ), | |
'taxonomies' => array( 'category' ), | |
'public' => true, | |
'show_ui' => true, | |
'show_in_menu' => true, | |
'menu_position' => 5, | |
'menu_icon' => 'dashicons-universal-access', | |
'show_in_admin_bar' => true, | |
'show_in_nav_menus' => true, | |
'can_export' => true, | |
'has_archive' => true, | |
'exclude_from_search' => false, | |
'query_var' => true, | |
'query_var' => true, | |
'rewrite' => true, | |
'hierarchical' => true, | |
'capability_type' => 'page', | |
'show_in_rest' => true, | |
); | |
register_post_type( 'communities', $args ); | |
} | |
add_action( 'init', 'community_post_type',99 ); | |
/** | |
* Add category slug to community post links | |
* @param $permalink | |
* @param $post | |
* @return $permalink | |
*/ | |
function commmunity_posts_link( $permalink, $post ) { | |
if( $post->post_type == 'communities' ) { | |
// $resource_terms = get_the_terms( $post, 'resource_type' ); | |
$resource_terms = get_terms( array( | |
'taxonomy' => 'category', | |
'hide_empty' => false | |
)); | |
$term_slug = ''; | |
if( ! empty( $resource_terms ) ) { | |
foreach ( $resource_terms as $term ) { | |
// you should prob do some conditional checking here | |
// such as 'featured' or something, otherwise you have | |
// too much duplicate content | |
if( $term->parent != 0 ) { | |
continue; | |
} | |
$term_slug = $term->slug; | |
break; | |
} | |
} | |
$permalink = trailingslashit(get_home_url()) . $term_slug . "/communities/" . $post->post_name; | |
} | |
return $permalink; | |
} | |
add_filter('post_type_link',"commmunity_posts_link",10,2); | |
/** | |
* Adds the rewrite rules | |
* @param $wp_rewrite | |
*/ | |
function communities_cpt_rewrite_rule($wp_rewrite) { | |
$post_type = 'communities'; | |
$terms = get_terms( array( | |
'taxonomy' => 'category', | |
'hide_empty' => false | |
)); | |
$rules = array(); | |
foreach ($terms as $term) { | |
$rules[ $term->slug .'/communities'. '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&category=$matches[1]&name=$matches[1]'; | |
} | |
// merge with global rules | |
$wp_rewrite->rules = $rules + $wp_rewrite->rules; | |
} | |
add_filter('generate_rewrite_rules', 'communities_cpt_rewrite_rule'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment