WP_Query breaks my Custom Post Type
-
Hi
I am trying to list the posts (title only) of a custom post type in a dropdown with a metabox of another custom post type.
I’ve managed to create the dropdown in the metabox and save the data.
I’ve also managed to populate the dropdown with the CPT post titles.
However, I am using WP_Query, and whenever I use that, it does strange things.Using wp_query stops me from updating/saving the dropdown selection, and also changes the permalink to the last option for the dropdown.
I’ve tried using wp_reset_postdata and wp_reset_query, but neither make a difference.
/* Create role metabox */ function create_roles_metabox() { add_meta_box( 'roles_metabox', 'Role Area & Level', 'show_roles_metabox', 'role', 'side', 'high' ); } function get_proareas() { // I will look at storing the titles here $pro_area_array = array(); // arguments not in use for testing $pro_area_args = array( 'post_type' => 'proarea', 'publish_status' => 'publish', 'posts_per_page' => -1, 'order' => 'ASC' ); $pro_area_query = new WP_Query(); $pro_area_query->query('post_type=proarea&posts_per_page=-1'); while ($pro_area_query->have_posts()) { $pro_area_query->the_post(); $post_id = get_the_ID(); echo $post_id; echo "<br>"; // the IDs can be seen - for testing } wp_reset_postdata(); // currently empty print_r($pro_area_array); } /* Show roles metabox */ function show_roles_metabox() { global $post; wp_nonce_field(basename(__FILE__), 'role_nonce'); // do_action('get_professional_areas'); // hard coded for when not using wp_query $pro_areas = array( 'One', 'Two', 'Three' ); echo '<span><label for="pro_area">Professional Area: </label></span>'; echo '<select name="pro_area">'; // this array will be switched out once wp_query works as I need it to foreach($pro_areas as $pro_area) { if($pro_area == get_post_meta($post->ID, 'pro_area', true)) echo '<option selected>'. $pro_area .'</option>'; else echo '<option>'. $pro_area .'</option>'; } echo '</select>'; } /* Save roles metabox */ function save_roles_metabox($post_id, $post) { if(!isset($_POST['role_nonce']) || !wp_verify_nonce($_POST['role_nonce'], basename(__FILE__))) return $post->ID; if(!current_user_can('edit_post', $post->ID)) return $post->ID; if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post->ID; if(isset($_POST['pro_area'])) update_post_meta($post_id, 'pro_area', $_POST['pro_area']); } add_action('get_professional_areas', 'get_proareas'); add_action('add_meta_boxes', 'create_roles_metabox'); add_action('save_post', 'save_roles_metabox', 1, 2);
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘WP_Query breaks my Custom Post Type’ is closed to new replies.