• Resolved Quin

    (@quin452)


    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);
    • This topic was modified 8 years, 1 month ago by Quin.
    • This topic was modified 8 years, 1 month ago by Quin.
Viewing 1 replies (of 1 total)
  • Thread Starter Quin

    (@quin452)

    I’ve got it sorted.

    I needed to store $post somewhere as a backup.
    So for the show metabox function, it now looks like:

    		/*	Show roles metabox	*/
    		function show_roles_metabox() {
    			global $post;
    			$tempPost = $post;
    
    			wp_nonce_field(basename(__FILE__), 'role_nonce');
    
    			$pro_areas = array(
    				'One',
    				'Two',
    				'Three'
    			);
    
    		$args = array(
    			'post_type' => 'proarea',
    			'publish_status' => 'publish',
    			'posts_per_page' => -1,
    			'order' => 'ASC'
    		);
    		$the_query = new WP_Query($args);
    		if($the_query->have_posts()) {
    			echo '<ul>';
    			while ( $the_query->have_posts()) {
    				$the_query->the_post();
    				echo '<li>' . get_the_title() . '</li>';
    			}
    			echo '</ul>';
    		} else {
    		}
    		wp_reset_postdata();
    
    		/*
    		$mcpt_query = array();
    		$the_query = get_posts('post_type=proarea');
    		foreach ( $the_query as $post ) : setup_postdata( $post );
    		$mcpt_query[] = array(
    			'title'	=> get_the_title($post->ID)
    		);
    		endforeach;
    		wp_reset_query();
    		print_r($mcpt_query);
    		*/
    
    		$post = $tempPost;
    
    			echo '<span><label for="pro_area">Professional Area: </label></span>';
    			echo '<select name="pro_area">';
    
    			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>';
    		}

    I called global $post, make a backup in $tempPost, and then after the “reset”, I load $psot with $tempPost.

Viewing 1 replies (of 1 total)
  • The topic ‘WP_Query breaks my Custom Post Type’ is closed to new replies.