0

I know there has been a lot of times this question has been asked here before, but no matter what method I have tried from here, nothing seems to be working for me. I guess I am sure I am writing in the write codes, but still this is not working for me.

I have a website for pizza delivery, where I want the url structure of locations post type and zip codes taxonomy to always have a base pizza-delivery/state, so the url would look like this..

domain.com/pizza-delivery/state/city/
domain.com/pizza-delivery/state/city/zipcode/
domain.com/pizza-delivery/state/city/zipcode/postname

City is the main taxonomy term name, while zip codes will be the sub terms. I have followed the steps mentioned here How to create a permalink structure with custom taxonomies and custom post types like base-name/parent-tax/child-tax/custom-post-type-name but its not working for me at all.

Here is the complete code

function location_post_type() {
    $labels = array(
       'name'               => _x( 'Locations', 'post type general name' ),
       'singular_name'      => _x( 'Location', 'post type singular name' ),
       'add_new'            => _x( 'Add New', 'location' ),
       'add_new_item'       => __( 'Add New Location' ),
       'edit_item'          => __( 'Edit Location' ),
       'new_item'           => __( 'New Location' ),
       'all_items'          => __( 'All Locations' ),
       'view_item'          => __( 'View Location' ),
       'search_items'       => __( 'Search Locations' ),
       'not_found'          => __( 'No Locations found' ),
       'not_found_in_trash' => __( 'No Locations found in the Trash' ), 
       'parent_item_colon'  => '',
       'menu_name'          => 'Locations'
);

$args = array(
    'labels'        => $labels,
    'public'        => true,
    'publicly_queryable' => true,
    'menu_position' => 5,
    'supports'      => array( 'title' ),
    'has_archive'   => true,
    'hierarchical'  => false,
    'query_var'     => true,
    'rewrite'       => array( 'slug' => 'pizza-delivery/georgia/%zip_codes%', 'with_front' => false )
);

register_post_type( 'locations', $args ); 
}

add_action( 'init', 'location_post_type' );



function zip_code_taxonomy() {

  register_taxonomy('zip_codes', 'locations', array(

     'hierarchical' => true,
     'query_var'    => true,
     'labels' => array(
         'name' => _x( 'Zip Codes', 'taxonomy general name' ),
         'singular_name' => _x( 'Zip Code', 'taxonomy singular name' ),
         'search_items' =>  __( 'Search Zip Codes' ),
         'all_items' => __( 'All Zip Codes' ),
         'parent_item' => __( 'Parent Zip Code' ),
         'parent_item_colon' => __( 'Parent Zip Code' ),
         'edit_item' => __( 'Edit Zip Code' ),
         'update_item' => __( 'Update Zip Code' ),
         'add_new_item' => __( 'Add New Zip Code' ),
         'new_item_name' => __( 'New Zip Code Name' ),
         'menu_name' => __( 'Cities & Zip Codes' ),
    ),
    'rewrite' => array(
         'slug' => 'pizza-delivery/georgia',
         'with_front' => false,
         'hierarchical' => true
    ),
  ));
}

add_action( 'init', 'zip_code_taxonomy', 0 );

add_filter('rewrite_rules_array', 'mmp_rewrite_rules');

function mmp_rewrite_rules($rules) {
    $newRules  = array();
    $newRules['pizza-delivery/georgia/(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?locations=$matches[4]';
    $newRules['pizza-delivery/georgia/(.+)/?$']                = 'index.php?zip_codes=$matches[2]'; 

    return array_merge($newRules, $rules);
}


function filter_post_type_link($link, $post) {
    if ($post->post_type != 'locations')
        return $link;

    if ($cats = get_the_terms($post->ID, 'zip_codes'))
    {
        $link = str_replace('%zip_codes%', get_taxonomy_parents(array_pop($cats)->term_id, 'zip_codes', false, '/', true), $link); // see custom function defined below
    }
    return $link;
}

add_filter('post_type_link', 'filter_post_type_link', 10, 2);

function get_taxonomy_parents($id, $taxonomy, $link = false, $separator = '/', $nicename = false, $visited = array()) {    
    $chain = '';   
    $parent = &get_term($id, $taxonomy);

    if (is_wp_error($parent)) {
        return $parent;
    }

    if ($nicename)    
        $name = $parent -> slug;        
    else    
        $name = $parent -> name;

    if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, $visited)) {    
        $visited[] = $parent -> parent;    
        $chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $separator, $nicename, $visited);

    }

    if ($link) {
        // nothing, can't get this working :(
    } else    
    $chain .= $name . $separator;    
    return $chain;    
}

Now the thing here is that when I upload this code, I go to the Permalinks page to refresh the permalinks, but then when I go to any taxonomy term or custom post type url of the concerned, it always gets redirected to the index.php page ?

Thanks a lot !

  • I changed $newRules['pizza-delivery/georgia/(.+)/?$'] = 'index.php?zip_codes=$matches[3]'; to $newRules['pizza-delivery/georgia/(.+)/?$'] = index.php?zip_codes=$matches[1]'; and the taxonomy term pages are opening fine now, but the custom post types are giving a page not found – Patrick S May 10 '14 at 9:59
  • why are zip codes hierarchical, and why do you add those extra rules for them? – Milo May 10 '14 at 16:27
  • Thats the way the client wants it to be. Extra rules for what ? – Patrick S May 11 '14 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.