I've created 2 custom post types
like so:
register_post_type(
'resources',
theme_build_post_args(
// $slug, $singular, $plural
'resources', 'Resource', 'Resources',
array(
'menu_icon' => get_template_directory_uri().'/assets/build/vectors/backend-icon.svg',
'menu_position' => 20,
'has_archive' => true,
'public' => true,
'supports' => array('title', 'revisions', 'thumbnail', 'editor', 'author'),
'taxonomies' => array('tags'),
// 'rewrite' => array( 'slug' => 'resources' ),
)
)
);
register_post_type(
'video-demos-tours',
theme_build_post_args(
// $slug, $singular, $plural
'video-demos-tours', 'Video Demos & Tours', 'Video Demos & Tours',
array(
'menu_icon' => get_template_directory_uri().'/assets/build/vectors/backend-icon.svg',
'menu_position' => 23,
'has_archive' => true,
'public' => true,
'supports' => array('title', 'revisions', 'thumbnail', 'editor', 'author'),
'taxonomies' => array('tags'),
// 'rewrite' => array('slug' => 'resources'),
)
)
);
video-demo-tours
shares the same single-
template as the resources
post type, via this function:
function custom_single_template($single_template) {
global $post;
if (in_array($post->post_type, array('video-demos-tours'))) {
return get_stylesheet_directory() . '/single-resources.php';
}
return $single_template;
}
add_filter('single_template', 'custom_single_template');
All posts from resources
and video-demos-tours
are being shown on /resources
But, when I click into a video-demos-tours
post, the URL is /video-demos-tours/post-name
. I want it to be /resources/post-name
.
To do this, I've tried rewrite
:
function custom_post_type_permalink($url, $post) {
if (in_array($post->post_type, array('resources', 'video-demos-tours'))) {
return home_url("resources/{$post->post_type}/{$post->post_name}/");
}
return $url;
}
add_filter('post_type_link', 'custom_post_type_permalink', 10, 2);
But, it didn't change anything (have flushed permalinks after too).
Any ideas?
Edit
Latest CTP
and function:
register_post_type(
'resources',
theme_build_post_args(
// $slug, $singular, $plural
'resources', 'Resource', 'Resources',
array(
'menu_icon' => get_template_directory_uri().'/assets/build/vectors/backend-icon.svg',
'menu_position' => 20,
'has_archive' => true,
'public' => true,
'supports' => array('title', 'revisions', 'thumbnail', 'editor', 'author'),
'taxonomies' => array('tags'),
)
)
);
register_post_type(
'video-demos-tours',
theme_build_post_args(
// $slug, $singular, $plural
'video-demos-tours', 'Video Demos & Tours', 'Video Demos & Tours',
array(
'menu_icon' => get_template_directory_uri().'/assets/build/vectors/backend-icon.svg',
'menu_position' => 23,
'has_archive' => true,
'public' => true,
'supports' => array('title', 'revisions', 'thumbnail', 'editor', 'author'),
'taxonomies' => array('tags'),
'rewrite' => array( 'slug' => 'resources' )
)
)
);
add_action( 'parse_request', 'wpse418900_parse_request' );
function wpse418900_parse_request( $wp ) {
$post_types = array( 'resources', 'video-demos-tours' );
if ( preg_match( '#^resources/#', $wp->request ) &&
isset( $wp->query_vars['post_type'], $wp->query_vars['name'] ) &&
in_array( $wp->query_vars['post_type'], $post_types )
) {
$wp->query_vars['post_type'] = $post_types;
}
}
I have flushed permalinks via settings but my posts still sit under /video-demos-tours/video-post/
To clarify (incase misunderstood), I want my video posts to sit under the resources CPT slug, as in /resources/video-post/
.
Edit 2 ##:
Full list of CPTs and taxonomies:
public function post_types_taxonomies() {
register_post_type(
'resources',
theme_build_post_args(
// $slug, $singular, $plural
'resources', 'Resource', 'Resources',
array(
'menu_icon' => get_template_directory_uri().'/assets/build/vectors/backend-icon.svg',
'menu_position' => 20,
'has_archive' => true,
'public' => true,
'supports' => array('title', 'revisions', 'thumbnail', 'editor', 'author'),
'taxonomies' => array('tags'),
)
)
);
register_post_type(
'blogs-and-news',
theme_build_post_args(
// $slug, $singular, $plural
'blogs-and-news', 'Blogs and News', 'Blogs and News',
array(
'menu_icon' => get_template_directory_uri().'/assets/build/vectors/backend-icon.svg',
'menu_position' => 21,
'has_archive' => false,
'public' => true,
'supports' => array('title', 'revisions', 'thumbnail', 'editor', 'author'),
'taxonomies' => array('tags'),
'rewrite' => array('slug' => 'resources/blogs-and-news'),
)
)
);
register_post_type(
'case-studies',
theme_build_post_args(
// $slug, $singular, $plural
'case-studies', 'Case study', 'Case studies',
array(
'menu_icon' => get_template_directory_uri().'/assets/build/vectors/backend-icon.svg',
'menu_position' => 22,
'has_archive' => false,
'public' => true,
'supports' => array('title', 'revisions', 'thumbnail', 'editor', 'author'),
'taxonomies' => array('tags'),
'rewrite' => array('slug' => 'case-studies', 'with_front' => false),
)
)
);
register_post_type(
'video-demos-tours',
theme_build_post_args(
// $slug, $singular, $plural
'video-demos-tours', 'Video Demos & Tours', 'Video Demos & Tours',
array(
'menu_icon' => get_template_directory_uri().'/assets/build/vectors/backend-icon.svg',
'menu_position' => 23,
'has_archive' => true,
'public' => true,
'supports' => array('title', 'revisions', 'thumbnail', 'editor', 'author'),
'taxonomies' => array('tags'),
'rewrite' => array( 'slug' => 'resources/video-demos-tours' )
)
)
);
register_post_type(
'whitepapers',
theme_build_post_args(
// $slug, $singular, $plural
'whitepapers-and-reports', 'Whitepaper & Reports', 'Whitepapers & Reports',
array(
'menu_icon' => get_template_directory_uri().'/assets/build/vectors/backend-icon.svg',
'menu_position' => null,
'has_archive' => false,
'public' => true,
'supports' => array('title', 'revisions', 'thumbnail', 'author'),
'taxonomies' => array('tags'),
'rewrite' => array('slug' => 'resources/whitepapers-and-reports'),
)
)
);
register_post_type(
'solution-briefs',
theme_build_post_args(
// $slug, $singular, $plural
'solution-briefs', 'Solution brief', 'Solution briefs',
array(
'menu_icon' => get_template_directory_uri().'/assets/build/vectors/backend-icon.svg',
'menu_position' => 25,
'has_archive' => false,
'public' => true,
'supports' => array('title', 'revisions', 'thumbnail', 'author'),
'taxonomies' => array('tags'),
'rewrite' => array('slug' => 'resources/solution-briefs'),
)
)
);
register_post_type(
'user-guides',
theme_build_post_args(
// $slug, $singular, $plural
'user-guides', 'User guide', 'User guides',
array(
'menu_icon' => get_template_directory_uri().'/assets/build/vectors/backend-icon.svg',
'menu_position' => 26,
'has_archive' => false,
'public' => true,
'supports' => array('title', 'revisions', 'thumbnail', 'editor', 'author'),
'taxonomies' => array('tags'),
'rewrite' => array('slug' => 'resources/user-guides'),
)
)
);
register_post_type(
'product-briefs',
theme_build_post_args(
// $slug, $singular, $plural
'product-briefs', 'Product brief', 'Product briefs',
array(
'menu_icon' => get_template_directory_uri().'/assets/build/vectors/backend-icon.svg',
'menu_position' => 27,
'has_archive' => false,
'public' => true,
'supports' => array('title', 'revisions', 'thumbnail', 'editor', 'author'),
'taxonomies' => array('tags'),
'rewrite' => array('slug' => 'resources/product-briefs'),
)
)
);
register_post_type(
'webinars-whiteboards',
theme_build_post_args(
// $slug, $singular, $plural
'webinars-whiteboards', 'Webinars & Whiteboards', 'Webinars & Whiteboards',
array(
'menu_icon' => get_template_directory_uri().'/assets/build/vectors/backend-icon.svg',
'menu_position' => 28,
'has_archive' => false,
'public' => true,
'supports' => array('title', 'revisions', 'thumbnail', 'editor', 'author'),
'taxonomies' => array('tags'),
'rewrite' => array('slug' => 'resources/webinars-whiteboards'),
)
)
);
}
function themes_taxonomy() {
$post_types = array('resources', 'blogs-and-news', 'case-studies', 'video-demos-tours', 'whitepapers', 'solution-briefs', 'user-guides', 'product-briefs', 'webinars-whiteboards');
$taxonomy_args = array(
'hierarchical' => true,
'public' => true,
'rewrite' => array('slug' => 'resources/tag'),
'label' => 'Tags',
'query_var' => true,
'show_admin_column' => true,
'publicly_queryable' => true,
);
foreach ($post_types as $post_type) {
register_taxonomy(
'tags', // taxonomy name/slug
$post_type, // attach to this post type
$taxonomy_args
);
}
}
theme_build_post_args()
:
public function buildPostLabels( $singular = 'Post', $plural = 'Posts' )
{
if($singular != 'Post' && $plural == 'Posts' ) {
$plural = $singular . 's';
}
$labels = array(
'name' => _x($plural, 'post type general name', 'lightbox'),
'singular_name' => _x($singular, 'post type singular name', 'lightbox'),
'menu_name' => _x($plural, 'admin menu', 'lightbox'),
'name_admin_bar' => _x($singular, 'add new on admin bar', 'lightbox'),
'add_new' => _x('Add New', $singular, 'lightbox'),
'add_new_item' => __('Add New ' . $singular, 'lightbox'),
'new_item' => __('New ' . $singular, 'lightbox'),
'edit_item' => __('Edit ' . $singular, 'lightbox'),
'view_item' => __('View ' . $singular, 'lightbox'),
'all_items' => __('All ' . $plural, 'lightbox'),
'search_items' => __('Search ' . $plural, 'lightbox'),
'parent_item_colon' => __('Parent ' . $plural . ':', 'lightbox'),
'not_found' => __('No ' . strtolower($plural) . ' found.', 'lightbox'),
'not_found_in_trash' => __('No ' . strtolower($plural) . ' found in Trash.', 'lightbox'),
);
return $labels;
}
public function buildPostArgs( $slug, $singular = 'Post', $plural = 'Posts', $args = array() )
{
$args = wp_parse_args($args, $this->postDefaults);
$args['rewrite']['slug'] = $slug;
$args['labels'] = $this->buildPostLabels($singular, $plural);
return $args;
}
function theme_build_post_args( $slug, $singular = 'Post', $plural = 'Posts', $args = array() )
{
$builder = new theme_PTTaxArgBuilder;
return $builder->buildPostArgs($slug, $singular, $plural, $args);
}