Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. <?php
  2. /**
  3. * Remove the slug from published post permalinks. Only affect our CPT though.
  4. */
  5. function wpex_remove_cpt_slug( $post_link, $post, $leavename ) {
  6. if ( 'portfolio' != $post->post_type || 'publish' != $post->post_status ) {
  7. return $post_link;
  8. }
  9. $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
  10. return $post_link;
  11. }
  12. add_filter( 'post_type_link', 'wpex_remove_cpt_slug', 10, 3 );
  13.  
  14.  
  15. /**
  16. * Some hackery to have WordPress match postname to any of our public post types
  17. * All of our public post types can have /post-name/ as the slug, so they better be unique across all posts
  18. * Typically core only accounts for posts and pages where the slug is /post-name/
  19. */
  20. function wpex_parse_request_tricksy( $query ) {
  21. // Only noop the main query
  22. if ( ! $query->is_main_query() )
  23. return;
  24. // Only noop our very specific rewrite rule match
  25. if ( 2 != count( $query->query ) || ! isset( $query->query['page'] ) ) {
  26. return;
  27. }
  28. // 'name' will be set if post permalinks are just post_name, otherwise the page rule will match
  29. if ( ! empty( $query->query['name'] ) ) {
  30. $query->set( 'post_type', array( 'post', 'portfolio', 'page' ) );
  31. }
  32. }
  33. add_action( 'pre_get_posts', 'wpex_parse_request_tricksy' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement