1

So I'm really at my wits end here. I'm trying to get the single page working for my custom post type. I've done it several times now, but right now its just not working (404 - not found). I hope I just overlooked something...

Another weird thing is, the archive page for my custom post type is working. Enough talk, here is some code.

in functions.php

function register_my_post_type() {

$labels = [

        "name" => esc_html__( "regies", "my-text-domain" ),

        "singular_name" => esc_html__( "Regie + Buch", "my-text-domain" ),

        "menu_name" => esc_html__( "Regie und Buch", "my-text-domain" ),

        "all_items" => esc_html__( "Alle Regisseure", "my-text-domain" ),

        "add_new" => esc_html__( "Neuer Regisseur", "my-text-domain" ),

    ];

  

    $args = [

        "label" => esc_html__( "regies", "my-text-domain" ),

        "labels" => $labels,

        "description" => "Regie und Buch",

        "public" => true,

        "publicly_queryable" => true,

        "show_ui" => true,

        "show_in_rest" => true,

        "rest_base" => "regie",

        "rest_controller_class" => "WP_REST_Posts_Controller",

        "rest_namespace" => "wp/v2",

        "has_archive" => true,

        "show_in_menu" => true,

        "show_in_nav_menus" => true,

        "delete_with_user" => false,

        "exclude_from_search" => false,

        "capability_type" => "post",

        "map_meta_cap" => true,

        "hierarchical" => false,

        "can_export" => false,

        "rewrite" => [ "slug" => "regisseur", "with_front" => true ],

        "query_var" => true,

        "supports" => [ "title" ],

        "taxonomies" => [ "category", "post_tag" ],

        "show_in_graphql" => false,

    ];

    register_post_type( "regie", $args );
}
add_action('init', 'register_my_post_type');

archive-regie.php is working as expected. single-regie.php isn't. (404)

What I'm wondering about as well, even if my custom single page is not working. Why is my themes/single.php not doing anything?

I've resaved the permalinks. Either via saving it "plain" and then setting the "real" permalinks or just saving it, done that at least 20 times now. Directly flushing the rewrite rules via code is also not working.

I've deleted and let Wordpress handle the creation of .htaccess.

Even changing the slug to not be the same as the registered custom post type name, didn't help.

I've multiple custom post types working on this exact site. I even copied these register_post_type snippets and changed it accordingly. To no avail.

Hope you can help me.

1 Answer 1

2

After some time banging my head against the wall, I found the solution. I used https://underscores.me/ as a starter for my theme. One function sets the main query, where one can add custom post types as well. So if anybody encounters the same problem, be aware of the hook "pre_get_posts".

function gp_add_cpt_post_names_to_main_query( $query ) {
    // Bail if this is not the main query.
    if ( ! $query->is_main_query() ) {
        return;
    }

    // Bail if this query doesn't match our very specific rewrite rule.
    if ( ! isset( $query->query['page'] ) ) {
        return;
    }

    // Bail if we're not querying based on the post name.
    if ( empty( $query->query['name'] ) ) {
        return;
    }
    // Add CPT to the list of post types WP will include when it queries based on the post name.
    $query->set( 'post_type', array( 'post', 'page', 'regie' ) );
}
add_action( 'pre_get_posts', 'gp_add_cpt_post_names_to_main_query' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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