Display meta box dropdown (from custom post type) in the page post type

I have been struggling with this issue for (already) hours. I am trying to add a meta box to the “page” post type, in which I succeeded.. In my meta box I would like to display a dropdown with titles from an other custom post type (named “Albums”). I also succeeded in that, but once I select a specific album in the dropdown and save the page, it changes the page permalink to the permalink of the chosen album.

Thanks a lot in advance!
Below you can see my code.

add_action(‘add_meta_boxes’, ‘add_meta_box_album’);
function add_meta_box_album() {
add_meta_box(‘meta-box-album-id’, ‘Album’, ‘meta_box_album_callback’, ‘page’, ‘normal’, ‘high’);

function meta_box_album_callback($post) {
$values = get_post_custom($post->ID);
$mytheme_custom_select = (isset($values[‘mytheme_custom_select’][0]) && ” !== $values[‘mytheme_custom_select’][0]) ? $values[‘mytheme_custom_select’][0] : ”;
wp_nonce_field(‘my_meta_box_nonce’, ‘meta_box_nonce’);
<label for=””>Select album:</label><br>

<? $getAlbums = new WP_Query(array(
‘post_type’ => ‘albums’,
<select id=”mytheme_custom_select” name=”mytheme_custom_select”>
<option value=””>Selecht an album…</option>
<? while ($getAlbums->have_posts()):$getAlbums->the_post(); ?>
value=”<?php the_title($getAlbums->ID); ?>” <?php selected($mytheme_custom_select, the_title($getAlbums->ID), true); ?>><?php the_title($getAlbums->ID); ?></option>
<? endwhile; ?>
<? wp_reset_query(); ?>

<? }

add_action(‘save_post’, ‘cd_meta_box_save’);
function cd_meta_box_save($post_id) {
if (defined(‘DOING_AUTOSAVE’) && DOING_AUTOSAVE) return;
if (!isset($_POST[‘meta_box_nonce’]) || !wp_verify_nonce($_POST[‘meta_box_nonce’], ‘my_meta_box_nonce’)) return;
if (!current_user_can(‘edit_post’, $post_id)) return;
$allowed = array(
‘a’ => array(
‘href’ => array()
if (isset($_POST[‘mytheme_custom_select’])) { // Input var okay.
update_post_meta($post_id, ‘mytheme_custom_select’, sanitize_text_field(wp_unslash($_POST[‘mytheme_custom_select’]))); // Input var okay.

Read more here:: Display meta box dropdown (from custom post type) in the page post type

Leave a Reply

Your email address will not be published. Required fields are marked *