WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] add_meta_box based on custom posts (3 posts)

  1. coopersita
    Member
    Posted 3 years ago #

    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/ as a base.

    Thanks

  2. coopersita
    Member
    Posted 3 years ago #

    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);
      }
    
    }
  3. ldexterldesign
    Member
    Posted 3 years ago #

    Did you work out how to output your custom data into a page template?

Topic Closed

This topic has been closed to new replies.

About this Topic