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 have successfully pulled a custom post type through into a drop-down that is in a custom meta box. However, when displaying it on the front end I would like to also provide a link to the actual post, not just the name of the post. So I am guessing I need to save this as an array? Is this possible through a drop-down? Confused on how I should approach this. Any help is greatly appreciated.

Here is what I have so far:

// Add Meta Box To Select Overseeing Pastor
add_action('admin_init', 'ministry_select_add_meta');
function ministry_select_add_meta(){
    add_meta_box('ministry_select_post', __('Overseeing Pastor'), 'ministry_select_meta', 'ministry', 'side');
}

function ministry_select_meta( $post ) {
    $values = get_post_custom( $post->ID );
    $selected = isset( $values['pastor_select'] ) ? esc_attr( $values['pastor_select'][0] ) : '';
    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
    ?>
        <select name="pastor_select">
            <?php
            $args = array(
                'post_type' => 'employee',
                'position' => 'pastor'
            );
            $pastorList = new WP_Query($args); while ($pastorList->have_posts()) : $pastorList->the_post();
                $is_selected = (get_the_title() == $selected) ? 'selected="selected"' : '';
                echo '<option value="'.get_the_title().'" '.$is_selected.'>'.get_the_title().'</option>';
            endwhile; wp_reset_postdata();
            ?>
        </select>
    <?php   
}

add_action( 'save_post', 'ministry_select_save' );
function ministry_select_save( $post_id )
{
    // Stop If Autosaving
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    // Stop If Nonce Can't Be Verified
    if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;

    // Stop If Unauthorized User
    if( !current_user_can( 'edit_post' ) ) return;

    // Make Sure Data Is Set Then Save      
    if( isset( $_POST['pastor_select'] ) )
        update_post_meta( $post_id, 'pastor_select', esc_attr( $_POST['pastor_select'] ) );
}
share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

Quite simple. Instead of saving the post title, save the post ID, so in the front end you can extract all necessary info.
Like so: get_post( $the_saved_ID_value ); or get_permalink( $the_saved_ID_value );.

In you code, change this line:

echo '<option value="'.get_the_ID().'" '.$is_selected.'>'.get_the_title().'</option>';

Notes:

share|improve this answer
    
This is actually what I ended up doing but forgot to post it. Appreciate the answer! Thanks. –  ftntravis Feb 15 '13 at 21:25
add comment

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.