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 https://carsonified.com/blog/dev/create-your-first-wordpress-custom-post-type/ as a base.
Thanks
- The topic ‘add_meta_box based on custom posts’ is closed to new replies.