Skip to content

Instantly share code, notes, and snippets.

@PauliusKrutkis
Last active March 7, 2017 20:51
Show Gist options
  • Save PauliusKrutkis/2b280ce508bf31dbc8373005febda50b to your computer and use it in GitHub Desktop.
Save PauliusKrutkis/2b280ce508bf31dbc8373005febda50b to your computer and use it in GitHub Desktop.
Wordpress custom post types - remove slug
<?php
function queryPareRequest($query)
{
if (
!$query->is_main_query()
|| 2 != count($query->query)
|| !isset($query->query['page'])
) {
return;
}
if (!empty($query->query['name'])) {
$query->set('post_type', ['post', 'products', 'page']);
}
}
add_action('pre_get_posts', 'queryPareRequest');
function productsRemoveSlug($postLink, $post)
{
if ('products' != $post->post_type || 'publish' != $post->post_status) {
return $postLink;
}
$postLink = str_replace('/'.$post->post_type.'/', '/', $postLink);
return $postLink;
}
add_filter('post_type_link', 'productsRemoveSlug', 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment