• Hello,

    I am a multimedia student and in my final degree work I am developing a website where I have implemented a custom post type.

    The case is that I added metaboxes , but the information is not stored in the database. Furthemore I can’t see the data that has been entered once the post has been updated.

    Next I show you the code that I have implemented.

    function learning_tools_meta_boxes() {
        //add_meta_box($id, $title, $callback, $screen, $context, $priority, $callback_args);
    	add_meta_box('learning_tools',  __('Learning Tools'), 'learning_tools_output_meta_box', 'Learning Tools', 'normal', 'high');
    }
    add_action('add_meta_boxes', 'learning_tools_meta_boxes');
    
    function learning_tools_output_meta_box($post) {
    
        $learning_tools_date = get_post_meta($post->ID, '_learning_tools_date', true);
        $learning_tools_hour = get_post_meta($post->ID, '_learning_tools_hour', true);
        $learning_tools_duration = get_post_meta($post->ID, '_learning_tools_duration', true);
        $learning_tools_language = get_post_meta($post->ID, '_learning_tools_language', true);
        $learning_tools_modality = get_post_meta($post->ID, '_learning_tools_modality', true);
    
        // Usaremos este nonce field más adelante cuando guardemos en learning_tools_save_meta_box()
    	wp_nonce_field( 'save_learning_tools', 'learning_tools_nonce' );
    
        // Date
        echo('<label for="learning_tools_date">' . __('Date', 'text_domain') . '</label> ');
        echo('<input type="date" name="learning_tools_date" id="learning_tools_date" value=""><br><br>');
    
        // Hour
        echo('<label for="learning_tools_hour">' . __('Hour', 'text_domain') . '</label> ');
        echo('<input type="time" name="learning_tools_hour" id="learning_tools_hour" value=""><br><br>');
    
        // Duration
        echo('<label for="learning_tools_duration">' . __('Duration', 'text_domain') . '</label> ');
        echo('<input type="number" name="learning_tools_duration" id="learning_tools_duration" value=""> h <br><br>');
        
        // Language
        echo('<label for="learning_tools_language">' . __('Language: ', 'text_domain') . '</label> ');
    
        echo('<label for="learning_tools_english">' . __('English ', 'text_domain') . '</label> ');
        echo('<input type="checkbox" name="learning_tools_english" id="learning_tools_english" value="">');
    
        echo('<label for="learning_tools_german">' . __('German ', 'text_domain') . '</label> ');
        echo('<input type="checkbox" name="learning_tools_german" id="learning_tools_german" value="">');
    
        echo('<label for="learning_tools_italian">' . __('Italian ', 'text_domain') . '</label> ');
        echo('<input type="checkbox" name="learning_tools_italian" id="learning_tools_italian" value=""><br><br>');
    
        // Modality
        echo('<label for="learning_tools_modality">' . __('Modality ', 'text_domain') . '</label> ');
        echo('<select name="learning_tools_modality" id="learning_tools_modality"><br><br>');
        echo('<option value="Online">' .__( 'Online', 'learning_tools_textdomain' ).'</option>');
        echo('<option value="On-site">' .__( 'On-site', 'learning_tools_textdomain' ).'</option>');
        echo('</select>');
    }
    
    function learning_tools_save_meta_boxes( $post_id ) {
    
        if(!isset($_POST['learning_tools_nonce']) || ! wp_verify_nonce($_POST['learning_tools_nonce'], 'save_learning_tools')) {
    		return $post_id;
    	}
     
        // Comprovem que Learning Tools és un CPT
        if ( 'learning_tools_date' != $_POST['post_type'] ) {
            return $post_id;
        }
     
        // Comprovem l'usuari que està editant el post i els permisos per si ho pot fer
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return $post_id;
        }
     
        // Guardem totes les meta-dades
        $learning_tools_date = $_POST['learning_tools_date'];
        update_post_meta ($post_id, '_learning_tools_date', $learning_tools_date );
    
        /*$learning_tools_hour = sanitize_text_field( $_POST['learning_tools_hour'] );
        update_post_meta( $post_id, '_learning_tools_hour', $learning_tools_hour );
    
        $learning_tools_duration = sanitize_text_field( $_POST['learning_tools_duration'] );
        update_post_meta( $post_id, '_learning_tools_duration', $learning_tools_duration );
    
        $learning_tools_language = sanitize_text_field( $_POST['learning_tools_language'] );
        update_post_meta( $post_id, '_learning_tools_date', $learning_tools_language );
    
        $learning_tools_modality = sanitize_text_field( $_POST['learning_tools_modality'] );
        update_post_meta( $post_id, '_learning_tools_modality', $learning_tools_modality ); */
    
        return true;
         
    }
    add_action('save_post', 'learning_tools_save_meta_boxes');

    Could you tell me what I’m doing wrong?
    Thank you very much in advance and greetings!

    • This topic was modified 4 years, 10 months ago by gseurope.
Viewing 6 replies - 1 through 6 (of 6 total)
  • Can you tell me what is your post type?
    Make sure this part is correct

    
    // Comprovem que Learning Tools és un CPT
        if ( 'learning_tools_date' != $_POST['post_type'] ) {
            return $post_id;
        }
    

    Also you need to echo the value here like this.
    echo('<input type="date" name="learning_tools_date" id="learning_tools_date" value="'.$learning_tools_date.'"><br><br>');

    • This reply was modified 4 years, 10 months ago by irizweb.
    • This reply was modified 4 years, 10 months ago by irizweb.
    • This reply was modified 4 years, 10 months ago by irizweb.
    Thread Starter gseurope

    (@gseurope)

    Hello,

    Thank you for your comments!
    I have the modifications what you tell me in the first part because it wasn’t correct as I had it.

    Furthermore I have modified the part of the code that you tell me about the input type value, but still it still does not store nothing in the Database.

    To check-I do the following query in the database:
    SELECT P.ID, P.post_title, M.meta_key, M.meta_value FROM wp_posts AS P INNER JOIN wp_postmeta AS M ON M.post_id = P.ID WHERE P.post_type = 'learning_tools' and P.post_status = 'publish' ORDER BY post_title, meta_key

    On the other hand, my custom post type is called learning_tools_cpt and it is used to add information about courses. (course day, start hour, time, language, and online or on-site modality)

    Thanks!

    Do you see the date after saving now? first you can trouble shoot one by one.
    See if it’s showing, it should. I tested it myself with your code. Recheck your code if it’s not working.

    You can also do a simple query and check if the meta value is added to the table SELECT * FROM wp_postmeta WHERE meta_key='_learning_tools_date'

    Then check the wp_postmeta post_id against wp_posts ID

    • This reply was modified 4 years, 10 months ago by irizweb.
    • This reply was modified 4 years, 10 months ago by irizweb.
    • This reply was modified 4 years, 10 months ago by irizweb.
    Thread Starter gseurope

    (@gseurope)

    Hi,

    I checked the database and the date still does not appear, in fact I have created a new post and in the database I only get one row and it’s a post that I created since 3 days ago. Tha value of this meta is NULL into the row that i can see.

    I am somewhat confused because the code does not finish working and prevents me from seeing the date in the Back Office and in the Database.

    This are the modifications that I done.
    Modify input echo from output meta box function:

    echo('<label for="learning_tools_date">' . __('Date', 'text_domain') . '</label> ');
        echo('<input type="date" name="learning_tools_date" id="learning_tools_date" value= "'.$learning_tools_date.'"><br><br>');

    Modify content in the function for save meta boxes:

    function learning_tools_save_meta_boxes( $post_id ) {
    
        if(!isset($_POST['learning_tools_nonce']) || ! wp_verify_nonce($_POST['learning_tools_nonce'], 'save_learning_tools')) {
    		return $post_id;
    	}
     
        if ( 'learning_tools_cpt' != $_POST['post_type'] ) {
            return $post_id;
        }
     
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return $post_id;
        }
     
        $learning_tools_date = $_POST['learning_tools_date'];
        update_post_meta ($post_id, '_learning_tools_date', $learning_tools_date );
    
    return true;
    }
    add_action('save_post', 'learning_tools_save_meta_boxes');

    Any suggestion or something you think I’m doing wrong?
    Thank you very much again

    Following code works for me fine. Check your add_meta_box. See if it works in your end. All these code added to functions.php? what theme are you using? try switching it to a default theme. If not send me the complete code and more details of your installation.

    function learning_tools_meta_boxes() {
        //add_meta_box($id, $title, $callback, $screen, $context, $priority, $callback_args);
    	add_meta_box('learning_tools',  __('Learning Tools'), 'learning_tools_output_meta_box', 'learning_tools_cpt', 'side', 'high');
    }
    add_action('add_meta_boxes', 'learning_tools_meta_boxes');
    
    function learning_tools_output_meta_box($post) {
    
        $learning_tools_date = get_post_meta($post->ID, '_learning_tools_date', true);
        $learning_tools_hour = get_post_meta($post->ID, '_learning_tools_hour', true);
        $learning_tools_duration = get_post_meta($post->ID, '_learning_tools_duration', true);
        $learning_tools_language = get_post_meta($post->ID, '_learning_tools_language', true);
        $learning_tools_modality = get_post_meta($post->ID, '_learning_tools_modality', true);
    
        // Usaremos este nonce field más adelante cuando guardemos en learning_tools_save_meta_box()
    	wp_nonce_field( 'save_learning_tools', 'learning_tools_nonce' );
    
        // Date
        echo('<label for="learning_tools_date">' . __('Date', 'text_domain') . '</label> ');
        echo('<input type="date" name="learning_tools_date" id="learning_tools_date" value= "'.$learning_tools_date.'"><br><br>');
    
        // Hour
        echo('<label for="learning_tools_hour">' . __('Hour', 'text_domain') . '</label> ');
        echo('<input type="time" name="learning_tools_hour" id="learning_tools_hour" value=""><br><br>');
    
        // Duration
        echo('<label for="learning_tools_duration">' . __('Duration', 'text_domain') . '</label> ');
        echo('<input type="number" name="learning_tools_duration" id="learning_tools_duration" value=""> h <br><br>');
        
        // Language
        echo('<label for="learning_tools_language">' . __('Language: ', 'text_domain') . '</label> ');
    
        echo('<label for="learning_tools_english">' . __('English ', 'text_domain') . '</label> ');
        echo('<input type="checkbox" name="learning_tools_english" id="learning_tools_english" value="">');
    
        echo('<label for="learning_tools_german">' . __('German ', 'text_domain') . '</label> ');
        echo('<input type="checkbox" name="learning_tools_german" id="learning_tools_german" value="">');
    
        echo('<label for="learning_tools_italian">' . __('Italian ', 'text_domain') . '</label> ');
        echo('<input type="checkbox" name="learning_tools_italian" id="learning_tools_italian" value=""><br><br>');
    
        // Modality
        echo('<label for="learning_tools_modality">' . __('Modality ', 'text_domain') . '</label> ');
        echo('<select name="learning_tools_modality" id="learning_tools_modality"><br><br>');
        echo('<option value="Online">' .__( 'Online', 'learning_tools_textdomain' ).'</option>');
        echo('<option value="On-site">' .__( 'On-site', 'learning_tools_textdomain' ).'</option>');
        echo('</select>');
    }
    
    function learning_tools_save_meta_boxes( $post_id ) {
    
        if(!isset($_POST['learning_tools_nonce']) || ! wp_verify_nonce($_POST['learning_tools_nonce'], 'save_learning_tools')) {
    		return $post_id;
    	}
     
        if ( 'learning_tools_cpt' != $_POST['post_type'] ) {
            return $post_id;
        }
     
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return $post_id;
        }
     
        $learning_tools_date = $_POST['learning_tools_date'];
        update_post_meta ($post_id, '_learning_tools_date', $learning_tools_date );
    
    return true;
    }
    add_action('save_post', 'learning_tools_save_meta_boxes');
    
    • This reply was modified 4 years, 10 months ago by irizweb.
    Thread Starter gseurope

    (@gseurope)

    Hello,

    Thanks again.
    I have reviewed your code and I see that the add_meta_box poses it differently than I do.

    I have tested your code and the custom fields of the back office disappear…i dont’ undestant why.

    On the other hand, all the developed code, I implement it in the functions.php file of my theme Recycle (Premium)

    This is the full code that i use for develop this funcionality:

    if ( ! function_exists('learning_tools_cpt') ) {
    
    // Creem la funció del Custom Post Type Learning Tools
    function learning_tools_cpt() {
    
    	$labels = array(
    		'name'                  => _x( 'Learning Tools', 'Post Type General Name', 'learning_tools_domain' ),
    		'singular_name'         => _x( 'Learning Tool', 'Post Type Singular Name', 'learning_tools_domain' ),
    		'menu_name'             => __( 'Learning Tools', 'learning_tools_domain' ),
    		'name_admin_bar'        => __( 'Learning Tools', 'learning_tools_domain' ),
    		'archives'              => __( 'Learning Tools Archives', 'learning_tools_domain' ),
    		'attributes'            => __( 'Learning Tool Attributes', 'learning_tools_domain' ),
    		'parent_item_colon'     => __( 'Parent Learning Tool:', 'learning_tools_domain' ),
    		'all_items'             => __( 'All Learning Tools', 'learning_tools_domain' ),
    		'add_new_item'          => __( 'Add New Learning Tool', 'learning_tools_domain' ),
    		'add_new'               => __( 'Add New', 'learning_tools_domain' ),
    		'new_item'              => __( 'New Learning Tool', 'learning_tools_domain' ),
    		'edit_item'             => __( 'Edit Learning Tool', 'learning_tools_domain' ),
    		'update_item'           => __( 'Update Learning Tool', 'learning_tools_domain' ),
    		'view_item'             => __( 'View Learning Tool', 'learning_tools_domain' ),
    		'view_items'            => __( 'View Learning Tools', 'learning_tools_domain' ),
    		'search_items'          => __( 'Search Learning Tool', 'learning_tools_domain' ),
    		'not_found'             => __( 'Not found', 'learning_tools_domain' ),
    		'not_found_in_trash'    => __( 'Not found in Trash', 'learning_tools_domain' ),
    		'featured_image'        => __( 'Featured Image', 'learning_tools_domain' ),
    		'set_featured_image'    => __( 'Set featured image', 'learning_tools_domain' ),
    		'remove_featured_image' => __( 'Remove featured image', 'learning_tools_domain' ),
    		'use_featured_image'    => __( 'Use as featured image', 'learning_tools_domain' ),
    		'insert_into_item'      => __( 'Insert into Learning Tool', 'learning_tools_domain' ),
    		'uploaded_to_this_item' => __( 'Uploaded to this Learning Tool', 'learning_tools_domain' ),
    		'items_list'            => __( 'Learning Tools list', 'learning_tools_domain' ),
    		'items_list_navigation' => __( 'Learning Tools list navigation', 'learning_tools_domain' ),
    		'filter_items_list'     => __( 'Filter Learning Tools list', 'learning_tools_domain' ),
    	);
    	$rewrite = array(
    		'slug'                  => 'learning-tools',
    		'with_front'            => true,
    		'pages'                 => true,
    		'feeds'                 => true,
    	);
    	$args = array(
    		'label'                 => __( 'Learning Tool', 'learning_tools_domain' ),
    		'description'           => __( 'Learning Tools For Entrepreneurs', 'learning_tools_domain' ),
    		'labels'                => $labels,
    		'supports'              => array( 'title', 'editor', 'thumbnail', 'comments', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes', 'post-formats' ),
    		'taxonomies'            => array( 'category', 'post_tag' ),
    		'hierarchical'          => false,
    		'public'                => true,
    		'show_ui'               => true,
    		'show_in_menu'          => true,
    		'menu_position'         => 25,
    		'menu_icon'             => 'dashicons-paperclip',
    		'show_in_admin_bar'     => true,
    		'show_in_nav_menus'     => true,
    		'can_export'            => true,
    		'has_archive'           => true,
    		'exclude_from_search'   => false,
    		'publicly_queryable'    => true,
    		'rewrite'               => $rewrite,
    		'capability_type'       => 'post',
    	);
    	register_post_type( 'Learning Tools', $args );
    
    }
    add_action( 'init', 'learning_tools_cpt', 0 );
    
    }
    
    function learning_tools_meta_boxes() {
        //add_meta_box($id, $title, $callback, $screen, $context, $priority, $callback_args);
        //add_meta_box('learning_tools',  __('Learning Tools'), 'learning_tools_output_meta_box', 'learning_tools_cpt', 'side', 'high');
          add_meta_box('learning_tools',  __('Learning Tools'), 'learning_tools_output_meta_box', 'Learning Tools', 'normal', 'high');
    }
    add_action('add_meta_boxes', 'learning_tools_meta_boxes');
    
    function learning_tools_output_meta_box($post) {
    
        $learning_tools_date = get_post_meta($post->ID, '_learning_tools_date', true);
        $learning_tools_hour = get_post_meta($post->ID, '_learning_tools_hour', true);
        $learning_tools_duration = get_post_meta($post->ID, '_learning_tools_duration', true);
        $learning_tools_language = get_post_meta($post->ID, '_learning_tools_language', true);
        $learning_tools_modality = get_post_meta($post->ID, '_learning_tools_modality', true);
    
        // Usaremos este nonce field más adelante cuando guardemos en learning_tools_save_meta_box()
            wp_nonce_field( 'save_learning_tools', 'learning_tools_nonce' );
    
        // Date
        echo('<label for="learning_tools_date">' . __('Date', 'text_domain') . '</label> ');
        echo('<input type="date" name="learning_tools_date" id="learning_tools_date" value= "'.$learning_tools_date.'"><br><br>');
    
        // Hour
        echo('<label for="learning_tools_hour">' . __('Hour', 'text_domain') . '</label> ');
        echo('<input type="time" name="learning_tools_hour" id="learning_tools_hour" value=""><br><br>');
    
        // Duration
        echo('<label for="learning_tools_duration">' . __('Duration', 'text_domain') . '</label> ');
        echo('<input type="number" name="learning_tools_duration" id="learning_tools_duration" value=""> h <br><br>');
    
        // Language
        echo('<label for="learning_tools_language">' . __('Language: ', 'text_domain') . '</label> ');
    
        echo('<label for="learning_tools_english">' . __('English ', 'text_domain') . '</label> ');
        echo('<input type="checkbox" name="learning_tools_english" id="learning_tools_english" value="">');
    
        echo('<label for="learning_tools_german">' . __('German ', 'text_domain') . '</label> ');
        echo('<input type="checkbox" name="learning_tools_german" id="learning_tools_german" value="">');
    
        echo('<label for="learning_tools_italian">' . __('Italian ', 'text_domain') . '</label> ');
        echo('<input type="checkbox" name="learning_tools_italian" id="learning_tools_italian" value=""><br><br>');
    
        // Modality
        echo('<label for="learning_tools_modality">' . __('Modality ', 'text_domain') . '</label> ');
        echo('<select name="learning_tools_modality" id="learning_tools_modality"><br><br>');
        echo('<option value="Online">' .__( 'Online', 'learning_tools_textdomain' ).'</option>');
        echo('<option value="On-site">' .__( 'On-site', 'learning_tools_textdomain' ).'</option>');
        echo('</select>');
    }
    
    function learning_tools_save_meta_boxes( $post_id ) {
    
        if(!isset($_POST['learning_tools_nonce']) || ! wp_verify_nonce($_POST['learning_tools_nonce'], 'save_learning_tools')) {
                    return $post_id;
            }
    
        if ( 'learning_tools_cpt' != $_POST['post_type'] ) {
            return $post_id;
        }
    
        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return $post_id;
        }
    
        $learning_tools_date = $_POST['learning_tools_date'];
        update_post_meta ($post_id, '_learning_tools_date', $learning_tools_date );
    
    return true;
    }
    add_action('save_post', 'learning_tools_save_meta_boxes');

    Thank you very much for the patience and your help.
    Cheers,

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Meta Boxes not save values’ is closed to new replies.