在将CVS文件中的数据解析为自定义post类型时,我想将数据解析为我创建的元框的字段。
元框字段通常更新如下:
function fredningszone_meta_box() {
add_meta_box(
\'punkt_fredningszone\', // $id - Meta box ID (used in the \'id\' attribute for the meta box).
\'Data for fredningszone\', // $title - Title of the meta box.
\'punkt_fredningszone_callback\', // $callback - Function that fills the box with the desired content. The function should echo its output.
\'punkt\', // $screen - he screen or screens on which to show the box (such as a post type, \'link\', or \'comment\').
\'normal\', // $context - The context within the screen where the boxes should display.
\'high\' // $priority - The priority within the context where the boxes should show (\'high\', \'low\').
);
}
add_action( \'add_meta_boxes\', \'fredningszone_meta_box\' );
/**
* Enable and display custom fields.
**/
function punkt_fredningszone_callback() {
global $post;
$meta = get_post_meta( $post->ID, \'fredningszone_data\', true ); ?>
<input type="hidden" name="fredningszone_data_nonce" value="<?php echo wp_create_nonce( basename(__FILE__) ); ?>">
<!-- All fields goes below this line -->
<p>
<label for="fredningszone_data[id]">ID</label>
<br>
<input type="text" name="fredningszone_data[id]" id="fredningszone_data[id]" class="regular-text widefat" placeholder="Indtast fredningszonens ID (f.eks 450)" value="<?php echo $meta[\'id\']; ?>">
</p>
。。但是,当我通过我构建的另一个功能解析来自CSV的数据时,我无法访问元框的字段。请参见下面的代码:
下面的代码是我试图构建的导入插件的一段代码,其中CSV数据不会解析为meta box的字段。见下文:
$check_post_exists = function( $title ) use ( $wpdb, $postTypeArray ) {
// Get an array of all posts within our custom post type
$posts = $wpdb->get_col( "SELECT post_title FROM {$wpdb->posts} WHERE post_type = \'{$postTypeArray["custom-post-type"]}\' AND post_status = \'publish\'" );
// Check if the passed title exists in array
return in_array( $title, $posts );
};
$i = 0;
foreach ( $posts() as $post ) {
// If the post exists, skip this post and go to the next one
if ( $check_post_exists( $post["zoneid"] ) ) {
continue;
}
$i++;
// Insert the post into the database
$post["id"] = wp_insert_post( array(
"post_title" => $post["zoneid"],
"post_content" => $post["bemaerkning"],
"post_type" => $postTypeArray["custom-post-type"],
"post_punktcat" => array( 4 ),
"post_status" => "publish"
));
// Set post category to 4
wp_set_post_terms($post["id"], 4, \'punktcat\', false );
$meta = get_post_meta($post["id"], \'fredningszone_data\', true );
var_dump($meta);
// THIS IS THE FIELD I WANT TO UPDATE, BUT I HAVE NOT YET SUCCESFULLY POPULATED THE FIELD YET
update_post_meta($post["id"], $meta["id"], $post["zoneid"]);
// Update Custom Meta
update_post_meta($post["id"], \'_location\', $post["zoneid"]);
}
echo \'<div id="message" class="updated fade"><p>\' . $i . \' posts have succesfully been imported from CSV!\' . \'</p></div>\';
如何将数据解析到自定义元框的字段中?
最合适的回答,由SO网友:Dougless 整理而成
可以我自己解决了这个问题。我没有以正确的方式填充meta\\u键的数组。
下面,您将找到一个可行的解决方案:
$check_post_exists = function( $title ) use ( $wpdb, $postTypeArray ) {
// Get an array of all posts within our custom post type
$posts = $wpdb->get_col( "SELECT post_title FROM {$wpdb->posts} WHERE post_type = \'{$postTypeArray["custom-post-type"]}\' AND post_status = \'publish\'" );
// Check if the passed title exists in array
return in_array( $title, $posts );
};
$i = 0;
$e = 0;
foreach ( $posts() as $post ) {
// If the post exists, skip this post and go to the next one
if ( $check_post_exists( $post["properties/ID"] ) ) {
$e++;
continue;
}
$i++;
// Insert the post into the database
$post["id"] = wp_insert_post( array(
"post_title" => $post["properties/ID"],
"post_type" => $postTypeArray["custom-post-type"],
"post_punktcat" => array( 4 ),
"post_status" => "publish"
));
// Set post category to 4
wp_set_post_terms($post["id"], 4, \'punktcat\', false );
$meta = get_post_meta( $post["id"], \'fredningszone_data\', true );
$fredningszone_data = array(
"id" => $post["properties/ID"],
"navn" => $post["properties/NAVN"],
"kontaktsted" => $post["properties/Kontaktsted"],
"fredningsperiode" => $post["properties/FREDNINGSP"],
"periode_type" => $post["properties/PeriodeType"],
"lovgivningsgrundlag" => $post["properties/LOVGRUNDLA"],
"bemaerkning" => $post["properties/BEMARKNING"],
"www" => $post["properties/WWW"],
"www2" => $post["properties/WWW2"],
"geometry" => $post["geometry/type"]
);
// Update meta box values
update_post_meta($post["id"], \'fredningszone_data\', $fredningszone_data);
//update_post_meta($post["id"], \'fredningszone_data\', $post["zoneid"]);
update_post_meta($post["id"], \'_location\', $post["zoneid"]);
}