Retrieving metadata from CPT-metabox

add_action( 'add_meta_boxes', 'la_season_sponsor_add_metaboxes' );
function la_season_sponsor_add_metaboxes()
    global $post, $wp_query;
    $wp_query->post = $post;

    add_meta_box( 'la_season_sponsor_metaboxid', 'League Sponsor', 'la_seasons_sponsor_metaboxes_container', 'league', 'side', 'default' );
    function la_seasons_sponsor_metaboxes_container( $post )
        $values = get_post_custom( $post->ID );
        $selected = isset( $values['la_seasons_sponsor_metabox_select'] ) ? esc_attr( $values['la_seasons_sponsor_metabox_select'][0] ) : ”;
            <select name="la_seasons_sponsor_metabox_select" id="la_seasons_sponsor_metabox_select" style="width:100%;">

                <option value="select a sponsor">Please select a sponsor</option>
                    $laleaguesponsor = array( 'post_type' => 'sponsors', 'orderby' => 'title', 'order' => 'asc', );
                    $laleaguesponsorloop = new WP_Query( $laleaguesponsor );
                    while ( $laleaguesponsorloop->have_posts() ) : $laleaguesponsorloop->the_post();
                    ?> <option value="<?php the_title(); ?>" <?php selected( $selected, get_the_title(), get_the_id() ); ?> ><?php the_title(); ?></option>
                    <?php endwhile; wp_reset_postdata(); ?>
        <?php wp_reset_query(); ?>
        <input type="hidden" name="la_season_sponsor_nonce" id="la_season_sponsor_nonce" value="<?php echo wp_create_nonce( 'la_season_sponsor_nonce' ); ?>" />


add_action( 'save_post', 'la_seasons_sponsor_metaboxes_save' );
function la_seasons_sponsor_metaboxes_save( $post_id )
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    if( !isset( $_POST['la_season_sponsor_nonce'] ) || !wp_verify_nonce( $_POST['la_season_sponsor_nonce'], 'la_season_sponsor_nonce' ) ) return;

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

    if( isset( $_POST['la_seasons_sponsor_metabox_select'] ) )
        update_post_meta( $post_id, 'la_seasons_sponsor_metabox_select', esc_attr( $_POST['la_seasons_sponsor_metabox_select'] ) );


So I’ve fixed the permalink problem, with the added code at the top.
The Sponsorship CPT is showing within the metabox of the CPT league. It’s saving and I can read out the sponsor itself, which is great. But I can only display the title and not the metadata.

I’ve tried many things to alter this code, but nothing gives me the option to display the metadata from the chosen sponsor CPT.

Is it possible to save the ID and call on the metedata, permalink, info and so forth, from the sponsors CPT and not only the title?


Read more here: Retrieving metadata from CPT-metabox

Leave a Reply

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