Title: add_meta_box based on custom posts
Last modified: August 19, 2016

---

# add_meta_box based on custom posts

 *  Resolved [coopersita](https://wordpress.org/support/users/coopersita/)
 * (@coopersita)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/add_meta_box-based-on-custom-posts/)
 * Hi all,
 * I have 2 custom posts: “staff” and “projects”. I would like to have a custom 
   data field that lists all available staff with checkboxes next to the name, so
   a one or more staff members could be associated to a project.
 * I’m close, but the loop that lists my staff members is messing things, and I 
   don’t know how to reset the loop back. I’ve tried a bunch of things, but none
   seem to do it.
 * With the code I have, the projects page starts to list staff (only one staff),
   and my project disappears… the project pages list staff. If I delete my code.
   Everything comes back to normal.
 * Here’s my code in functions.php:
 *     ```
       add_action("admin_init", "admin_init");
       function admin_init(){
       	add_meta_box("credits_meta", "Design & Build Credits", "credits_meta", "projects", "side", "low");
       }
   
       function credits_meta() {
         global $post;
         $custom = get_post_custom($post->ID);
         $designers = $custom["designers"];
   
         ?>
         <p><label>Designed By:</label><br />
   
         <?php 
   
         $my_query = new WP_Query('post_type=staff');
         if ($my_query->have_posts()) {
       	while ($my_query->have_posts()) : $my_query->the_post(); $do_not_duplicate = $post->ID;
       	$checked = '';
       	if(count($designers) > 0) {
       		foreach ($designers as $d)
       			if ($d == the_title('', '', false)) $checked ='checked = checked';
       	}
       	?>
               <input type="checkbox" name="designers[]" value="<?php the_title(); ?>" <?php echo $checked; ?>><?php the_title(); ?><br />
   
       	<?php endwhile; } 
   
       	?>
   
         <?php
         wp_reset_query();
       }
   
       add_action('save_post', 'save_details');
       function save_details(){
         global $post;
   
         if ($_POST["designers"]) {
         	foreach ($_POST["designers"] as $x) add_post_meta($post->ID, "designers", $x, false);
         }
       }
       ```
   
 * To create this code, I used [http://carsonified.com/blog/dev/create-your-first-wordpress-custom-post-type/](http://carsonified.com/blog/dev/create-your-first-wordpress-custom-post-type/)
   as a base.
 * Thanks

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Thread Starter [coopersita](https://wordpress.org/support/users/coopersita/)
 * (@coopersita)
 * [15 years, 10 months ago](https://wordpress.org/support/topic/add_meta_box-based-on-custom-posts/#post-1581056)
 * Figured it out.
 * I used a custom query, instead of the loop:
 *     ```
       add_action("admin_init", "admin_init");
   
       function admin_init(){
   
         add_meta_box("credits_meta", "Credits", "credits_meta", "projects", "side", "low");
       } 
   
       function credits_meta() {
         global $post;
         global $wpdb;
         $custom = get_post_custom($post->ID);
         $designers = $custom["designers"];
   
         ?>
   
         <?php
          $querystr = "
           SELECT wposts.*
           FROM $wpdb->posts wposts
           WHERE wposts.post_type = 'staff'
           AND wposts.post_title != 'Auto Draft'
           ORDER BY wposts.post_name DESC
        ";
   
        $my_query = $wpdb->get_results($querystr, OBJECT);
   
        if( $my_query ) {
   
         foreach( $my_query as $s ) {
   
       	$checked = '';
       	if(count($designers) > 0) {
       		foreach ($designers as $d)
       			if ($d == $s->ID) $checked ='checked = checked';
       	}
       	?>
   
           <p><input type="checkbox" name="designers[]" value="<?php echo $s->ID ?>" <?php echo $checked; ?>> <?php echo $s->post_title; ?></p>
   
       	<?php
         }
   
        }
   
       }
   
       add_action('save_post', 'save_details');
       function save_details(){
         global $post;
   
         delete_post_meta($post->ID, 'designers');
         if ($_POST["designers"]) {
         	foreach ($_POST["designers"] as $x) add_post_meta($post->ID, "designers", $x, false);
         }
   
       }
       ```
   
 *  [ldexterldesign](https://wordpress.org/support/users/ldexterldesign/)
 * (@ldexterldesign)
 * [15 years ago](https://wordpress.org/support/topic/add_meta_box-based-on-custom-posts/#post-1581363)
 * Did you work out how to output your custom data into a page template?

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘add_meta_box based on custom posts’ is closed to new replies.

## Tags

 * [add_meta_box](https://wordpress.org/support/topic-tag/add_meta_box/)
 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)
 * [reset](https://wordpress.org/support/topic-tag/reset/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [ldexterldesign](https://wordpress.org/support/users/ldexterldesign/)
 * Last activity: [15 years ago](https://wordpress.org/support/topic/add_meta_box-based-on-custom-posts/#post-1581363)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
