Advertisement
Guest User

rewrite url

a guest
Apr 7th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.79 KB | None | 0 0
  1. add_filter( 'post_type_link', array( $this, 'changeCustomPostTypeUrl' ), 1, 3 );
  2. add_action( 'init', array( $this, 'customPostTypeReWriteRules' ) );
  3.  
  4. function changeCustomPostTypeUrl( $post_link, $id = 0 ) {
  5.             $post = get_post( $id );
  6.             if ( $post->post_type == 'chart' ) {
  7.                 if ( is_object( $post ) ) {
  8.                     $terms = wp_get_object_terms( $post->ID,
  9.                                                   array( 'chart_type' ) );
  10.                     if ( $terms ) {
  11.                         return str_replace( '%cat%',
  12.                                             $terms[0]->slug,
  13.                                             $post_link );
  14.                     }
  15.                 }
  16.             }
  17.  
  18.             return $post_link;
  19.         }
  20.  
  21.         function customPostTypeReWriteRules() {
  22.             add_rewrite_rule( '^hitlister/(.*)/(.*)?$',
  23.                               'index.php?post_type=chart&name=matches[2]',
  24.                               'top' );
  25.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement