Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The initial set-up requires initialising two post types, questions and answers,
- // also managing capabilities for these.
- function register_question_post_types() {
- register_post_type(
- 'question',
- array(
- 'label' => 'Questions',
- 'labels' => array(
- 'name' => 'Questions',
- 'singular_name' => 'Question',
- 'edit_item' => 'Edit Question',
- 'view_item' => 'View Question'
- ),
- 'exclude_from_search' => false,
- 'publicly_queryable' => true,
- 'show_ui' => true,
- 'capability_type' => 'question',
- 'capabilites' => array(
- 'publish_posts' => 'publish_questions',
- 'edit_posts' => 'edit_questions',
- 'edit_others_posts' => 'edit_others_questions',
- 'delete_posts' => 'delete_questions',
- 'delete_others_posts' => 'delete_others_questions',
- 'read_private_posts' => 'read_private_questions',
- 'edit_post' => 'edit_question',
- 'delete_post' => 'delete_question',
- 'read_post' => 'read_question',
- ),
- 'supports' => array( 'title', 'editor', 'author', 'excerpt', 'comments', 'revisions' ),
- 'has_archive' => true
- )
- );
- register_post_type(
- 'answer',
- array(
- 'label' => 'Answers',
- 'labels' => array(
- 'name' => 'Answers',
- 'singular_name' => 'Answer',
- 'edit_item' => 'Edit Answer',
- 'view_item' => 'View Question'
- ),
- 'exclude_from_search' => false,
- 'publicly_queryable' => false,
- 'show_ui' => true,
- 'show_in_menu' => 'edit.php?post_type=question', // Display within the "Questions" menu
- 'capability_type' => 'answer',
- 'capabilites' => array(
- 'publish_posts' => 'publish_answers',
- 'edit_posts' => 'edit_answers',
- 'edit_others_posts' => 'edit_others_answers',
- 'delete_posts' => 'delete_answers',
- 'delete_others_posts' => 'delete_others_answers',
- 'read_private_posts' => 'read_private_answers',
- 'edit_post' => 'edit_answer',
- 'delete_post' => 'delete_answer',
- 'read_post' => 'read_answer',
- ),
- 'supports' => array( 'title', 'editor', 'author', 'comments', 'revisions' )
- )
- );
- add_filter( 'map_meta_cap', 'questions_map_meta_cap', 10, 4 );
- add_filter( 'map_meta_cap', 'answers_map_meta_cap', 10, 4 );
- // Mapping the meta capabilities so that users can edit their own questions/answers, publish
- // them, delete them, etc. Borrowed from Justin Tadlock:
- // [ http://justintadlock.com/archives/2010/07/10/meta-capabilities-for-custom-post-types ]
- function questions_map_meta_cap( $caps, $cap, $user_id, $args ) {
- /* If editing, deleting, or reading a question, get the post and post type object. */
- if ( 'edit_question' == $cap || 'delete_question' == $cap || 'read_question' == $cap ) {
- $post = get_post( $args[0] );
- $post_type = get_post_type_object( $post->post_type );
- /* Set an empty array for the caps. */
- $caps = array();
- }
- /* If editing a question, assign the required capability. */
- if ( 'edit_question' == $cap ) {
- if ( $user_id == $post->post_author )
- $caps[] = $post_type->cap->edit_posts;
- else
- $caps[] = $post_type->cap->edit_others_posts;
- }
- /* If deleting a question, assign the required capability. */
- elseif ( 'delete_question' == $cap ) {
- if ( $user_id == $post->post_author )
- $caps[] = $post_type->cap->delete_posts;
- else
- $caps[] = $post_type->cap->delete_others_posts;
- }
- /* If reading a private question, assign the required capability. */
- elseif ( 'read_question' == $cap ) {
- if ( 'private' != $post->post_status )
- $caps[] = 'read';
- elseif ( $user_id == $post->post_author )
- $caps[] = 'read';
- else
- $caps[] = $post_type->cap->read_private_posts;
- }
- /* Return the capabilities required by the user. */
- return $caps;
- }
- // Do it all again for answers
- function answers_map_meta_cap( $caps, $cap, $user_id, $args ) {
- /* If editing, deleting, or reading a answer, get the post and post type object. */
- if ( 'edit_answer' == $cap || 'delete_answer' == $cap || 'read_answer' == $cap ) {
- $post = get_post( $args[0] );
- $post_type = get_post_type_object( $post->post_type );
- /* Set an empty array for the caps. */
- $caps = array();
- }
- /* If editing a answer, assign the required capability. */
- if ( 'edit_answer' == $cap ) {
- if ( $user_id == $post->post_author )
- $caps[] = $post_type->cap->edit_posts;
- else
- $caps[] = $post_type->cap->edit_others_posts;
- }
- /* If deleting a answer, assign the required capability. */
- elseif ( 'delete_answer' == $cap ) {
- if ( $user_id == $post->post_author )
- $caps[] = $post_type->cap->delete_posts;
- else
- $caps[] = $post_type->cap->delete_others_posts;
- }
- /* If reading a private answer, assign the required capability. */
- elseif ( 'read_answer' == $cap ) {
- if ( 'private' != $post->post_status )
- $caps[] = 'read';
- elseif ( $user_id == $post->post_author )
- $caps[] = 'read';
- else
- $caps[] = $post_type->cap->read_private_posts;
- }
- /* Return the capabilities required by the user. */
- return $caps;
- }
- }
- // Run all that at init
- add_action( 'init', 'register_question_post_types' );
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement