permastruct for custom post type not working in one of four cases

You’re currently placing your rewrite rules in the global scope, which means they execute as soon as your file is loaded, which may be too early. Instead try adding them on the init hook, or rewriting them to use the generate_rewrite_rules filter instead, e.g.:

add_action('generate_rewrite_rules', 'themes_dir_add_rewrites');

function themes_dir_add_rewrites() {
  $theme_name = next(explode('/themes/', get_stylesheet_directory()));

  global $wp_rewrite;
  $new_non_wp_rules = array(
    'css/(.*)'       => 'wp-content/themes/'. $theme_name . '/css/$1',
    'js/(.*)'        => 'wp-content/themes/'. $theme_name . '/js/$1',
    'images/wordpress-urls-rewrite/(.*)'    => 'wp-content/themes/'. $theme_name . '/images/wordpress-urls-rewrite/$1',
  );
  $wp_rewrite->non_wp_rules += $new_non_wp_rules;
}

Taken from Hongkiat

Or

function josfaber_add_rules() {
    global $wp_rewrite;

    $wp_rewrite->add_rewrite_tag("%merkname%", '([^/]+)', "merk=");
    $wp_rewrite->add_permastruct('merk', '/merken/%merkname%', false);

    $wp_rewrite->add_rewrite_tag("%modelname%", '([^/]+)', "model=");
    $wp_rewrite->add_permastruct('model', '/merken/%merkname%/%modelname%', false);

    $wp_rewrite->add_rewrite_tag("%carname%", '([^/]+)', "car=");
    $wp_rewrite->add_permastruct('car', '/merken/%merkname%/%modelname%/%carname%', false);

    $wp_rewrite->add_rewrite_tag("%companyname%", '([^/]+)', "company=");
    $wp_rewrite->add_permastruct('company', '/company/%companyname%', false);
}
add_action( 'init', 'josfaber_add_rules' );

Leave a Comment