Rewrite url for custom post type

I’m using WPML plugin to translate my site.
I have a custom post type called “vinos” and I use this args:

$args = array( 
            'labels' => $labels,
            'hierarchical' => false,
            'supports' => array('title', 'thumbnail'),
            'public' => true,
            'show_ui' => true,
            'show_in_menu' => true,
            'menu_position' => 6,
            'show_in_nav_menus' => true,
            'publicly_queryable' => true,
            'exclude_from_search' => false,
            'has_archive' => 'nuestros-vinos/catalogo',
            'query_var' => true,
            'can_export' => true,
            'capability_type' => 'post',
            'rewrite' => array('slug' => 'nuestros-vinos/catalogo/marcas/%marcas%')
    );
    register_post_type('vinos', $args);

The problem is that I can’t translate my string “nuestros-vinos/catalogo/marcas” to english and for that reason my urls are:

Read More

www.domain.com/nuestros-vinos/catalogo/ ———> show all my list of wines in spanish
www.domain.com/en/nuestros-vinos/catalogo/ ——> show all my list of wines in english

but I would like that in english version, the url was:

www.domain.com/en/our-wines/catalog/

I’m trying to use rewrite rules:

add_filter('generate_rewrite_rules', 'customposttype_rewrites');
function customposttype_rewrites($wp_rewrite) {
    $newrules = array();
    $newrules['en/our-wines/catalog/?$'] = 'en/index.php?post_type=vinos';
    $wp_rewrite->rules = $newrules + $wp_rewrite->rules;
}

But it doesn’t work, always page not found.

How can I do it?

Thanks.

Related posts

2 comments

  1. this will not work:

    'en/index.php?post_type=vinos'
    

    there is no en/index.php, it has to be:

    'index.php?post_type=vinos'
    

    if you need to detect en in the path, add a query var, then set that query var in your rewrite:

    function wpa_query_vars( $qvars ) {
        $qvars[] = 'wpa_lang';
        return $qvars;
    }
    add_filter( 'query_vars', 'wpa_query_vars' );
    

    then in your rewrite rule:

    $newrules['en/our-wines/catalog/?$'] = 'index.php?post_type=vinos&wpa_lang=en';
    
  2. Try the solution mentioned here:
    http://wpengineer.com/2044/custom-post-type-and-permalink/
    which basically entails re-saving your permalink settings once again.

    Example: set permalinks to default structure, save. Switch to your desired permalink structure, save again and then check again if your custom post permalink issue persists.

    I have had 404 issues in the past with custom post permalinks and the solution mentioned above worked for me.

    Edit: Also, hopefully you don’t have enabled any caching plugins while you are coding, just forgot to mention this.

Comments are closed.