This is my code to register the post type:
// Register new post type, User Conversations: b2bking_conversation
add_action( 'init', array($this, 'b2bking_register_post_type_conversation'), 0 );
function b2bking_register_post_type_conversation() {
// Build labels and arguments
$labels = array(
'name' => __( 'Conversations', 'b2bking' ),
'singular_name' => __( 'Conversation', 'b2bking' ),
'all_items' => __( 'Conversations', 'b2bking' ),
'menu_name' => _x( 'Conversations', 'Admin menu name', 'b2bking' ),
'add_new' => __( 'Start Conversation', 'b2bking' ),
'add_new_item' => __( 'Start new conversation', 'b2bking' ),
'edit' => __( 'Edit', 'b2bking' ),
'edit_item' => __( 'Edit conversation', 'b2bking' ),
'new_item' => __( 'New conversation', 'b2bking' ),
'view_item' => __( 'View conversation', 'b2bking' ),
'view_items' => __( 'View conversations', 'b2bking' ),
'search_items' => __( 'Search conversations', 'b2bking' ),
'not_found' => __( 'No conversations found', 'b2bking' ),
'not_found_in_trash' => __( 'No conversations found in trash', 'b2bking' ),
'parent' => __( 'Parent conversation', 'b2bking' ),
'featured_image' => __( 'Conversation image', 'b2bking' ),
'set_featured_image' => __( 'Set conversation image', 'b2bking' ),
'remove_featured_image' => __( 'Remove conversation image', 'b2bking' ),
'use_featured_image' => __( 'Use as conversation image', 'b2bking' ),
'insert_into_item' => __( 'Insert into conversation', 'b2bking' ),
'uploaded_to_this_item' => __( 'Uploaded to this conversation', 'b2bking' ),
'filter_items_list' => __( 'Filter conversations', 'b2bking' ),
'items_list_navigation' => __( 'Conversations navigation', 'b2bking' ),
'items_list' => __( 'Conversations list', 'b2bking' )
);
$args = array(
'label' => __( 'Conversation', 'b2bking' ),
'description' => __( 'This is where you can create new conversations', 'b2bking' ),
'labels' => $labels,
'supports' => array('title','editor','author', 'comments','thumbnail'),
'hierarchical' => false,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => 'b2bking',
'menu_position' => 100,
'show_in_admin_bar' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'capability_type' => 'post',
'rewrite' => array("slug" => "conversations"),
);
// Actually register the post type
register_post_type( 'b2bking_conversation', $args );
}
No matter what I do, I can't actually view these posts. The link either goes to 404 or some page where only the default Hello World post can be seen, or redirects to the front page.
I ALREADY saved permalinks, tried flush_rewrite_rules(), tried multiple domains...
This is my code to register the post type:
// Register new post type, User Conversations: b2bking_conversation
add_action( 'init', array($this, 'b2bking_register_post_type_conversation'), 0 );
function b2bking_register_post_type_conversation() {
// Build labels and arguments
$labels = array(
'name' => __( 'Conversations', 'b2bking' ),
'singular_name' => __( 'Conversation', 'b2bking' ),
'all_items' => __( 'Conversations', 'b2bking' ),
'menu_name' => _x( 'Conversations', 'Admin menu name', 'b2bking' ),
'add_new' => __( 'Start Conversation', 'b2bking' ),
'add_new_item' => __( 'Start new conversation', 'b2bking' ),
'edit' => __( 'Edit', 'b2bking' ),
'edit_item' => __( 'Edit conversation', 'b2bking' ),
'new_item' => __( 'New conversation', 'b2bking' ),
'view_item' => __( 'View conversation', 'b2bking' ),
'view_items' => __( 'View conversations', 'b2bking' ),
'search_items' => __( 'Search conversations', 'b2bking' ),
'not_found' => __( 'No conversations found', 'b2bking' ),
'not_found_in_trash' => __( 'No conversations found in trash', 'b2bking' ),
'parent' => __( 'Parent conversation', 'b2bking' ),
'featured_image' => __( 'Conversation image', 'b2bking' ),
'set_featured_image' => __( 'Set conversation image', 'b2bking' ),
'remove_featured_image' => __( 'Remove conversation image', 'b2bking' ),
'use_featured_image' => __( 'Use as conversation image', 'b2bking' ),
'insert_into_item' => __( 'Insert into conversation', 'b2bking' ),
'uploaded_to_this_item' => __( 'Uploaded to this conversation', 'b2bking' ),
'filter_items_list' => __( 'Filter conversations', 'b2bking' ),
'items_list_navigation' => __( 'Conversations navigation', 'b2bking' ),
'items_list' => __( 'Conversations list', 'b2bking' )
);
$args = array(
'label' => __( 'Conversation', 'b2bking' ),
'description' => __( 'This is where you can create new conversations', 'b2bking' ),
'labels' => $labels,
'supports' => array('title','editor','author', 'comments','thumbnail'),
'hierarchical' => false,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => 'b2bking',
'menu_position' => 100,
'show_in_admin_bar' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'capability_type' => 'post',
'rewrite' => array("slug" => "conversations"),
);
// Actually register the post type
register_post_type( 'b2bking_conversation', $args );
}
No matter what I do, I can't actually view these posts. The link either goes to 404 or some page where only the default Hello World post can be seen, or redirects to the front page.
I ALREADY saved permalinks, tried flush_rewrite_rules(), tried multiple domains...
Share Improve this question asked Mar 29, 2020 at 14:23 Stefan SStefan S 192 bronze badges 5- A couple of days ago, I need to do something with permalinks and found out the reason why it keep redirect to 404 or frontpage in some occasions. Did you resolve this problem? If not, I may share some insights by updating the answer. – 西門 正 Code Guy - JingCodeGuy Commented Apr 8, 2020 at 1:40
- Hi Simon, I ended up avoiding the issue by doing a custom posts display page where I just query the posts. Still not sure what was causing it. Don't need it anymore but an answer may be valuable to anyone searching in the future, and I'm curious too – Stefan S Commented Apr 8, 2020 at 6:40
- Sure, could you tell me some more information? if you turn off the permalinks, what is the link to a post that you could open? And if turn on, what is the link it return 404? Give me example links so that I could have a better picture of the situation. The example should be the same post. – 西門 正 Code Guy - JingCodeGuy Commented Apr 11, 2020 at 0:31
- You have mentioned about "in some occasions." It would be great if you could give those occasions information too. This could help narrow down the causes. – 西門 正 Code Guy - JingCodeGuy Commented Apr 11, 2020 at 1:04
- Because there is another question asking about rewrite. I have explained why it is 404 over that post with tracing technique, If you could provide more information, I might be possible to write the example here.. – 西門 正 Code Guy - JingCodeGuy Commented Apr 13, 2020 at 4:14
1 Answer
Reset to default 0I think it is actually registered. I did copy and paste your code to have a try on my 5.3.2 installation.
You can try comment out the following to try.
// 'show_in_menu' => 'b2bking',
And then you will discover that it is on your left side menu, probably at the bottom.
Because you set position as 100.
show_in_menu menu
means, it will appear under a specific submenu with slug called "b2bking".
But if that menu slug does not exist, then it will not show. However, it is registered.
You could also test if it is registered by going to the link.
eg. "http://your-site/wp-admin/edit.php?post_type=b2bking_conversation"
If it is registered, you could see the post list screen and add new post for this post type.
Additional notes: There is one more thing I have changed. Because I test it in functions.php so I just change the add_action to
add_action( 'init', 'your_function' );
In this way, could reduce other problem like your plugin is activated or not. Or the function is called or not.