0

I have an issue with custom meta boxes in a custom made plugin. In fact, the task is simple, but I am new in this field. I made a plugin that allows user to enter the movie title in a custom meta box. This title must be seen on frontend, on a single custom post page. My code is here:

MOVIES.PHP

function movie_cpt() {

$labels = array(
    'name'                  => _x( 'Movies', 'Post Type General Name', 'text_domain' ),
    'singular_name'         => _x( 'Movie', 'Post Type Singular Name', 'text_domain' ),
    'menu_name'             => __( 'Movies', 'text_domain' ),
    'name_admin_bar'        => __( 'Movies', 'text_domain' ),
    'archives'              => __( 'Movie Archive', 'text_domain' ),
    'attributes'            => __( 'Item Attributes', 'text_domain' ),
    'parent_item_colon'     => __( 'Parent Item:', 'text_domain' ),
    'all_items'             => __( 'All Movies', 'text_domain' ),
    'add_new_item'          => __( 'Add New Movie', 'text_domain' ),
    'add_new'               => __( 'Add New Movie', 'text_domain' ),
    'new_item'              => __( 'New Movie', 'text_domain' ),
    'edit_item'             => __( 'Edit Movie', 'text_domain' ),
    'update_item'           => __( 'Update Movie', 'text_domain' ),
    'view_item'             => __( 'View Movie', 'text_domain' ),
    'view_items'            => __( 'View Movie', 'text_domain' ),
    'search_items'          => __( 'Search Movies', 'text_domain' ),
    'not_found'             => __( 'Movie not Found', 'text_domain' ),
    'not_found_in_trash'    => __( 'Movie not Found in Trash', 'text_domain' ),
    'featured_image'        => __( 'Featured Image', 'text_domain' ),
    'set_featured_image'    => __( 'Set featured image', 'text_domain' ),
    'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
    'use_featured_image'    => __( 'Use as featured image', 'text_domain' ),
    'insert_into_item'      => __( 'Insert into item', 'text_domain' ),
    'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
    'items_list'            => __( 'Movie List', 'text_domain' ),
    'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
    'filter_items_list'     => __( 'Filter items list', 'text_domain' ),
);
$args = array(
    'label'                 => __( 'Movie', 'text_domain' ),
    'description'           => __( 'Post Type Description', 'text_domain' ),
    'labels'                => $labels,
    'supports'              => array( 'title', 'editor'),
    'taxonomies'            => array( 'category', 'post_tag' ),
    'hierarchical'          => false,
    'public'                => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 5,
    'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'has_archive'           => true,
    'exclude_from_search'   => false,
    'publicly_queryable'    => true,
    'capability_type'       => 'page',
    'show_in_rest'          => true,
    'register_meta_box_cb'  => 'movie_meta_box'
);
register_post_type( 'movie', $args );

}
add_action( 'init', 'movie_cpt');

function movie_meta_box() {
    add_meta_box(
        'movie-title',
        __('Movie Title', 'sitepoint'),
        'movie_title_meta_box_callback',
    );
}

add_action('add_meta_boxes_movie', 'movie_meta_box');

function movie_title_meta_box_callback($post) {
    wp_nonce_field('movie_title_nonce', 'movie_title_nonce');
    $value = get_post_meta($post->ID, '_movie_title', true);
    echo '<textarea style = "width:100%" id="movie_title" name="movie_title">' . esc_attr( $value ) . '</textarea>';
}
function save_movie_title_meta_box_data($post_id) {
    if (!isset($_POST['movie_title_nonce'])) {
        return;
    }
    if(!wp_verify_nonce( $_POST['movie_title_nonce'], 'movie_title_nonce' )) {
        return;
    }
    if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    if(isset($_POST['post_type']) && 'page' == $_POST['post_type']) {
        if ( ! current_user_can( 'edit_page', $post_id ) ) {
            return;
        }
    }
    else {
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
    }
    if(!isset($_POST['movie_title'])) { 
        return; 
    }
    $my_data = sanitize_text_field( $_POST['movie_title'] );
    update_post_meta( $post_id, '_movie_title', $my_data );
}

add_action('save_post', 'save_movie_title_meta_box_data');

File SINGLE-MOVIE.PHP is here:

<?php get_header();

$m_title = get_post_meta(get_the_ID(), 'movie-title', true)''

if(have_posts()) :
    while(have_posts()) : the_post();
    if(isset($m_title) && $m_title !== '') {
        echo printf('<h1><strong>Movie Title: </strong>', $m_title, get_the_title());
    }
        the_content();
    endwhile;
endif;
get_footer();
?>

So, I think I made this code good in general, but I can't see a value of custom meta field "Movie Title" like I want on frontend. What can be a problem here and how to solve it? Thank you in advance.

2
  • Did you try adding your $m_title variable declaration to the line after you start your while loop?
    – rudtek
    Nov 28, 2022 at 4:09
  • @rudtek Yes, just like it is wrtitten in a next comment (see Shiddikur's coment please). But it didn't work for me. Nov 28, 2022 at 6:03

1 Answer 1

0

// For Movie Template Page

<?php

/*
 * Template Name: Movie Page
 *
 * */


get_header();

    $args = array(
        'post_type'=>'movie',
    );
    $m_title = new WP_Query( $args ); ?>

<?php if ( $m_title->have_posts() ) : ?>

    <!-- the loop -->
    <?php while ( $m_title->have_posts() ) : $m_title->the_post(); ?>
        <?php $get_m_title = get_post_meta(get_the_ID(), '_movie_title', true); ?>
        <h1>Movie Title: </h1> <?php echo $get_m_title; ?> <br> <h3>Content:</h3> <?php the_content(); ?>

    <?php endwhile; ?>
    <!-- end of the loop -->

    <?php wp_reset_postdata(); ?>

<?php else : ?>
    <p><?php _e( 'Sorry, no movie found.' ); ?></p>
<?php endif; ?>

<?php get_footer(); ?>

// For Movie Single Page

<?php get_header();

$m_title = get_post_meta(get_the_ID(), '_movie_title', true);

if(have_posts()) :
    while(have_posts()) : the_post();
        if(isset($m_title) && $m_title !== '') {?>
            
            <h1><strong>Movie Title: </strong><?php echo $m_title; ?>
            <?php echo get_the_title(); ?>
            
        <?php }
        the_content();
    endwhile;
endif;
get_footer();
?>
8
  • Dear @Shiddikur, thank you for answering. Unfortunately, this method didn't solve the problem. Again I can't see a value of meta-field in the page. Maybe the problem is somewhere else? Movies.php and movie-single.php are the only two files I have. Movies.php is under wp-content/plugins/movies, and single-movie.php is under wp-content/themes/twentytwentythree Nov 28, 2022 at 5:59
  • I also made this type of change, again without results. Really don't know what can be a problem. <?php get_header(); ?> <?php $args = array{ 'post_type' => 'movie' } $m_title = new WP_Query($args); if($m_title->have_posts()): while($m_title->have_posts()): $m_title->the_post(); $meta = get_post_meta($post->ID, 'movie-title', true); ?> <h1>Movie Title: </h1> <?php echo $meta['movie-title']; ?> <br> <h3>Content:</h3> <?php the_content(); ?> <?php endwhile; endif; wp_reset_postdata(); get_footer(); ?> Nov 28, 2022 at 6:32
  • Here I see all works good. Nov 28, 2022 at 6:58
  • So, "For Movie Template Page" stands for archive page of 2023 WP template, and "// For Movie Single Page" stands for single-movie.php page? I made just single-movie.php and value of metabox value is still not shown on the frontend. Really disappointing for me, don't know where is the mistake. Nov 28, 2022 at 7:32
  • @NovicaVojinovic If you want to then create a staging and give me credential then I can debug the issue, what was the problem there. My skype is shiddik96 Nov 28, 2022 at 9:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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