Advertisement
Guest User

Untitled

a guest
Jan 10th, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.22 KB | None | 0 0
  1. function pm_toolset_related_posts_fields($default_uri, $native_slug, $post, $slug, $native_uri) {
  2.     global $wpdb;
  3.  
  4.     // Do not change native permalinks or differnt post types
  5.     if($native_uri) { return $default_uri; }
  6.  
  7.     // List of relationship fields that should be replaced
  8.     $relationship_fields = array(
  9.         'custom-post-1', // Use %__custom-post-1% tag in Permastructure settings
  10.         'custom-post-2', // Use %__custom-post-2% tag in Permastructure settings
  11.     );
  12.  
  13.     foreach($relationship_fields as $field) {
  14.         if(strpos($default_uri, "%__{$field}%") === false) { continue; }
  15.  
  16.         $relationship = $wpdb->get_row($wpdb->prepare("SELECT parent_id, child_id FROM {$wpdb->prefix}toolset_relationships as r LEFT JOIN {$wpdb->prefix}toolset_associations AS a ON r.id = a.relationship_id  WHERE slug = %s", $field));
  17.  
  18.         if(!empty($relationship->parent_id)) {
  19.             $related_post_id = ($post->ID !== $relationship->parent_id) ? $relationship->parent_id : $relationship->child_id;
  20.             $related_post = get_post($related_post_id);
  21.  
  22.             $default_uri = str_replace("%__{$field}%", $related_post->post_name, $default_uri);
  23.         }
  24.     }
  25.  
  26.     return $default_uri;
  27. }
  28. add_filter('permalink_manager_filter_default_post_uri', 'pm_toolset_related_posts_fields', 10, 5);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement