Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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(
        array(
            'post_type' => 'services',
            'posts_per_page' => -1,
            'post_status' => 'publish'
    ));


?>
    <div class="slider meta">
        <p>
            <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; ?>
            </select>
        </p>
    </div>

<?php 

}

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');
share|improve this question

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).

share|improve this answer
    
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 –  Richard Mišenčík Nov 6 '13 at 14:19
    
Try changing rm_save_slider_meta($post) to rm_save_slider_meta($post_id)? –  ptriek Nov 6 '13 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. –  Richard Mišenčík Nov 6 '13 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 Nov 6 '13 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 Nov 6 '13 at 15:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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