Advertisement
mbis

Toolset Relationship field

Jan 10th, 2020 (edited)
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * 1. Toolset API
  3.  */
  4. function pm_toolset_related_posts_fields($custom_field_value, $custom_field, $post) {
  5.     // The permastructure tag should be formated like this
  6.     // %__{relationship_slug}%, eg. %__posts_pages%
  7.  
  8.     // Do not change native permalinks
  9.     if($native_uri || empty($post->ID) || !function_exists('toolset_get_relationship')) { return $custom_field_value; }
  10.    
  11.     $relationship = toolset_get_relationship($custom_field);
  12.  
  13.     if(!empty($relationship)) {
  14.         $related_post_id = toolset_get_related_post($post->ID, $custom_field);
  15.  
  16.         if(!empty($related_post_id)) {
  17.             $related_post = get_post($related_post_id);
  18.            
  19.             if(!empty($related_post->post_name)) {
  20.                 $custom_field_value = Permalink_Manager_Helper_Functions::force_custom_slugs($related_post->post_name, $related_post);
  21.             }
  22.         }
  23.     }
  24.  
  25.     return $custom_field_value;
  26. }
  27. add_filter('permalink_manager_custom_field_value', 'pm_toolset_related_posts_fields', 1, 5);
  28.  
  29. /**
  30.  * 2. Legacy variant
  31.  */
  32. function pm_toolset_related_posts_fields($default_uri, $native_slug, $post, $slug, $native_uri) {
  33.     global $wpdb;
  34.    
  35.     // The permastructure tag should be formated like this
  36.     // %__{relationship_slug}%, eg. %__posts_pages%
  37.    
  38.     // Do not change native permalinks
  39.     if($native_uri || empty($post->ID) || !function_exists('toolset_get_relationship')) { return $default_uri; }
  40.    
  41.     // Use it only when custom fields tags are present in the permastructure settings
  42.     if(strpos($default_uri, "%__") === false) { return $default_uri; }
  43.    
  44.     // Use it only for specific post types
  45.     // if(empty($post->post_type) && !in_array($post->post_type, array('cpt1', 'cpt2'))) { return $default_uri; }
  46.    
  47.     // List of available relationship fields that should be replaced
  48.     $relationship_fields = $wpdb->get_col("SELECT slug FROM {$wpdb->prefix}toolset_relationships");
  49.    
  50.     if(!empty($relationship_fields)) {
  51.         foreach($relationship_fields as $field) {
  52.             if(strpos($default_uri, "%__{$field}%") === false) { continue; }
  53.            
  54.             $related_post_id = toolset_get_related_post($post, $field);
  55.            
  56.             if(!empty($related_post_id) && is_numeric($related_post_id)) {
  57.                 $related_post_uri = get_page_uri($related_post_id);
  58.                
  59.                 $default_uri = str_replace("%__{$field}%", $related_post_uri, $default_uri);
  60.             }
  61.         }
  62.     }
  63.     return $default_uri;
  64. }
  65. add_filter('permalink_manager_filter_default_post_uri', 'pm_toolset_related_posts_fields', 5, 5);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement