Last active
March 7, 2017 20:51
-
-
Save PauliusKrutkis/2b280ce508bf31dbc8373005febda50b to your computer and use it in GitHub Desktop.
Wordpress custom post types - remove slug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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