Or, embed this snippet using GenerateWP WordPress Plugin.

Download

Clone

test wordpress dropdown custom post

class Custompostlist_Meta_Box {

	public function __construct() {

		if ( is_admin() ) {
			add_action( 'load-post.php',     array( $this, 'init_metabox' ) );
			add_action( 'load-post-new.php', array( $this, 'init_metabox' ) );
		}

	}

	public function init_metabox() {

		add_action( 'add_meta_boxes',        array( $this, 'add_metabox' )         );
		add_action( 'save_post',             array( $this, 'save_metabox' ), 10, 2 );

	}

	public function add_metabox() {

		add_meta_box(
			'Custompostlist001',
			__( 'Keuze bericjten lijst', 'text_domain' ),
			array( $this, '_Custompostlistrender_metabox' ),
			'page',
			'side',
			'default'
		);

	}

	public function _Custompostlistrender_metabox( $post ) {

		// Retrieve an existing value from the database.
		$hb_custompostlist001 = get_post_meta( $post->ID, 'hb_custompostlist001', true );

		// Set default values.
		if( empty( $hb_custompostlist001 ) ) $hb_custompostlist001 = '';

		// Form fields.
		echo '<table class="form-table">';

		echo '	<tr>';
		echo '		<th><label for="hb_custompostlist001" class="hb_custompostlist001_label">' . __( 'Keuze berichten', 'text_domain' ) . '</label></th>';
		echo '		<td>';
		wp_dropdown_pages( array( 'id' => 'hb_custompostlist001', 'name' => 'hb_custompostlist001', 'class' => 'hb_custompostlist001_field', 'selected' => $hb_custompostlist001 ) );
		echo '		</td>';
		echo '	</tr>';

		echo '</table>';

	}

	public function save_metabox( $post_id, $post ) {

		// Sanitize user input.
		$hb_new_custompostlist001 = isset( $_POST[ 'hb_custompostlist001' ] ) ? sanitize_text_field( $_POST[ 'hb_custompostlist001' ] ) : '';

		// Update the meta field in the database.
		update_post_meta( $post_id, 'hb_custompostlist001', $hb_new_custompostlist001 );

	}

}

new Custompostlist_Meta_Box;