stefanbc
9/19/2013 - 7:32 AM

These functions remove the slug from a permalink for a custom post type. These should be placed in functions.php in your theme and the perma

These functions remove the slug from a permalink for a custom post type. These should be placed in functions.php in your theme and the permalink structure should be /%postname%/

<?php
add_filter('post_type_link','custom_post_type_link',10,3); 
function custom_post_type_link($permalink, $post, $leavename) {
 
  $url_components = parse_url($permalink);
  $post_path = $url_components['path'];
  $post_name = end(explode('/', trim($post_path, '/')));
 
  if(!empty($post_name)) {
 
    switch($post->post_type) {
 
      case 'CUSTOM_POST_TYPE_NAME':
        $permalink = str_replace($post_path, '/' . $post_name . '/', $permalink);
        break;
 
    }
 
  }
 
  return $permalink;
}

function custom_pre_get_posts($query) {
    global $wpdb;
 
    if(!$query->is_main_query()) {
      return;
    }
 
    $post_name = $query->get('name');
 
    $post_type = $wpdb->get_var(
      $wpdb->prepare(
        'SELECT post_type FROM ' . $wpdb->posts . ' WHERE post_name = %s LIMIT 1',
        $post_name
      )
    );
 
    switch($post_type) {
      case 'CUSTOM_POST_TYPE_NAME':
        $query->set('CUSTOM_POST_TYPE_NAME', $post_name);
        $query->set('post_type', $post_type);
        $query->is_single = true;
        $query->is_page = false;
        break;
    }

    return $query;
}
add_action('pre_get_posts','custom_pre_get_posts');
?>