404 page for post site with custom post type

Question

I am stuck on a problem with a custom post type and own taxonomy.

The archive pages are displayed correct. So:

  • page.com/videotraining/ <- working
  • page.com/videotraining/’cpt-title’/ <- working
  • page.com/videotraining/’cpt-title”https://wordpress.stackexchange.com/”post-title’/ <- 404 error

Any ideas what I am doing wrong?
Thanks for any ideas, hints and input.

I created the CPT like this:

function create_posttype_videotraining() {

    register_post_type( 'videotraining',
        // CPT Options
        array(
            'labels' => array(
                'name' => __( 'Video Trainings' ),
                'singular_name' => __( 'Video Training' ),
            ),
            'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail' ),
            'public' => true,
            'has_archive' => __( 'videotraining' ),
            //'slug' => 'kurs',
            'show_in_rest' => true,
            'menu_position'       => 5,
            'exclude_from_search' => false,
            'menu_icon'           => 'dashicons-format-video',
            'rewrite'             => array('slug' => 'videotraining/%vtthema%'),
            //'rewrite'             => array('slug' => 'videotraining'),
            'taxonomies'          => array( 'videotrainingtopic','anbieter','referent' ),
            'publicly_queryable'  => true,
        )
    );
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype_videotraining' );

taxonomy code:

function custom_taxonomy_video_topic() {

    $labels = array(
        ...
    );

    register_taxonomy(
        'videotrainingtopic', // taxonomy name
        array('videotraining'), // post type name
        array(
            'hierarchical' => true,
            'labels' => $labels,
            'show_ui' => true,
            'show_admin_column' => true,
            'query_var' => true,
            'show_in_rest' => true,
            'rewrite' => array( 'slug' => 'videotraining', 'with_front' => false),
        )
    );
}
add_action( 'init', 'custom_taxonomy_video_topic', 0 );

And for the code for the rewrite rule (using post_type_link hook)

function vt_post_link( $post_link, $id = 0 ){
    $post = get_post($id);

    // general term slug
    $termSlug = wp_get_object_terms( $post->ID, 'videotrainingtopic' )[0]->slug;

    // check if pirmary term is defined
    if ( class_exists('WPSEO_Primary_Term') ) {
        $wpseo_primary_term = new WPSEO_Primary_Term( 'videotrainingtopic', $post->ID );
        $term = get_term( $wpseo_primary_term->get_primary_term() );

        if (!is_wp_error($term)) {
            $termSlug = $term->slug;
        }
    }

    // replace url with slug
    if( $termSlug ){
        return str_replace( '%vtthema%' , $termSlug , $post_link );
    }
    return $post_link;
}
add_filter( 'post_type_link', 'vt_post_link', 1, 3 );

0
Waldgeist 1 month 2021-04-07T13:31:22-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse