Question

I created a metabox in admin using wp_dropdown_pages() to display the list of posts from my custom post type workshop. The metabox dropdown works fine in admin, I can select the post title and save it.

The issue is, when I try to display the value of the metabox in frontend it only returns the ID of the post selected, not the title.

How can I return the title of the selected post in frontend?

The PHP code of the metabox:

add_action( 'add_meta_boxes', 'mysite_work_add_meta_box' );
if ( ! function_exists( 'mysite_work_add_meta_box' ) ) {
    /**
     * Add meta box to page screen
     *
     * @since 1.0.0
     */

    function mysite_work_add_meta_box() {
        add_meta_box( 'additional-work-metabox-options', esc_html__( 'Info', 'mysite' ), 'mysite_work_metabox_controls', 'work', 'normal', 'low' );
    }
}

if ( ! function_exists( 'mysite_work_metabox_controls' ) ) {
    /**
     * Meta box render function
     *
     * @param  object $post Post object.
     * @since  1.0.0
     */

    function mysite_work_metabox_controls( $post ) {
        $meta = get_post_meta( $post->ID );
        $workshop_page_select = ( isset( $meta['workshop_page_select'][0] ) && '' !== $meta['workshop_page_select'][0] ) ? $meta['workshop_page_select'][0] : '';
        wp_nonce_field( 'mysite_work_control_meta_box', 'mysite_work_control_meta_box_nonce' ); // Always add nonce to your meta boxes!
        ?>
        <div class="post_meta_extras">
            <p>
                <?php
                $args_pages = array(
                    'post_type'                         => 'workshop',
                    'orderby'                           => 'publish_date',
                    'depth'                 => 0,
                    'child_of'              => 0,
                    'selected'              => $workshop_page_select,
                    'echo'                  => 1,
                    'name'                  => 'workshop_page_select',
                    'id'                    => 'workshop_page_select',
                    'class'                 => null,
                    'show_option_none'      => null,
                    'show_option_no_change' => null,
                    'option_none_value'     => esc_html__( '&ndash; Elegir &ndash;', 'mysite' ),
                );
                ?>
                <label for="workshop_page_select"><?php esc_attr_e( 'Taller relacionado', 'mysite' ); ?></label>
                <?php wp_dropdown_pages( $args_pages ); ?>
            </p>

            <?php
    }
}

add_action( 'save_post', 'mysite_work_save_metaboxes' );

if ( ! function_exists( 'mysite_work_save_metaboxes' ) ) {
    /**
     * Save controls from the meta boxes
     *
     * @param  int $post_id Current post id.
     * @since 1.0.0
     */
    function mysite_work_save_metaboxes( $post_id ) {

        if ( ! isset( $_POST['mysite_work_control_meta_box_nonce'] ) || ! wp_verify_nonce( sanitize_key( $_POST['mysite_work_control_meta_box_nonce'] ), 'mysite_work_control_meta_box' ) ) { // Input var okay.
            return $post_id;
        }

        if ( isset( $_POST['post_type'] ) && 'page' === $_POST['post_type'] ) { // Input var okay.
            if ( ! current_user_can( 'edit_page', $post_id ) ) {
                return $post_id;
            }
        } else {
            if ( ! current_user_can( 'edit_post', $post_id ) ) {
                return $post_id;
            }
        }

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

        if ( isset( $_POST['workshop_page_select'] ) ) { // Input var okay.
            update_post_meta( $post_id, 'workshop_page_select', sanitize_text_field( wp_unslash( $_POST['workshop_page_select'] ) ) ); // Input var okay.
        }

    }
}

The code in frontend:

<?php 
$related_workshop = get_post_meta( get_the_ID(), 'workshop_page_select', true ); 
if ( isset( $related_workshop ) && '' !== $related_workshop ) : 
  echo $related_workshop; 
endif; 
?>

Thank you.

Was it helpful?

Solution

That's because, as documented the value of options in wp_dropdown_pages() is the ID:

'value_field' (string) Post field used to populate the 'value' attribute of the option elements. Accepts any valid post field. Default 'ID'.

So since you've saved the ID, use that to get the title:

echo get_the_title( $related_workshop );
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top