I set up 2 custom post types: beats and tracks. And I set up the taxonomy 'tags' and 'style' for both custom post types.
the permalinks for the custom post types are set in functions.php: beats/%style%/%postname% and tracks/%style%/%postname%
I have a problem, when the url is /beats/%style% and /tracks/%style% without the postname (archive). Then, all 'beats' and 'tracks' posts whiches are in the %style%, are visible. Not only the beats or only the tracks from the style tax.
When I make an echo of get_post_type(), the echo always is 'beats'. Even when the url is /tracks/%style%.
Here is my code:
<?php
add_action('init', 'reg_music');
function reg_music() {
register_taxonomy(
'style',
array (
0 => 'beats',
1 => 'tracks',
),
array(
'label' => 'Styles',
'singular_label' => 'Style',
'hierarchical' => true,
'query_var' => true,
'rewrite' => array('slug' => 'beats','slug' => 'tracks'),
)
);
$labels = array(
'name' => _x('Beats', 'post type general name'),
'singular_name' => _x('Beat', 'post type singular name')
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => 'beats',
'capability_type' => 'post',
'hierarchical' => true,
'menu_position' => null,
'supports' => array('title','editor','excerpt','trackbacks','custom-fields','comments','revisions','thumbnail','author','page-attributes',),
'rewrite' => array(
'slug' => 'beats/%style%',
'with_front' => false
),
'has_archive' => true
);
register_post_type( 'beats' , $args );
$labels = array(
'name' => _x('Tracks', 'post type general name'),
'singular_name' => _x('Track', 'post type singular name')
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => 'tracks',
'capability_type' => 'post',
'hierarchical' => true,
'menu_position' => null,
'supports' => array('title','editor','excerpt','trackbacks','custom-fields','comments','revisions','thumbnail','author','page-attributes',),
'rewrite' => array(
'slug' => 'tracks/%style%',
'with_front' => false
),
'has_archive' => true
);
register_post_type( 'tracks' , $args );
// flush_rewrite_rules();
}
add_filter('post_type_link', 'music_permalink_structure', 10, 4);
function music_permalink_structure($post_link, $post, $leavename, $sample)
{
if ( false !== strpos( $post_link, '%style%' ) ) {
$event_type_term = get_the_terms( $post->ID, 'style' );
$post_link = str_replace( '%style%', array_pop( $event_type_term )->slug, $post_link );
}
return $post_link;
}
register_taxonomy('tags',array (
0 => 'beats',
1 => 'tracks',
),array( 'hierarchical' => false, 'label' => 'Tags','show_ui' => true,'query_var' => true,'rewrite' => array('slug' => 'tag'),'singular_label' => 'Tag') );
?>