• I have registered a custom post type named ‘worksn’ as per the codex and I have added a custom metabox to it. The post type and the box show properly in the admin area and when entered the information saves properly. The problem I have is when I try to display the metabox on the front end. Hre is my code for the metabox (taken from the codex):

    /**
     * Adds a box to the main column on edit screen.
     */
    function myplugin_add_custom_box() {
    
        $screens = array( 'worksn' );
        foreach ( $screens as $screen ) {
            add_meta_box(
                'myplugin_sectionid',
                __( 'Work Description', 'myplugin_textdomain' ),
                'myplugin_inner_custom_box',
                $screen
            );
        }
    }
    add_action( 'add_meta_boxes', 'myplugin_add_custom_box' );
    /**
     * Prints the box content.
     *
     * @param WP_Post $post The object for the current post/page.
     */
    function myplugin_inner_custom_box( $post ) {
      // Add an nonce field so we can check for it later.
      wp_nonce_field( 'myplugin_inner_custom_box', 'myplugin_inner_custom_box_nonce' );
      /*
       * Use get_post_meta() to retrieve an existing value
       * from the database and use the value for the form.
       */
      $value = get_post_meta( $post->ID, '_my_meta_value_key', true );
      echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="' . esc_attr( $value ) . '" style="width: 98%" />';
    }
    /**
     * When the post is saved, saves our custom data.
     *
     * @param int $post_id The ID of the post being saved.
     */
    function myplugin_save_postdata( $post_id ) {
      /*
       * We need to verify this came from the our screen and with proper authorization,
       * because save_post can be triggered at other times.
       */
      // Check if our nonce is set.
      if ( ! isset( $_POST['myplugin_inner_custom_box_nonce'] ) )
        return $post_id;
      $nonce = $_POST['myplugin_inner_custom_box_nonce'];
      // Verify that the nonce is valid.
      if ( ! wp_verify_nonce( $nonce, 'myplugin_inner_custom_box' ) )
          return $post_id;
      // If this is an autosave, our form has not been submitted, so we don't want to do anything.
      if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
          return $post_id;
      // Check the user's permissions.
      if ( 'page' == $_POST['post_type'] ) {
        if ( ! current_user_can( 'edit_page', $post_id ) )
            return $post_id;
      } else {
        if ( ! current_user_can( 'edit_post', $post_id ) )
            return $post_id;
      }
      /* OK, its safe for us to save the data now. */
      // Sanitize user input.
      $mydata = sanitize_text_field( $_POST['myplugin_new_field'] );
      // Update the meta field in the database.
      update_post_meta( $post_id, '_my_meta_value_key', $mydata );
    }
    add_action( 'save_post', 'myplugin_save_postdata' );

    After many tries and errors I failed to display the metabox. Here is what I have so far (it does not work):

    <p><?php $value = get_post_meta( $post->ID, '_my_meta_value_key', true ); echo $value; ?></p>

  • The topic ‘Problem with displaying custom metabox from custom post type’ is closed to new replies.