Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.00 KB | None | 0 0
  1. function oldurl_init() {
  2.     // create a new taxonomy
  3.     register_taxonomy(
  4.         'oldurl',
  5.         'post',
  6.         array(
  7.             'label' => __( 'Old URL' ),
  8.             'rewrite' => array( 'slug' => 'oldurl' ),
  9.             'hierarchical'      => false,
  10.            
  11.         )
  12.     );
  13. }
  14. add_action( 'init', 'oldurl_init' );
  15.  
  16. function flushRules(){
  17.     global $wp_rewrite;
  18.     $wp_rewrite->flush_rules();
  19. }
  20. add_filter('init','flushRules');  
  21.  
  22. function custom_rewrite_basic() {
  23. global $post;
  24.  
  25. $args = array(
  26.     'date_query' => array(
  27.         array(
  28.             'year' => date( 'Y' ),
  29.             'week' => date( 'W' ),
  30.         ),
  31.     ),
  32. );
  33.         $getOldUrl = new WP_Query( $args );
  34.  
  35. // The Loop
  36. if ( $getOldUrl->have_posts() ) {
  37.    
  38.     while ( $getOldUrl->have_posts() ) {
  39.         $getOldUrl->the_post();
  40.          $terms = wp_get_post_terms( get_the_ID(), 'oldurl' );
  41.     if ( ! empty( $terms ) && ! is_wp_error( $terms ) )
  42.        
  43.         {
  44.             $trimmedTermName = ltrim($terms[0]->name, '/');
  45.             $toRedirect = "index.php?page_id=" . get_the_ID();
  46.          add_rewrite_rule( $trimmedTermName, $toRedirect, 'top');
  47.  
  48.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement