Here’s the updated meta box callback function. I’ve used get_posts()
instead of WP_Query
as get_posts()
is more performant as well as easy to use in this case. And removed get_post_custom()
as it’ll fetch all the custom metadata when you don’t need them. So, I’ve replaced that with get_post_meta()
. Hope your dropdown will work as expected now.
I’d suggest you save post id (album id) instead of the title.
function meta_box_album_callback( $post ) {
wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
$mytheme_custom_select = get_post_meta( $post->ID, 'mytheme_custom_select', true );
$albums = get_posts( array(
'post_type' => 'albums',
'post_status' => 'publish',
'posts_per_page' => -1
) );
?>
<p>
<label for="mytheme_custom_select">Select album:</label><br>
<select id="mytheme_custom_select" name="mytheme_custom_select">
<option value="">Selecht an album...</option>
<?php foreach ( $albums as $album ) : ?>
<option value="<?php echo esc_attr( $album->post_title ); ?>" <?php selected( $mytheme_custom_select, esc_attr( $album->post_title ) ); ?>><?php echo esc_html( $album->post_title ); ?></option>
<?php endforeach; ?>
</select>
</p>
<?php
}