How to fix 404 on post after prepending Custom Post Type url with Custom Taxonomy Term slug

I'm setting up "special" url structure for posts etcResult i want to get is: sitenameclubjustcavaliclu

I'm setting up "special" url structure for posts etc

Result i want to get is: sitename/club/justcavali club is term from taxonomy (club_type) and justcavali is post type (booking_type)

after i setup rewrite for cpt to go for %club_type%, all of my posts/pages go to 404 and im getting some nonsense query on those pages post_type = "attachment" for example,

i've tried every single way to fix this, but someone either taxonomy/cpt breaks or post/page breaks

It breaks when i insert "%club_type%" to rewrite for cpt, and yes i've disabled "with front", also i am deleting taxonomy base from terms

club_type/club - > /club

function cptui_register_my_cpts_listing() {

/**
 * Post Type: Listing.
 */

$labels = array(
    "name" => __( "Listing", "sage" ),
);

$args = array(
    "label" => __( "Listing", "sage" ),
    "labels" => $labels,
    "description" => "New Listinga",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "delete_with_user" => false,
    "show_in_rest" => true,
    "rest_base" => "",
    "rest_controller_class" => "WP_REST_Posts_Controller",
    "has_archive" => true,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "exclude_from_search" => false,
    "capability_type" => "post",
    "map_meta_cap" => true,
    "hierarchical" => true,
    "rewrite" => array( "slug" => "/%club_type%/", "with_front" => false ),
    "query_var" => true,
    "supports" => array( "title", "editor" ),
    "taxonomies" => array( "club_type", "city_part" ),
);

register_post_type( "listing", $args );
    }

    add_action( 'init', 'cptui_register_my_cpts_listing' );

function cptui_register_my_taxes_club_type() {

/**
 * Taxonomy: Club Type
 */

$labels = array(
    "name" => __( "club_type", "sage" ),
    "singular_name" => __( "club_type", "sage" ),
);

$args = array(
    "label" => __( "club_type", "bgn" ),
    "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" => array( 'slug' => 'club_type', 'with_front' => false, ),
    "show_admin_column" => true,
    "show_in_rest" => true,
    "rest_base" => "club_type",
    "rest_controller_class" => "WP_REST_Terms_Controller",
    "show_in_quick_edit" => true,
    );
register_taxonomy( "club_type", array( "listing" ), $args );
}
add_action( 'init', 'cptui_register_my_taxes_club_type' );

I'm setting up "special" url structure for posts etc

Result i want to get is: sitename/club/justcavali club is term from taxonomy (club_type) and justcavali is post type (booking_type)

after i setup rewrite for cpt to go for %club_type%, all of my posts/pages go to 404 and im getting some nonsense query on those pages post_type = "attachment" for example,

i've tried every single way to fix this, but someone either taxonomy/cpt breaks or post/page breaks

It breaks when i insert "%club_type%" to rewrite for cpt, and yes i've disabled "with front", also i am deleting taxonomy base from terms

club_type/club - > /club

function cptui_register_my_cpts_listing() {

/**
 * Post Type: Listing.
 */

$labels = array(
    "name" => __( "Listing", "sage" ),
);

$args = array(
    "label" => __( "Listing", "sage" ),
    "labels" => $labels,
    "description" => "New Listinga",
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "delete_with_user" => false,
    "show_in_rest" => true,
    "rest_base" => "",
    "rest_controller_class" => "WP_REST_Posts_Controller",
    "has_archive" => true,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "exclude_from_search" => false,
    "capability_type" => "post",
    "map_meta_cap" => true,
    "hierarchical" => true,
    "rewrite" => array( "slug" => "/%club_type%/", "with_front" => false ),
    "query_var" => true,
    "supports" => array( "title", "editor" ),
    "taxonomies" => array( "club_type", "city_part" ),
);

register_post_type( "listing", $args );
    }

    add_action( 'init', 'cptui_register_my_cpts_listing' );

function cptui_register_my_taxes_club_type() {

/**
 * Taxonomy: Club Type
 */

$labels = array(
    "name" => __( "club_type", "sage" ),
    "singular_name" => __( "club_type", "sage" ),
);

$args = array(
    "label" => __( "club_type", "bgn" ),
    "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" => array( 'slug' => 'club_type', 'with_front' => false, ),
    "show_admin_column" => true,
    "show_in_rest" => true,
    "rest_base" => "club_type",
    "rest_controller_class" => "WP_REST_Terms_Controller",
    "show_in_quick_edit" => true,
    );
register_taxonomy( "club_type", array( "listing" ), $args );
}
add_action( 'init', 'cptui_register_my_taxes_club_type' );
Share Improve this question asked Apr 22, 2019 at 21:43 DzodzosDzodzos 1 1
  • Check this question. – nmr Commented Apr 22, 2019 at 22:13
Add a comment  | 

1 Answer 1

Reset to default 0

First, you don't need to have two actions called during init, they can both be done under the same action function -- so move the register CPT and taxonomy to a single function (or call separate functions from the main one) -- no need to have two init actions.

You should also be registering the taxonomy before the CPT (if you don't want to change your code just call taxonomy action at lower priority than CPT register), otherwise put your taxonomy registration code above the CPT registration code.

"rewrite" => array( "slug" => "/%club_type%/", "with_front" => false )

Can also just be this without the slashes:

"rewrite" => array( "slug" => "%club_type%", "with_front" => false ),

Although I recommend that you add some kind of identifier in your slug, like listing/%club_type% otherwise WordPress will try to use that as a query for specific page.

Check these as well:

Permalinks: custom post type -> custom taxonomy -> post

Mixing custom post type and taxonomy rewrite structures?

You could also look at different plugins available that could do this: https://wordpress/plugins/custom-post-type-permalinks/

I suspect your issue is probably because you're not using any kind of identifier before your taxonomy, and as such it's attempting to find it as a post name -- not as a taxonomy slug

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745564758a4633325.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信