Edit

Wordpress Post referencing setup Assign multiple disciplines to a project

<?php


// Adds custom post type for Projects and Services

function create_post_type(){

  register_post_type('project',
    array(
      'labels' => array(
        'name' => __('Projects'),
        'singular_name' => __('Project')
      ),
      'taxonomies' => array('category'),
      'public' => true,
      'has_archive' => true,
      'capabilities' => array(
          'edit_post' => 'edit_project',
          'edit_posts' => 'edit_projects',
          'publish_posts' => 'publish_project',
          'read_post' => 'read_project',
      ),
      'supports' => array(
          'title','editor','thumbnail'
      ),
    )
  );

  register_post_type('service',
    array(
      'labels' => array(
        'name' => __('Services'),
        'singular_name' => __('Service')
      ),
      'public' => true,
      'has_archive' => true,
      'capabilities' => array(
          'edit_post' => 'edit_service',
          'edit_posts' => 'edit_services',
          'publish_posts' => 'publish_service',
          'read_post' => 'read_service',
      ),
      'supports' => array(
          'title','editor','thumbnail'
      ),
    )
  );

}

add_action('init','create_post_type');





function add_theme_caps(){

    $subscribers = get_role( 'subscriber' );

    //$subscribers->add_cap( 'edit_project' );
    //$subscribers->add_cap( 'edit_projects' );
    //$subscribers->add_cap( 'publish_project' );

    $subscribers->add_cap( 'read_project' );
    $subscribers->add_cap( 'read_service' );

    $admins = get_role( 'administrator' );

    $admins->add_cap( 'edit_project' );
    $admins->add_cap( 'edit_projects' );
    $admins->add_cap( 'publish_project' );
    $admins->add_cap( 'read_project' );

    $admins->add_cap( 'edit_service' );
    $admins->add_cap( 'edit_services' );
    $admins->add_cap( 'publish_service' );
    $admins->add_cap( 'read_service' );

}

add_action('init','add_theme_caps');





// Adds custom field to the PROJECT custom post type

function add_project_metas(){
    add_meta_box(
        'project_service-meta',
        'Service',
        'project_service',
        'project',
        'normal',
        'low'
    );
}
add_action('admin_init','add_project_metas');


function project_service(){
    global $post;
    $custom=get_post_custom($post->ID);
    $project_services=$custom['project_service'][0];
    $project_services=unserialize($project_services);
    $args=array(
        'post_type'=>'service',
        'posts_per_page'=>'200',
        'orderby'=>'title',
        'order'=>'ASC'
    );
    $loop=new WP_Query($args);
    while($loop->have_posts()):$loop->the_post();
        echo('<div><input type="checkbox" name="project_services[]" value="'.get_the_ID().'" id="project_service_'.get_the_ID().'"');
        if(is_array($project_services)&&in_array(get_the_ID(),$project_services)) echo(' checked="checked"');
        //checked($project_services,get_the_ID());
        echo('><label for="project_service_'.get_the_ID().'">'.get_the_title(get_the_ID()).'</label></div>');
    endwhile;
}



function save_details(){
    global $post;
    update_post_meta($post->ID,'project_service',$_POST["project_services"]);
}

add_action('save_post','save_details');




?>