Rewrite for multiple Custom Post Type
-
Hey π
I have two custom post types project and company, and i manage to have an url like /company/company-name/project/
add_action('admin_init', 'flush_rewrite_rules'); add_action('generate_rewrite_rules', 'company_rewrite_rules'); function company_rewrite_rules( $wp_rewrite ) { $new_rules = array( 'company/(.+)/(.+)' => 'index.php?post_type=' . $wp_rewrite->preg_index(2) . '&company=' .$wp_rewrite->preg_index(1), 'company/(.+)/(.+)/(.+)' => 'index.php?post_type=' . $wp_rewrite->preg_index(2) . '&company=' .$wp_rewrite->preg_index(1). '&project=' .$wp_rewrite->preg_index(3) ); $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; }
Next step i guess π was to change the template based on the URL so, i did create a template redirection base on a test of the CPT
function get_custom_post_type_template($single_template) { global $wp_query; $post_type = $wp_query->query_vars['post_type']; if ( !isset($wp_query->query_vars['company'] ) ) return $template; $filter = $wp_query->query_vars['company']; if ($post_type == 'project' && $filter ) { $template = dirname( __FILE__ ) . '/project-template.php'; } return $template; } add_filter( 'single_template', 'get_custom_post_type_template' ) ;
Rewrite Analyser show me than it finding some value : post_type: project/ and company: company-name, wish is great but i guess i should in this case see an archive page with project for the company company-name …
an echo of get_query_var(‘post_type’); return company, and $wp_query->query_vars[‘post_type’]; return company too. Should it not be project ?
I think it’s should not be a big issue has many wordpress using complex url to filter that, and everything cannot be manage by taxonomy. So, i thing there is something i don’t have pickup.
i simply want to have urls like
/company/company-name/project/ showing all the projects for company-name (archive project page like)
/company/company-name/project/project-name showing project-name (single-project page like)Any help, or hint would be nice. I did already pass few days on it testing everything ;(
Thank u
- The topic ‘Rewrite for multiple Custom Post Type’ is closed to new replies.