document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. <?php
  2. // Ensure MetaBox Relationships is loaded, manually require if needed
  3. if ( ! function_exists( \'mb_relationships_get_connected\' ) && file_exists( WP_PLUGIN_DIR . \'/meta-box-relationships/meta-box-relationships.php\' ) ) {
  4.     require_once WP_PLUGIN_DIR . \'/meta-box-relationships/meta-box-relationships.php\';
  5. }
  6.  
  7. // Custom permalink structure for City URLs based on relationships
  8. function custom_city_permalink_structure($post_link, $post) {
  9.     if (empty($post_link)) {
  10.         return $post_link;  // Return early if $post_link is not set
  11.     }
  12.  
  13.     if (\'city\' === $post->post_type) {
  14.         // Ensure MetaBox Relationships functions are available
  15.         if (function_exists(\'mb_relationships_get_connected\')) {
  16.             // Get the parent County for this City using correct relationship type
  17.             $county = mb_relationships_get_connected(\'county-to-city\', array(\'to\' => $post->ID));
  18.             if ($county && is_array($county) && !empty($county)) {
  19.                 error_log(\'County found: \' . $county[0]->post_name);
  20.                
  21.                 // Get the parent State for this County using correct relationship type
  22.                 $state = mb_relationships_get_connected(\'state-to-county\', array(\'to\' => $county[0]->ID));
  23.                 if ($state && is_array($state) && !empty($state)) {
  24.                     error_log(\'State found: \' . $state[0]->post_name);
  25.                    
  26.                     // Replace placeholders with actual slug values from the relationships
  27.                     $post_link = str_replace(\'%state%\', $state[0]->post_name, $post_link);
  28.                     $post_link = str_replace(\'%county%\', $county[0]->post_name, $post_link);
  29.                     $post_link = str_replace(\'%city%\', $post->post_name, $post_link);
  30.                 } else {
  31.                     error_log(\'No state found for this county.\');
  32.                 }
  33.             } else {
  34.                 error_log(\'No county found for this city.\');
  35.             }
  36.         }
  37.     }
  38.     error_log(\'Generated post link: \' . $post_link);  // Log the generated post link for debugging
  39.     return $post_link;
  40. }
  41.  
  42. // Hook into wp_loaded to ensure all plugins are fully initialized before running the filter
  43. add_action(\'wp_loaded\', function() {
  44.     add_filter(\'post_type_link\', \'custom_city_permalink_structure\', 10, 2);
  45. }, 20);
  46.  
  47. // Add custom rewrite rule for City URLs
  48. function add_custom_city_rewrites() {
  49.     // Rewrite rule for location/state/county/city
  50.     add_rewrite_rule(
  51.         \'^location/([^/]+)/([^/]+)/([^/]+)/?$\',
  52.         \'index.php?city=$matches[3]&county=$matches[2]&state=$matches[1]\',
  53.         \'top\'
  54.     );
  55. }
  56.  
  57. add_action(\'init\', \'add_custom_city_rewrites\');
  58.  
  59. // Flush rewrite rules to ensure changes take effect
  60. add_action(\'init\', function() {
  61.     flush_rewrite_rules();
  62. });\'
  63. \'
  64. error_log(print_r(mb_relationships_get_connected(\'county-to-city\', array(\'to\' => $post->ID)), true));
  65.  
  66.  
');