1

I'm using the CPT UI plugin to create a custom post type with a custom taxonomy. I'm finding however that the individual post page shows "No Results Found".

I'm using this page slug as the CPT custom rewrite slug: wbc-activity-page

The terms page is working correctly on this url: domain.com/wbc-activity-page/home/

The issue is that the post page is showing 'no results found' on the post's url: domain.com/wbc-activity-page/wbc-post-1/

This is my post type settings:

    function cptui_register_my_cpts_wbc_activity() {

    /**
     * Post Type: WBC Activities.
     */

    $labels = [
        "name" => __( "WBC Activities", "mw-theme" ),
        "singular_name" => __( "WBC Activity", "mw-theme" ),
    ];

    $args = [
        "label" => __( "WBC Activities", "mw-theme" ),
        "labels" => $labels,
        "description" => "",
        "public" => true,
        "publicly_queryable" => true,
        "show_ui" => true,
        "show_in_rest" => true,
        "rest_base" => "",
        "rest_controller_class" => "WP_REST_Posts_Controller",
        "has_archive" => false,
        "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,
        "rewrite" => [ "slug" => "wbc-activity-page", "with_front" => true ],
        "query_var" => true,
        "supports" => [ "title", "editor", "thumbnail", "custom-fields", "comments", "revisions" ],
        "taxonomies" => [ "wbc_category" ],
    ];

    register_post_type( "wbc_activity", $args );
}

add_action( 'init', 'cptui_register_my_cpts_wbc_activity' );

This is my custom taxonomy settings:

    function cptui_register_my_taxes_wbc_category() {

    /**
     * Taxonomy: WBC Activity Categories.
     */

    $labels = [
        "name" => __( "WBC Activity Categories", "mw-theme" ),
        "singular_name" => __( "WBC Activity Category", "mw-theme" ),
    ];

    $args = [
        "label" => __( "WBC Activity Categories", "mw-theme" ),
        "labels" => $labels,
        "public" => true,
        "publicly_queryable" => true,
        "hierarchical" => true,
        "show_ui" => true,
        "show_in_menu" => true,
        "show_in_nav_menus" => true,
        "query_var" => true,
        "rewrite" => [ 'slug' => 'wbc-activity-page', 'with_front' => true, ],
        "show_admin_column" => false,
        "show_in_rest" => true,
        "rest_base" => "wbc_category",
        "rest_controller_class" => "WP_REST_Terms_Controller",
        "show_in_quick_edit" => true,
        ];
    register_taxonomy( "wbc_category", [ "wbc_activity" ], $args );
}
add_action( 'init', 'cptui_register_my_taxes_wbc_category' );

Is there something I'm missing here?

  • Taxonomies and post types can't have the same rewrite slug. When you go to wbc-activity-page/wbc-post-1/, WordPress thinks wbc-post-1 is a taxonomy term, which is why it can't find any posts. They'll need to be different. – Jacob Peattie May 27 at 11:41
  • 1
    my understanding of the code above is that both the terms, and the posts share exactly the same URL structure, which is a big problem, e.g. if I visit domain.com/wbc-activity-page/home/ is that for a CPT with the slug home or a term with the slug home? You can't have clashes – Tom J Nowell May 27 at 11:42
  • @JacobPeattie, thanks for this. If these have to be different, do you know how you would have the post and taxonomy term nested under the custom post type slug? Whenever I try to achieve this either the term or the single-post page shows no results found? (e.g. domain.com/custom-post-slug/taxomomy-term/post-title) – FreddieE May 27 at 13:03
-1

The code you have used is correct. No issue in that. But generally if you register any CPT you should have flush the rewrite rule once.

Solution:

1) Go to backend admin. Settings >> Permalink page. Just go to that page, Not to change anything. The frontend will work. It is because if you go that page it will flush the rewrite rule automatically.

2) In first option, You might get this issue again due to some other plugin CPT register and flush the rule internally. So to avoid this situation use following code:

If you are using theme then add the hook:

add_action( 'after_switch_theme', 'flush_rewrite_rules' );

If you are using plugin then on activation hook you need to use the code, In main plugin file:

function myplugin_activate() {
    // register taxonomies/post types here
    flush_rewrite_rules();
}

register_activation_hook( __FILE__, 'myplugin_activate' );
| improve this answer | |

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.