mikelittle

Bounce subcat to parent

May 23rd, 2015
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.32 KB | None | 0 0
  1. add_action( 'pre_get_posts', 'bounce_subcat_to_parent' );
  2. function bounce_subcat_to_parent( $query ) {
  3.     if ( is_admin() ) {
  4.         return;
  5.     }
  6.     if ( $query->is_main_query() && $query->is_category ) {
  7.         $cat = get_queried_object();
  8.         //var_dump($cat);
  9.         //exit;
  10.         if ( 0 != $cat->category_parent ) {
  11.             $link = get_category_link( $cat->category_parent );
  12.             $link = str_replace( 'category/', '', $link );
  13.         var_dump($link);
  14.         exit;
  15.             wp_safe_redirect( $link );
  16.         }
  17.     }
  18. }
  19.  
  20. add_action( 'template_redirect', 'bounce_special_post_to_cat' );
  21. function bounce_special_post_to_cat(  ) {
  22.     global $wpdb, $wp_query;
  23.  
  24.     // Guess the current post_type based on the query vars.
  25.     if ( get_query_var('post_type') )
  26.         $post_type = get_query_var('post_type');
  27.     else if ( !empty($wp_query->query_vars['pagename']) )
  28.         $post_type = 'page';
  29.     else   
  30.         $post_type = 'post';
  31.  
  32.     if ( is_singular() && 'post' == $post_type ) {
  33.         global $post;
  34.         $cats = wp_get_object_terms($post->ID, array( 'category'), $args = array( 'fields' => 'all' ) );
  35.         //error_log( var_export( $cats, 1 ));
  36.         foreach( $cats as $cat ) {
  37.             // check for meta data flag
  38.             $flat = get_terms_meta( $cat->term_id, 'flatcategory', true );
  39.             if ( 'checked' == $flat ) {
  40.                 $link = get_category_link( $cat->term_id );
  41.                 wp_safe_redirect( $link );
  42.                 exit();
  43.             }
  44.         }
  45.     }
  46.  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment