View difference between Paste ID: ZCAx62Ne and dUVTwCuE
SHOW: | | - or go back to the newest paste.
1
/**
2
 * 1. Toolset API
3
 */
4-
	// Do not change native permalinks or differnt post types
4+
function pm_toolset_related_posts_fields($custom_field_value, $custom_field, $post) {
5-
	if($native_uri) { return $default_uri; }
5+
	// The permastructure tag should be formated like this
6
	// %__{relationship_slug}%, eg. %__posts_pages%
7-
	// List of relationship fields that should be replaced
7+
8-
	$relationship_fields = array(
8+
	// Do not change native permalinks
9-
		'custom-post-1', // Use %__custom-post-1% tag in Permastructure settings
9+
	if($native_uri || empty($post->ID) || !function_exists('toolset_get_relationship')) { return $custom_field_value; }
10-
		'custom-post-2', // Use %__custom-post-2% tag in Permastructure settings
10+
	
11-
	);
11+
	$relationship = toolset_get_relationship($custom_field);
12
13-
	foreach($relationship_fields as $field) {
13+
	if(!empty($relationship)) {
14-
		if(strpos($default_uri, "%__{$field}%") === false) { continue; }
14+
		$related_post_id = toolset_get_related_post($post->ID, $custom_field);
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));
16+
		if(!empty($related_post_id)) {
17
			$related_post = get_post($related_post_id);
18-
		if(!empty($relationship->parent_id)) {
18+
			
19-
			$related_post_id = ($post->ID !== $relationship->parent_id) ? $relationship->parent_id : $relationship->child_id;
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-
			$default_uri = str_replace("%__{$field}%", $related_post->post_name, $default_uri);
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-
add_filter('permalink_manager_filter_default_post_uri', 'pm_toolset_related_posts_fields', 10, 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);