Advertisement
mbis

Exclude taxonomy when URI is duplicated

Apr 4th, 2020
373
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.90 KB | None | 0 0
  1. function bis_duplicated_uri($element_id, $uri_parts, $request_url) {
  2.     global $permalink_manager_uris, $wpdb;
  3.    
  4.     if(isset($_GET['test']) && !empty($uri_parts['uri'])) {    
  5.         // Get detected URI
  6.         $uri = $uri_parts['uri'];
  7.        
  8.         // Check if URI is used more than once
  9.         $duplicates = array_keys($permalink_manager_uris, $uri);
  10.        
  11.         if(!empty($duplicates) && count($duplicates) > 1) {
  12.             $duplicates = array_map(function($value) { return str_replace('tax-', '', $value); }, $duplicates);
  13.             $term_ids = implode(", ", $duplicates);
  14.            
  15.             // Exclude the post categories
  16.             $new_term_id = $wpdb->get_var("SELECT term_id FROM {$wpdb->term_taxonomy} AS tt WHERE tt.taxonomy != 'category' AND term_ID IN ({$term_ids}) ORDER BY term_id DESC LIMIT 1");
  17.         }
  18.     }
  19.    
  20.     return (!empty($new_term_id))? "tax-{$new_term_id}" : $element_id;
  21. }
  22. add_filter('permalink_manager_detected_element_id', 'bis_duplicated_uri', 10, 3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement