Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 1. Toolset API
- */
- function pm_toolset_related_posts_fields($custom_field_value, $custom_field, $post) {
- // The permastructure tag should be formated like this
- // %__{relationship_slug}%, eg. %__posts_pages%
- // Do not change native permalinks
- if($native_uri || empty($post->ID) || !function_exists('toolset_get_relationship')) { return $custom_field_value; }
- $relationship = toolset_get_relationship($custom_field);
- if(!empty($relationship)) {
- $related_post_id = toolset_get_related_post($post->ID, $custom_field);
- if(!empty($related_post_id)) {
- $related_post = get_post($related_post_id);
- if(!empty($related_post->post_name)) {
- $custom_field_value = Permalink_Manager_Helper_Functions::force_custom_slugs($related_post->post_name, $related_post);
- }
- }
- }
- return $custom_field_value;
- }
- add_filter('permalink_manager_custom_field_value', 'pm_toolset_related_posts_fields', 1, 5);
- /**
- * 2. Legacy variant
- */
- function pm_toolset_related_posts_fields($default_uri, $native_slug, $post, $slug, $native_uri) {
- global $wpdb;
- // The permastructure tag should be formated like this
- // %__{relationship_slug}%, eg. %__posts_pages%
- // Do not change native permalinks
- if($native_uri || empty($post->ID) || !function_exists('toolset_get_relationship')) { return $default_uri; }
- // Use it only when custom fields tags are present in the permastructure settings
- if(strpos($default_uri, "%__") === false) { return $default_uri; }
- // Use it only for specific post types
- // if(empty($post->post_type) && !in_array($post->post_type, array('cpt1', 'cpt2'))) { return $default_uri; }
- // List of available relationship fields that should be replaced
- $relationship_fields = $wpdb->get_col("SELECT slug FROM {$wpdb->prefix}toolset_relationships");
- if(!empty($relationship_fields)) {
- foreach($relationship_fields as $field) {
- if(strpos($default_uri, "%__{$field}%") === false) { continue; }
- $related_post_id = toolset_get_related_post($post, $field);
- if(!empty($related_post_id) && is_numeric($related_post_id)) {
- $related_post_uri = get_page_uri($related_post_id);
- $default_uri = str_replace("%__{$field}%", $related_post_uri, $default_uri);
- }
- }
- }
- return $default_uri;
- }
- 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