Skip to content

Instantly share code, notes, and snippets.

@utkrishta
Created July 27, 2020 00:33
Show Gist options
  • Save utkrishta/5ec146e2ab142698aa1995fc88e66b81 to your computer and use it in GitHub Desktop.
Save utkrishta/5ec146e2ab142698aa1995fc88e66b81 to your computer and use it in GitHub Desktop.
Custom Post Types with taxonomy in the permalink
<?php
/*
In this example, I've created custom post-types called "locations" and custom taxonomy called "state"
By default the generated permalink structure would look like this:
Locations: <base-url>/locations/<post-slug>/
State: <base-url>/state/<taxonomy-slug>/
But here is what I wanted:
1. Replace "locations" with "service-areas" in the permalink for locaitons.
2. Permalink structure for "locations" to be: <base-url>/service-areas/<state-slug>/<locations-slug>
Example: <base-url>/service-areas/sa/adelaide/
3. Permalink structure for "state" to be: <base-url>/service-areas/<state-slug>/
Example: <base-url>/service-areas/sa/
4. Permalink structure for "locations" archive to be : <base-url>/service-areas/
Note: The advantage of using this permalink structures. It has a flow to these URLs
*/
/*Custom Post Types*/
/**
* Register a Custom post type.
*
* @link http://codex.wordpress.org/Function_Reference/register_post_type
* @link https://developer.wordpress.org/resource/dashicons
*/
//Use this as an example as required
add_action( 'init', 'krish_locations_init' );
function krish_locations_init() {
$labels = array(
'name' => _x( 'Locations', 'post type general name', 'krish-cpt' ),
'singular_name' => _x( 'Locations', 'post type singular name', 'krish-cpt' ),
'menu_name' => _x( 'Locations', 'admin menu', 'krish-cpt' ),
'name_admin_bar' => _x( 'Location', 'add new on admin bar', 'krish-cpt' ),
'add_new' => _x( 'Add New', 'location', 'krish-cpt' ),
'add_new_item' => __( 'Add New Location', 'krish-cpt' ),
'new_item' => __( 'New Location', 'krish-cpt' ),
'edit_item' => __( 'Edit Location', 'krish-cpt' ),
'view_item' => __( 'View Location', 'krish-cpt' ),
'all_items' => __( 'All Locations', 'krish-cpt' ),
'search_items' => __( 'Search Locations', 'krish-cpt' ),
'parent_item_colon' => __( 'Parent Location:', 'krish-cpt' ),
'not_found' => __( 'No locations found.', 'krish-cpt' ),
'not_found_in_trash' => __( 'No locations found in Trash.', 'krish-cpt' )
);
$args = array(
'labels' => $labels,
//'description' => __( '', 'krish-cpt' ), //update description if necessary or call custom field
'public' => TRUE,
'publicly_queryable' => TRUE,
'show_ui' => TRUE,
'show_in_menu' => TRUE,
'menu_icon' => 'dashicons-location-alt',
'query_var' => TRUE,
'rewrite' => array( 'with_front' => FALSE, 'slug' => 'service-areas/%state%' ),
'capability_type' => 'post',
'has_archive' => 'service-areas',
'hierarchical' => TRUE,
'menu_position' => NULL,
'supports' => array( 'title', 'content', 'thumbnail', 'revisions', 'page-attributes' )
);
register_post_type( 'locations', $args );
}
/*Custom Taxonomies*/
/*
* Register a Custom Taxonomy
*
* @link https://codex.wordpress.org/Function_Reference/register_taxonomy
*
*/
//Use this as an example as required
add_action( 'init', 'krish_init_state' );
function krish_init_state() {
$labels = array(
"name" => __( "States", "krish-cpt" ),
"singular_name" => __( "State", "krish-cpt" ),
);
$args = array(
"label" => __( "States", "krish-cpt" ),
"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( 'with_front' => FALSE, 'slug' => 'service-areas' ),
"show_admin_column" => TRUE,
"show_in_rest" => TRUE,
"rest_base" => "state",
"rest_controller_class" => "WP_REST_Terms_Controller",
"show_in_quick_edit" => TRUE,
);
$related_cpt = array( "locations" ); //change post-types as required
register_taxonomy( "state", $related_cpt, $args );
}
/* ===== Locations CPT permalink's %state% to be replaced by state taxonomy value ===== */
add_filter( 'post_type_link', 'locations_post_link', 1, 3 );
function locations_post_link( $post_link, $id = 0 ) {
$post = get_post( $id );
$terms = wp_get_object_terms( $post->ID, 'state' );
if ( $terms ) {
return str_replace( '%state%', $terms[0]->slug, $post_link );
}
return $post_link;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment