Skip to content

Instantly share code, notes, and snippets.

@randyjensen
Last active August 29, 2015 14:05
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 randyjensen/3fa5e2752248089c06d9 to your computer and use it in GitHub Desktop.
Save randyjensen/3fa5e2752248089c06d9 to your computer and use it in GitHub Desktop.
CPT URL Rewrite
add_action( 'init', 'mt_create_menus' );
function mt_create_menus() {
$labels = array(
'name' => _x('Food Menus', 'post type general name'),
'singular_name' => _x('Food Menu', 'post type singular name'),
'add_new' => _x('Add New', 'Food Menu'),
'add_new_item' => __('Add New Food Menu'),
'edit_item' => __('Edit Food Menu'),
'new_item' => __('New Food Menu'),
'view_item' => __('View Food Menu'),
'search_items' => __('Search Food Menus'),
'not_found' => __('No food menus found'),
'not_found_in_trash' => __('No food menus found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'supports' => array('title','page-attributes'),
'hierarchical' => false,
'public' => true,
'has_archive' => false,
'capability_type' => 'page',
'rewrite' => false,
'query_var' => 'mt_menu',
);
register_post_type('mt_menu',$args);
}
// Rewrite rules
function add_rewrite_rules() {
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag('%mt_menu%', '([^/]+)', 'mt_menu=');
//the root of the post type, ie mysite.com/movie-reviews/ will be the landing page for the post type
$permalink_prefix = 'location';
//the permalink structure for the post type that will be appended to the prefix, mysite.com/movie-reviews/2010/11/25/test-movie-review/
$permalink_structure = '%parent%/%mt_menu%/';
//we use the WP_Rewrite class to generate all the endpoints WordPress can handle by default.
$rewrite_rules = $wp_rewrite->generate_rewrite_rules($permalink_prefix.'/'.$permalink_structure, EP_ALL, true, true, true, true, true);
//build a rewrite rule from just the prefix to be the base url for the post type
$rewrite_rules = array_merge($wp_rewrite->generate_rewrite_rules($permalink_prefix), $rewrite_rules);
$rewrite_rules[$permalink_prefix.'/?$'] = 'index.php?paged=1';
foreach($rewrite_rules as $regex => $redirect) {
if(strpos($redirect, 'attachment=') === false) {
//add the post_type to the rewrite rule
$redirect .= '&post_type=mt_menu';
}
//turn all of the $1, $2,... variables in the matching regex into $matches[] form
if(0 < preg_match_all('@\$([0-9])@', $redirect, $matches)) {
for($i = 0; $i < count($matches[0]); $i++) {
$redirect = str_replace($matches[0][$i], '$matches['.$matches[1][$i].']', $redirect);
}
}
//add the rewrite rule to wp_rewrite
$wp_rewrite->add_rule($regex, $redirect, 'top');
}
}
function filter_movie_review_link($permalink, $post) {
if(('mt_menu' == $post->post_type) && '' != $permalink && !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
$rewritecode = array(
'%parent%',
'%mt_menu%'
);
$parent = get_field('field_53ff221fb785c', $post->ID);
$parent = $parent->post_name;
$rewritereplace = array(
$parent,
$post->post_name,
);
$permalink = str_replace($rewritecode, $rewritereplace, '/location/%parent%/%mt_menu%/');
$permalink = user_trailingslashit(home_url($permalink));
}
return $permalink;
}
add_action('init', 'add_rewrite_rules');
add_filter('post_type_link', 'filter_movie_review_link', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment