Advertisement
IlyaOlchikov

post_type in url

May 24th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.71 KB | None | 0 0
  1. function na_remove_slug( $post_link, $post, $leavename ) {
  2.  
  3.     if (!in_array($post->post_type, ['article', 'work']) || 'publish' != $post->post_status ) {
  4.         return $post_link;
  5.     }
  6.  
  7.     $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
  8.  
  9.     return $post_link;
  10. }
  11. add_filter( 'post_type_link', 'na_remove_slug', 10, 3 );
  12.  
  13.  
  14. function na_parse_request( $query ) {
  15.  
  16.     if ( ! $query->is_main_query() || 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
  17.         return;
  18.     }
  19.  
  20.     if ( ! empty( $query->query['name'] ) ) {
  21.         $query->set( 'post_type', array( 'post', 'work', 'page', 'article' ) );
  22.     }
  23. }
  24. add_action( 'pre_get_posts', 'na_parse_request' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement