daily pastebin goal
52%
SHARE
TWEET

Untitled

a guest Jan 22nd, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. $args = array(
  2.             'labels'                     => $labels,
  3.             'hierarchical'               => true,
  4.             'public'                     => true,
  5.             'show_ui'                    => true,
  6.             'show_admin_column'          => true,
  7.             'show_in_nav_menus'          => true,
  8.             'show_tagcloud'              => true,
  9.             'rewrite'                    => array( 'slug' => 'filter', 'with_front' => false ),
  10.         );
  11.         register_taxonomy( 'service', 'filter', $args );
  12.    
  13. /* Register CPT
  14. */
  15. function wpse_138987_post_type_filter() {
  16.  register_post_type('filter', array(
  17.     'labels' => array(
  18.         'name' => 'Filter',
  19.         'all_items' => 'All Posts'
  20.     ),
  21.     'public' => true
  22.  ));
  23. }
  24. add_action('init', 'wpse_138987_post_type_filter');
  25.  
  26. // register taxonomy
  27. function wpse_138987_taxonomy_service() {
  28.  register_taxonomy('service', array('filter'), array(
  29.     'labels' => array(
  30.         'name' => 'Service'
  31.     ),
  32.     'hierarchical'               => true,
  33.     'public'                     => true,
  34.     'show_ui'                    => true,
  35.     'show_admin_column'          => true,
  36.     'show_in_nav_menus'          => true,
  37.     'show_tagcloud'              => true,
  38.     'rewrite' => array('slug' => 'filter')
  39. ));
  40. }
  41. add_action('init', 'wpse_138987_taxonomy_service');
  42.  
  43.  
  44. /*
  45.  * Replace Taxonomy slug with Post Type slug in url
  46. */
  47. function taxonomy_slug_rewrite($wp_rewrite) {
  48.   $rules = array();
  49.   // get all custom taxonomies
  50.   $taxonomies = get_taxonomies(array('_builtin' => false), 'objects');
  51.   // get all custom post types
  52.   $post_types = get_post_types(array('public' => true, '_builtin' => false), 'objects');
  53.  
  54.   foreach ($post_types as $post_type) {
  55.     foreach ($taxonomies as $taxonomy) {
  56.  
  57.         // go through all post types which this taxonomy is assigned to
  58.         foreach ($taxonomy->object_type as $object_type) {
  59.  
  60.             // check if taxonomy is registered for this custom type
  61.             if ($object_type == $post_type->rewrite['slug']) {
  62.  
  63.                 // get category objects
  64.                 $terms = get_categories(array('type' => $object_type, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0));
  65.  
  66.                 // make rules
  67.                 foreach ($terms as $term) {
  68.                     $rules[$object_type . '/' . $term->slug . '/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug;
  69.                 }
  70.             }
  71.         }
  72.     }
  73. }
  74. // merge with global rules
  75. $wp_rewrite->rules = $rules + $wp_rewrite->rules;
  76. }
  77. add_filter('generate_rewrite_rules', 'taxonomy_slug_rewrite');
  78.    
  79. archive-filter.php
  80.  single-filter.php
  81.  taxonomy-service.php
  82.    
  83. function generate_taxonomy_rewrite_rules( $wp_rewrite ) {
  84.   $rules = array();
  85.   $post_types = get_post_types( array( 'name' => 'portfolio', 'public' => true, '_builtin' => false ), 'objects' );
  86.   $taxonomies = get_taxonomies( array( 'name' => 'services', 'public' => true, '_builtin' => false ), 'objects' );
  87.   foreach ( $post_types as $post_type ) {
  88.     $post_type_name = $post_type->name; // 'developer'
  89.     $post_type_slug = $post_type->rewrite['slug']; // 'developers'
  90.     foreach ( $taxonomies as $taxonomy ) {
  91.       if ( $taxonomy->object_type[0] == $post_type_name ) {
  92.         $terms = get_categories( array( 'type' => $post_type_name, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0 ) );
  93.         foreach ( $terms as $term ) {
  94.           $rules[$post_type_slug . '/' . $term->slug . '/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug;
  95.           $rules[$post_type_slug . '/' . $term->slug . '/page/?([0-9]{1,})/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug . '&paged=' . $wp_rewrite->preg_index( 1 );
  96.         }
  97.       }
  98.     }
  99.   }
  100.   $wp_rewrite->rules = $rules + $wp_rewrite->rules;
  101. }
  102. add_action('generate_rewrite_rules', 'generate_taxonomy_rewrite_rules');
  103.    
  104. function generate_taxonomy_rewrite_rules( $wp_rewrite ) {
  105.  
  106. $rules = array();
  107.  
  108. $post_types = get_post_types( array('public' => true, '_builtin' => false ), 'objects' );
  109. $taxonomies = get_taxonomies( array('public' => true, '_builtin' => false ), 'objects' );
  110.  
  111. foreach ( $post_types as $post_type ) {
  112.     $post_type_name = $post_type->name;
  113.     $post_type_slug = $post_type->rewrite['slug'];
  114.  
  115.     foreach ( $taxonomies as $taxonomy ) {//echo 'DDDDD'; print_r($taxonomy);
  116.         if ( $taxonomy->object_type[0] == $post_type_name ) {
  117.             $terms = get_categories( array( 'type' => $post_type_name, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0 ) );
  118.             //print_r($terms);
  119.             foreach ( $terms as $term ) {
  120.                 $rules[ $taxonomy->name.'/'. $term->slug . '/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug;
  121.                 $rules[ $taxonomy->name.'/'. $term->slug . '/page/?([0-9]{1,})/?$'] = 'index.php?'.'type='.$post_type_name."&" . $term->taxonomy . '=' . $term->slug . '&paged=' . $wp_rewrite->preg_index( 1 );
  122.  
  123.             }
  124.         }
  125.     }
  126. }
  127.  
  128. $wp_rewrite->rules = $rules + $wp_rewrite->rules;
  129.  
  130. }
  131.  
  132. add_action('generate_rewrite_rules', 'generate_taxonomy_rewrite_rules');
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top