Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function pm_toolset_related_posts_fields($default_uri, $native_slug, $post, $slug, $native_uri) {
- global $wpdb;
- // Do not change native permalinks or differnt post types
- if($native_uri) { return $default_uri; }
- // List of relationship fields that should be replaced
- $relationship_fields = array(
- 'custom-post-1', // Use %__custom-post-1% tag in Permastructure settings
- 'custom-post-2', // Use %__custom-post-2% tag in Permastructure settings
- );
- foreach($relationship_fields as $field) {
- if(strpos($default_uri, "%__{$field}%") === false) { continue; }
- $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));
- if(!empty($relationship->parent_id)) {
- $related_post_id = ($post->ID !== $relationship->parent_id) ? $relationship->parent_id : $relationship->child_id;
- $related_post = get_post($related_post_id);
- $default_uri = str_replace("%__{$field}%", $related_post->post_name, $default_uri);
- }
- }
- return $default_uri;
- }
- 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