Skip to content

Instantly share code, notes, and snippets.

@aaronsummers
Created December 5, 2022 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaronsummers/c95112e9dd3942f65b6bc98bd81fb29c to your computer and use it in GitHub Desktop.
Save aaronsummers/c95112e9dd3942f65b6bc98bd81fb29c to your computer and use it in GitHub Desktop.
Using custom taxonomy as custom post type URL base
<?php
add_action( 'init', 'my_custom_post_types_init', 0 );
function my_custom_post_types_init()
{
/**
* Register the post types
*/
$my_post_types = array(
'property' => array(
'post_type_name_singular' => 'Property',
'post_type_name_plural' => 'Properties',
'post_type_slug' => 'property',
'post_type_icon' => 'dashicons-building',
'hierarchical' => true, // make false so this works with the custom permalinks and rewrite rules
'rewrite' => array(
'slug' => '%location%', // Remove the slug
'with_front' => false, // without front
'pages' => true,
'feeds' => true,
)
),
);
/**
* Loop the post types
*/
foreach ( $my_post_types as $post_type_key => $my_post_type_args )
{
$post_type_name_singular = isset($my_post_type_args['post_type_name_singular']) ? $my_post_type_args['post_type_name_singular'] : 'empty';
$post_type_name_plural = isset($my_post_type_args['post_type_name_plural']) ? $my_post_type_args['post_type_name_plural'] : 'empty';
$post_type_slug = isset($my_post_type_args['post_type_slug']) ? $my_post_type_args['post_type_slug'] : 'empty';
$post_type_icon = isset($my_post_type_args['post_type_icon']) ? $my_post_type_args['post_type_icon'] : 'dashicons-nametag';
$hierarchical = isset($my_post_type_args['hierarchical']) ? $my_post_type_args['hierarchical'] : false;
$labels = array(
'name' => _x( $post_type_name_plural, 'Post Type General Name', 'any' ),
'singular_name' => _x( $post_type_name_singular, 'Post Type Singular Name', 'any' ),
'menu_name' => __( $post_type_name_plural, 'any' ),
'name_admin_bar' => __( $post_type_name_singular, 'any' ),
'archives' => __( $post_type_name_singular . ' Archives', 'any' ),
'attributes' => __( $post_type_name_singular . ' Attributes', 'any' ),
'parent_item_colon' => __( 'Parent ' . $post_type_name_singular . ':', 'any' ),
'all_items' => __( 'All ' . $post_type_name_plural, 'any' ),
'add_new_item' => __( 'Add New ' . $post_type_name_singular, 'any' ),
'add_new' => __( 'Add New', 'any' ),
'new_item' => __( 'New ' . $post_type_name_singular, 'any' ),
'edit_item' => __( 'Edit ' . $post_type_name_singular, 'any' ),
'update_item' => __( 'Update ' . $post_type_name_singular, 'any' ),
'view_item' => __( 'View ' . $post_type_name_singular, 'any' ),
'view_items' => __( 'View ' . $post_type_name_plural, 'any' ),
'search_items' => __( 'Search ' . $post_type_name_singular, 'any' ),
'not_found' => __( 'Not found', 'any' ),
'not_found_in_trash' => __( 'Not found in Trash', 'any' ),
'featured_image' => __( 'Featured Image', 'any' ),
'set_featured_image' => __( 'Set featured image', 'any' ),
'remove_featured_image' => __( 'Remove featured image', 'any' ),
'use_featured_image' => __( 'Use as featured image', 'any' ),
'insert_into_item' => __( 'Insert into ' . $post_type_name_singular, 'any' ),
'uploaded_to_this_item' => __( 'Uploaded to this ' . $post_type_name_singular, 'any' ),
'items_list' => __( $post_type_name_plural . ' list', 'any' ),
'items_list_navigation' => __( $post_type_name_plural . ' list navigation', 'any' ),
'filter_items_list' => __( 'Filter ' . $post_type_name_plural . ' list', 'any' ),
);
if ( isset($my_post_type_args['rewrite']) )
{
$rewrite = $my_post_type_args['rewrite'];
}
else
{
$rewrite = array(
'slug' => $post_type_slug,
'with_front' => true,
'pages' => true,
'feeds' => true,
);
}
$args = array(
'label' => __( $post_type_name_singular, 'any' ),
'description' => __( 'Post Type Description', 'any' ),
'labels' => $labels,
'supports' => array( 'title', 'thumbnail', 'editor', 'excerpt', 'page-attributes' ),
'taxonomies' => array('brand'),
'hierarchical' => $hierarchical,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'menu_icon' => $post_type_icon,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'page',
'show_in_rest' => true,
);
register_post_type( $post_type_key, $args );
}
}
if ( ! function_exists( 'location' ) )
{
// Register Custom Taxonomy
function location()
{
$labels = array(
'name' => _x( 'Locations', 'Taxonomy General Name', 'any' ),
'singular_name' => _x( 'Location', 'Taxonomy Singular Name', 'any' ),
'menu_name' => __( 'Location', 'any' ),
'all_items' => __( 'All Locations', 'any' ),
'parent_item' => __( 'Parent Location', 'any' ),
'parent_item_colon' => __( 'Parent Location:', 'any' ),
'new_item_name' => __( 'New Location Name', 'any' ),
'add_new_item' => __( 'Add New Location', 'any' ),
'edit_item' => __( 'Edit Location', 'any' ),
'update_item' => __( 'Update Location', 'any' ),
'view_item' => __( 'View Location', 'any' ),
'separate_items_with_commas' => __( 'Separate Locations with commas', 'any' ),
'add_or_remove_items' => __( 'Add or remove Location', 'any' ),
'choose_from_most_used' => __( 'Choose from the most used', 'any' ),
'popular_items' => __( 'Popular Locations', 'any' ),
'search_items' => __( 'Search Locations', 'any' ),
'not_found' => __( 'Not Found', 'any' ),
'no_terms' => __( 'No items', 'any' ),
'items_list' => __( 'Items list', 'any' ),
'items_list_navigation' => __( 'Items list navigation', 'any' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'show_in_rest' => true,
'query_var' => 'location',
'rewrite' => true
);
register_taxonomy( 'location', array( 'property' ), $args );
}
add_action( 'init', 'location', 0 );
}
/**
* Rewrite the URL for properties
*
* @link https://www.shibashake.com/wp/add-custom-taxonomy-tags-to-your-wordpress-permalinkss
* @param [type] $permalink
* @param [type] $post_id
* @param [type] $leavename
* @return void
*/
function location_permalink( $permalink , $post_id , $leavename )
{
if ( strpos ( $permalink , '%location%' ) === FALSE) return $permalink;
// Get post
$post = get_post( $post_id );
if (! $post ) return $permalink ;
// Get taxonomy terms
$terms = wp_get_object_terms( $post ->ID, 'location' );
if ( !is_wp_error( $terms ) && ! empty ( $terms ) && is_object ( $terms[0] ))
{
$taxonomy_slug = $terms[0]->slug;
}
else
{
$taxonomy_slug = 'no-location' ;
}
return str_replace ( '%location%' , $taxonomy_slug , $permalink );
}
add_filter( 'post_link' , 'location_permalink' , 10, 3);
add_filter( 'post_type_link' , 'location_permalink' , 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment