Reputation: 1227
I've created a custom post type using the example shown here. In my case I'm creating a Career post type for job ads.
Everything seems fine. My Careers section appears in the dashboard and I can create job ads, which have /careers/job-title as their permalink. Exactly as I would like. But I can't view them. If I click on view or go to the url directly I get a 404.
This is a new local installation using xampp and bitnami wordpress. It's basically a default installation, I've added a few plugins previously but for debugging purposes I've disabled all of them.
Any ideas?
function create_posttype() {
$args = array(
'labels' => array(
'name' => __('Careers'),
'singular_name' => __('Careers'),
'all_items' => __('All Job Postings'),
'add_new_item' => __('Add New Job Posting'),
'edit_item' => __('Edit Job Posting'),
'view_item' => __('View Job Posting')
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'careers'),
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'capability_type' => 'post',
'supports' => array('title', 'editor', 'thumbnail'),
'exclude_from_search' => false,
'menu_position' => 6,
'has_archive' => true,
'menu_icon' => 'dashicons-format-status'
);
register_post_type('careers', $args);
}
add_action( 'init', 'create_posttype');
function my_add_meta_box(){
add_meta_box( 'job-details', 'Job Details', 'my_meta_box_cb', 'Careers', 'normal', 'default');
}
function my_meta_box_cb($post){
$values = get_post_custom( $post->ID );
$salary = isset( $values['salary'] ) ? esc_attr( $values['salary'][0] ) : "";
$salary_unit = isset( $values['salary_unit'] ) ? esc_attr( $values['salary_unit'][0] ) : "hr";
wp_nonce_field( 'job_details_nonce_action', 'job_details_nonce' );
$html = '';
$html .= '<label>Salary</label>';
$html .= '<input type="number" name="salary" id="salary" style="margin-top:15px; margin-left:9px; margin-bottom:10px;" value="'. $salary .'" />';
$html .= '<label> / </label>';
$html .= '<input type="text" list=salary_units name="salary_unit" id="salary_unit" style="margin-left:9px; margin-top:15px;" value="'. $salary_unit .'" />';
$html .= '<datalist id=salary_units><option>hr<option>day<option>month<option>annum</datalist>';
echo $html;
}
function my_save_meta_box($post_id){
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our nonce isn't there, or we can't verify it, bail
if( !isset( $_POST['job_details_nonce'] ) || !wp_verify_nonce( $_POST['job_details_nonce'], 'job_details_nonce_action' ) ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
if(isset( $_POST['salary'] ) )
update_post_meta( $post_id, 'salary', $_POST['salary']);
if(isset( $_POST['salary_unit'] ) )
update_post_meta( $post_id, 'salary_unit', $_POST['salary_unit']);
}
add_action( 'add_meta_boxes', 'my_add_meta_box' );
add_action( 'save_post', 'my_save_meta_box' );
Upvotes: 0
Views: 70
Reputation: 19308
When you register a new post type, you need to flush your rewrite rules.
To do that go to Settings -> Permalinks and press save.
If you're building something you plan to distribute you'll want to flush rewrite rules automatically. For further information see: https://codex.wordpress.org/Function_Reference/register_post_type#Flushing_Rewrite_on_Activation
Upvotes: 1