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__( '– Elegir –', '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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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 );
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0