WordPress: Rewrite Rules for Custom Post Types

Note to self: When added a product custom post type, WP will display /product/item in the URL. To display /brand/item instead of /product/item, simply do …

function owl_add_rewrites() { global $wp_rewrite;

$owl_nwpr = array( ‘brand1/([^/]+)’ => ‘index.php?product=$1’, ‘brand2/([^/]+)’ => ‘index.php?product=$1’ );

$wp_rewrite->non_wp_rules = $owl_nwpr + $wp_rewrite->non_wp_rules; }

function owl_flush_rewrites() { global $wp_rewrite;

$wp_rewrite->flush_rules(); }

add_action(‘generate_rewrite_rules’, ‘owl_add_rewrites’); add_action(‘admin_init’, ‘owl_flush_rewrites’);

Everyday comes with new things to add to this tiny brain.