Advertisement
mbis

Remove CPT permalink base

Oct 25th, 2024
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.83 KB | None | 0 0
  1. function bis_rewrite_cpt_permalinks( $post_link, $post, $leavename, $sample ) {
  2.     if ( ! empty( $post->post_type ) && in_array( $post->post_type, array( 'video_skrn', 'episodes_skrn' ) ) ) {
  3.         if ( $leavename ) {
  4.             $post_link = home_url( "%{$post->post_type}%" );
  5.         } else {
  6.             $post_link = home_url( $post->post_name );
  7.         }
  8.  
  9.         $post_link = user_trailingslashit( $post_link );
  10.     }
  11.  
  12.     return $post_link;
  13. }
  14. add_filter( 'post_type_link', 'bis_rewrite_cpt_permalinks', 10, 4 );
  15.  
  16. function bis_parse_cpt_permalinks( $query ) {
  17.     if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
  18.         return;
  19.     }
  20.  
  21.     if ( ! empty( $query->query['name'] ) ) {
  22.         $query->set( 'post_type', array( 'post', 'page', 'video_skrn', 'episodes_skrn' ) );
  23.     }
  24. }
  25. add_action( 'pre_get_posts', 'bis_parse_cpt_permalinks' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement