0

I have a custom post type that is introduced with this setting:

$args = array(
            'label'                 => __( 'Location', 'genesis' ),
            'description'           => __( 'Locations', 'genesis' ),
            'labels'                => $labels,
            'supports'              => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'genesis-cpt-archives-settings'),
            'taxonomies'            => array( 'location', 'type_of_location', 'post_tag' ),
            'hierarchical'          => true,
            'public'                => true,
            'show_ui'               => true,
            'show_in_menu'          => true,
            'menu_position'         => 5,
            'show_in_admin_bar'     => true,
            'show_in_nav_menus'     => true,
            'can_export'            => true,
            'has_archive'           => 'location',
            'exclude_from_search'   => false,
            'publicly_queryable'    => true,
            'capability_type'       => 'page',
            'rewrite'       => array('slug' => 'location/%locations%','with_front' => false),
        );
        register_post_type( 'location', $args );

Additionally I have added three taxonomy's to this custom post type with this command:

register_taxonomy( 'kind_of_location', 'location', array(
        'hierarchical' => true,
        'labels' => $labels,
        'query_var' => true,
        'show_admin_column' => true,
        'rewrite' => array(
            'slug' => 'location/kind', // This controls the base slug that will display before each term
        ),
    ) );

Then I added this rewrite logic to replace the %locations% in my CPT URL setting:

add_filter('post_link', 'category_permalink_locations', 1, 3);
add_filter('post_type_link', 'category_permalink_locations', 1, 3);

function category_permalink_locations($permalink, $post_id, $leavename) {
    if (strpos($permalink, '%locations%') === FALSE) return $permalink;
        // Get post
        $post = get_post($post_id);
        if (!$post) return $permalink;

        // Get taxonomy terms
        $terms = wp_get_object_terms($post->ID, 'kind_of_location');
        if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
            $taxonomy_slug = $terms[0]->slug;
        else $taxonomy_slug = 'sonstiges';

    return str_replace('%locations%', $taxonomy_slug, $permalink);
}

This works fine unless I am on my taxanomy archive page and click on the link to see the previous posts - then I get an 404.

Something must be wrong in my setup ;-) Does anybody knows why?

Thanks!

0

I did solve it by myself - I had to register the taxonomy BEFORE the post type...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.