I would like to know if its possible to do something like this. I have Slider custom post type and Services custom post type.

For slider posts I have created a metabox for URL value to display next to all slider posts. What I would like to do now, is to get all posts from Services custom post type, and display them in this slider post metabox as select input, so I can choose which post should the link go to.

Thanks in advance :)

Here is the current code:

function rm_display_slider_metabox($post) {

    wp_nonce_field(basename(__FILE__), 'slider_nonce');

    $slide_url_value = get_post_meta($post->ID, '_slide_url', true);
    $slide_url = isset($slide_url_value) ? esc_attr($slide_url_value) : '';

    $services_loop = new WP_Query(
            'post_type' => 'services',
            'posts_per_page' => -1,
            'post_status' => 'publish'

    <div class="slider meta">
            <label for="slide-url"><?php _e('Select URL from the dropdown below for this slide', 'rmtheme' ); ?></label>
            <br />
            <select name="_slide_url">
                <?php while ($services_loop->have_posts()) : $services_loop->the_post(); ?>
                    <option value="<?php the_permalink(); ?>"><?php the_title(); ?></option>
                <?php endwhile; ?>



function rm_save_slider_meta($post) {

    global $post;

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
        return $post_id;

    if (!isset($_POST['slider_nonce']) || !wp_verify_nonce($_POST['slider_nonce'], basename(__FILE__)))
        return $post_id;

    if (!current_user_can('edit_post'))
        return $post_id;

    $allowed = array(   
        'a' => array(
            'href' => array()

    if (isset($_POST['_slide_url']))
        update_post_meta($post->ID, '_slide_url', wp_kses($_POST['_slide_url'], $allowed));


add_action('save_post', 'rm_save_slider_meta');
add_action('add_meta_boxes', 'rm_slider_metabox');

1 Answer 1


(0. Retrieve the meta value with get_post_custom (in order to highlight the selected value in the select input).)

  1. Query posts with get_posts( array( 'post_type' => 'services', 'post_status' => 'publish' ) )

  2. Loop through the results with foreach and build your select input.

  3. Hook into save_post (add_action('save_post', 'my_save_meta') to store the selected value (with update_post_meta).

  • I added my current code to the original post. But Im getting Notice: Undefined variable error: Check the image here: img31.imageshack.us/img31/8192/bbrt.png Commented Nov 6, 2013 at 14:19
  • Try changing rm_save_slider_meta($post) to rm_save_slider_meta($post_id)?
    – ptriek
    Commented Nov 6, 2013 at 14:31
  • That got rid of the first error. Should I also change that in services-post-type? Wont it break the code somehow? Btw that error only shows when creating new post. I guess its because the post->ID is not defined because the post is not yet saved. But when I save it it dissapears. The select seems to work. I get urls and they display on the homepage but when I save the select option value is changed to the default - first post. Commented Nov 6, 2013 at 14:43
  • 1. change that in services-post-type: not sure what you mean - all I know is that the save_post hook takes the ID as a parameter. 2. Select changed to default upon save: you need to add 'selected' to the <option> with the same URL as the one you retrieved with get_post_meta.
    – ptriek
    Commented Nov 6, 2013 at 15:07
  • Also, personally I wouldn't store the permalink as the meta value, but the ID of the Services post. Code for selected option would then be something like <option value="<?php the_ID();?>" <?php if($slide_ID == get_the_ID()) { echo " selected" }?> ><?php the_title(); ?></option>
    – ptriek
    Commented Nov 6, 2013 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.