Meta boxes not saving

时间:2015-08-11 作者:Cristina Martín Ruesgas

我有一篇带有两个自定义元框的自定义帖子,我使用本教程添加了这两个元框:http://wptheming.com/2010/08/custom-metabox-for-post-type/

当我更新帖子时,它消失了。我的代码:

function my_create_post_types() {


// Set UI labels for Custom Post Type
    $labels = array(
        \'name\'                => _x( \'Startups\', \'Post Type General Name\', \'twentythirteen\' ),
        \'singular_name\'       => _x( \'Startup\', \'Post Type Singular Name\', \'twentythirteen\' ),
        \'menu_name\'           => __( \'Startups\', \'twentythirteen\' ),
        \'parent_item_colon\'   => __( \'Padre Startup\', \'twentythirteen\' ),
        \'all_items\'           => __( \'Todas las Startups\', \'twentythirteen\' ),
        \'view_item\'           => __( \'Ver Startup\', \'twentythirteen\' ),
        \'add_new_item\'        => __( \'Añadir nueva Startup\', \'twentythirteen\' ),
        \'add_new\'             => __( \'Añadir nueva\', \'twentythirteen\' ),
        \'edit_item\'           => __( \'Editar Startup\', \'twentythirteen\' ),
        \'update_item\'         => __( \'Actualizar Startup\', \'twentythirteen\' ),
        \'search_items\'        => __( \'Buscar Startup\', \'twentythirteen\' ),
        \'not_found\'           => __( \'No encontrado\', \'twentythirteen\' ),
        \'not_found_in_trash\'  => __( \'No encontrado\', \'twentythirteen\' ),
    );


// Set other options for Custom Post Type

    $args = array(
        \'label\'               => __( \'Startups\', \'twentythirteen\' ),
        \'description\'         => __( \'Startup news and reviews\', \'twentythirteen\' ),
        \'labels\'              => $labels,
        // Features this CPT supports in Post Editor
        \'supports\'            => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'comments\', \'revisions\', \'custom-fields\', \'page-attributes\' ,\'post-formats\',\'tags\'),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        \'taxonomies\'          => array(\'post_tag\'),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */  
        \'hierarchical\'        => false,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_position\'       => 5,
        \'register_meta_box_cb\' => \'add_url_metaboxes\',
        \'menu_icon\'           => \'http://cink-innovation.es/hub/wp-content/themes/hub/images/startups.png\',
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'capability_type\'     => \'page\',
    );

                        $img_src = $retina === \'true\' ? aq_resize( get_post_meta( $post_id, \'krown_post_image\', true ), \'1340\', null ) : aq_resize( get_post_meta( $post_id, \'krown_post_image\', true ), \'680\', null );


// Registering your Custom Post Type
    register_post_type( \'Startups\', $args);


// Set UI labels for Custom Post Type
    $labels = array(
        \'name\'                => _x( \'Mentores\', \'Post Type General Name\', \'shadow\' ),
        \'singular_name\'       => _x( \'Mentores\', \'Post Type Singular Name\', \'shadow\' ),
        \'menu_name\'           => __( \'Mentores\', \'shadow\' ),
        \'parent_item_colon\'   => __( \'Padre Mentores\', \'shadow\' ),
        \'all_items\'           => __( \'Todas los Mentores\', \'shadow\' ),
        \'view_item\'           => __( \'Ver Mentores\', \'shadow\' ),
        \'add_new_item\'        => __( \'Añadir nuevo Mentor\', \'shadow\' ),
        \'add_new\'             => __( \'Añadir nuevo\', \'shadow\' ),
        \'edit_item\'           => __( \'Editar Mentor\', \'shadow\' ),
        \'update_item\'         => __( \'Actualizar Mentor\', \'shadow\' ),
        \'search_items\'        => __( \'Buscar Mentor\', \'shadow\' ),
        \'not_found\'           => __( \'No encontrado\', \'shadow\' ),
        \'not_found_in_trash\'  => __( \'No encontrado\', \'shadow\' ),
    );

// Set other options for Custom Post Type

    $args = array(
        \'label\'               => __( \'Mentores\', \'shadow\' ),
        \'description\'         => __( \'Mentores news and reviews\', \'shadow\' ),
        \'labels\'              => $labels,
        // Features this CPT supports in Post Editor
        \'supports\'            => array( \'title\', \'editor\', \'excerpt\', \'author\', \'thumbnail\', \'comments\', \'revisions\', \'custom-fields\', \'page-attributes\' ,\'post-formats\',\'tags\'),
        // You can associate this CPT with a taxonomy or custom taxonomy. 
        \'taxonomies\'          => array(\'post_tag\'),
        /* A hierarchical CPT is like Pages and can have
        * Parent and child items. A non-hierarchical CPT
        * is like Posts.
        */  
        \'hierarchical\'        => false,
        \'public\'              => true,
        \'show_ui\'             => true,
        \'show_in_menu\'        => true,
        \'show_in_nav_menus\'   => true,
        \'show_in_admin_bar\'   => true,
        \'menu_position\'       => 6,
        \'menu_icon\'           => \'http://cink-innovation.es/hub/wp-content/themes/hub/images/mentores.png\',
        \'can_export\'          => true,
        \'has_archive\'         => true,
        \'exclude_from_search\' => false,
        \'publicly_queryable\'  => true,
        \'capability_type\'     => \'page\',

    );

                        $img_src = $retina === \'true\' ? aq_resize( get_post_meta( $post_id, \'krown_post_image\', true ), \'1340\', null ) : aq_resize( get_post_meta( $post_id, \'krown_post_image\', true ), \'680\', null );

    // Registering your Custom Post Type
    register_post_type( \'Mentores\', $args); 



}


/* Hook into the \'init\' action so that the function
* Containing our post type registration is not 
* unnecessarily executed. 
*/

add_action( \'init\', \'my_create_post_types\' );


/*.........................CUSTOM FIELDS...................................*/


function add_url_metaboxes() {
    add_meta_box(\'wpt_url_boxes\', \'Información Adicional\', \'wpt_url_boxes\', \'startups\', \'normal\', \'high\');
    }

// Metaboxes

function wpt_url_boxes() {
    global $post;


    // Noncename needed to verify where the data originated
    echo \'<input type="hidden" name="urlmeta_noncename" id="urltmeta_noncename" value="\' . 
    wp_create_nonce( plugin_basename(__FILE__) ) . \'" />\';

    // Get the location data if its already been entered
    $web = get_post_meta($post->ID, \'_web\', true);
        $face = get_post_meta($post->ID, \'_face\', true);

    // Echo out the field
        echo \'<p>Url de la Startup:</p>\';
    echo \'<input type="text" name="_web" value="\' . $web  . \'" class="wideurl" />\';
        echo \'<p>Dirección de Facebook:</p>\';
        echo \'<input type="text" name="_face" value="\' . $face  . \'" class="wideface" />\';

}

// Save the Metabox Data

function wpt_save_url_meta($post_id, $post) {



    // verify this came from the our screen and with proper authorization,
    // because save_post can be triggered at other times
    if ( !wp_verify_nonce( $_POST[\'urlmeta_noncename\'], plugin_basename(__FILE__) )) {
    return $post->ID;
    }

    // Is the user allowed to edit the post or page?

    if ( \'Startups\' == $_POST[\'post_type\'] ) {
} else {
    if ( !current_user_can( \'edit_post\', $post_id ) )
        return;
}
    if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
        return;

    // OK, we\'re authenticated: we need to find and save the data
    // We\'ll put it into an array to make it easier to loop though.

    $url_meta[\'_web\'] = $_POST[\'_web\'];
    $url_meta[\'_face\'] = $_POST[\'_face\'];

    // Add values of $events_meta as custom fields

    foreach ($url_meta as $key => $value) { // Cycle through the $events_meta array!
        if( $post->post_type == \'startups\' ) return; // Don\'t store custom data twice
        $value = implode(\',\', (array)$value); // If $value is an array, make it a CSV (unlikely)
        if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
            update_post_meta($post->ID, $key, $value);
        } else { // If the custom field doesn\'t have a value
            add_post_meta($post->ID, $key, $value);
        }
        if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
    }

}

add_action(\'save_post\', \'wpt_save_url_meta\', 1, 2); // save the custom fields
有什么想法吗?谢谢

1 个回复
SO网友:TheGentleman

我认为问题在于这一行:

     if( $post->post_type == \'startups\' ) return;
你的自定义帖子不总是有这样的类型吗startups? 基本上,您是在告诉代码,每当我保存自定义帖子类型时,请忽略自定义字段。您可能打算使用一种WP保留类型(例如。revisionattachment) 这将防止您的元数据以不方便的方式被覆盖。

我会把那行改成

     if( $post->post_type == \'revision\' ) return;
或者您可以完全删除该行(出于测试目的)。

结束

相关推荐

Related posts with WP_Query

我一直在查询相关帖子。我需要相关的职位,以显示任何深度从主家长。我的网站上有以下结构类别:- Category level 1 -- Category level 2 --- Category level 3 *---- Post in level 3* --- Category level 3 *--- Post in level 3* -- Category level 2 -- Category level 2 当我们看到*----