0

I search a lot but couldn't get, just got partial answer , I declared custom post type "store" with taxonomy "city", what I am trying is to make list of store based on city list and make URL like post type = store taxonomy = city <posttype slug><taxonomy term>

.com/store/tokyo/my_store_name

some how I achieved it by using this code. Custom Post Type / Taxonomy Slug / Post Title with post type archive but now how use custom template , I tried "archive-city.php, single-store.php, taxonomy-city.php, and couple of more standard combination but all landed on 404.

2
  • if you're receiving 404s, then the code you linked is not working, it has nothing to do with templates. have you flushed rewrite rules?
    – Milo
    Dec 20, 2014 at 1:43
  • Yes I did, here is code of my Post type
    – user4517
    Dec 20, 2014 at 1:48

1 Answer 1

0
function my_custom_post_work() {
    $labels = array(
        'name'               => _x( 'Store', 'post type general name' ),
        'singular_name'      => _x( 'Store', 'post type singular name' ),
        'add_new'            => _x( 'Add New', 'Store' ),
        'add_new_item'       => __( 'Add New Store' ),
        'edit_item'          => __( 'Edit Store' ),
        'new_item'           => __( 'New Store' ),
        'all_items'          => __( 'All Store' ),
        'view_item'          => __( 'View Store' ),
        'search_items'       => __( 'Search Store' ),
        'not_found'          => __( 'No Store found' ),
        'not_found_in_trash' => __( 'No store found in the Trash' ), 
        'parent_item_colon'  => '',
        'menu_name'          => 'Our Store'
    );
$args = array(
        'labels'        => $labels,
        'description'   => 'Holds our works and work specific data',
        'public'        => true,
        'menu_position' => 5,
        'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
        'has_archive'   => true,
        'rewrite'       => array( 'slug' => 'store/%store_location%')
    );
    register_post_type( 'ss_store', $args );  

}


add_action( 'init', 'my_custom_post_work' );

$labels = array(
        'name'                       => 'Cities',
        'singular_name'              => 'City',
        'menu_name'                  => 'City',
        'all_items'                  => 'All Cities',
        'parent_item'                => 'Parent City',
        'parent_item_colon'          => 'Parent City:',
        'new_item_name'              => 'New City Name',
        'add_new_item'               => 'Add New City',
        'edit_item'                  => 'Edit City',
        'update_item'                => 'Update City',
        'separate_items_with_commas' => 'Separate City with commas',
        'search_items'               => 'Search Cities',
        'add_or_remove_items'        => 'Add or remove Cities',
        'choose_from_most_used'      => 'Choose from the most used Cities',
    );

    $args = array(
        'labels'                     => $labels,
        //'hierarchical'               => false,
        'public'                     => true,
        'show_ui'                    => true,
        'query_var'                  => true,
        'show_admin_column'          => true,
        'show_in_nav_menus'          => true,
        'show_tagcloud'              => false,
        'has_archive'                => true

    );


    register_taxonomy( 'city', 'ss_store', $args );


}
add_action( 'init', 'mav_taxonomies_work', 0 );

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

function filter_post_type_link( $post_link, $id = 0, $leavename = FALSE ) {
    if ( strpos('%store_location%', $post_link) === 'FALSE' ) {
      return $post_link;
    }
    $post = get_post($id);
    if ( !is_object($post) || $post->post_type != 'ss_store' ) {
      return $post_link;
    }
    $terms = wp_get_object_terms($post->ID, 'store_location');
    if ( !$terms ) {
      return str_replace('store/%store_location%/', '', $post_link);
    }
    return str_replace('%store_location%', $terms[0]->slug, $post_link);
}


add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
add_action( 'wp_loaded','my_flush_rules' );  

/// flush  
function my_flush_rules(){
    $rules = get_option( 'rewrite_rules' );
            global $wp_rewrite;
    $wp_rewrite->flush_rules();
} 

// Also, you will have to write rewrite rules for your custom post type for your proposed URI structure to work.

// Add the below code to your theme's functions.php file:


 `
// Adding a new rule    
function my_insert_rewrite_rules( $rules )    
{
    $newrules = array();
    $newrules['store/?$'] = 'index.php?post_type=ss_store';
    $newrules['store/page/?([0-9]{1,})/?$'] = 'index.php?post_type=ss_work&paged=$matches[1]';
    $newrules['store/(.+?)/page/?([0-9]{1,})/?$'] = 'index.php?post_type=ss_store&city=$matches[1]&paged=$matches[2]';
    //print_r($rules);
    return $newrules + $rules;

this is the code I used... permalinks are formatted as wanted but now template not loading...404

2
  • Please add your code to your question unless you want to answer your own question with a working solution. What happens when you re-save your permalinks? Dec 21, 2014 at 2:01
  • Nothing happens, permalink structure is ok, Reseting permalink from Setting/Permalink still results same, 404, even i am unable to see custom post single view..on default template.
    – user4517
    Dec 21, 2014 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.