donglu3243 2019-01-23 21:14
浏览 9

如何将数据解析为具有多个字段的元框?

I successfully build a function, that is able to import data from a CSV into posts of a Custom Post Type. I am able to parse data into the native title and description fields, but the function fails to parse data into a meta box with multiple fields.

Here is a code snippet of the function that are parsing CSV data into post fields:

// Check and see if the current post exists within the database.
$check_post_exists = function( $title ) use ( $wpdb, $postTypeArray ) {

// Get an array of all posts within the 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 the value "4"
wp_set_post_terms($post["id"], 4, 'punktcat', false );

// Parse data into a custom field normally referred 
// FOLLOWINF FUNCTION IS UPDATING EVERY FIELD OF THE META BOX WITH THE 
VALUE OF '1'
update_post_meta( $post["id"], 'fredningszone_data', true );
}

echo '<div id="message" class="updated fade"><p>' . $i . ' posts have succesfully been imported from CSV!' . '</p></div>';

So the update_post_meta() function in the code snippet above does not provide the data of the zoneid into the custom field fredningszone_data[id].

The way I build my custom field and be seen within the following code snippet:

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>

I will appreciate, if anyone would have a suggestion on how I could parse the data into one of the fields of my custom metabox with multiple fields.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 对于squad数据集的基于bert模型的微调
    • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
    • ¥20 steam下载游戏占用内存
    • ¥15 CST保存项目时失败
    • ¥15 树莓派5怎么用camera module 3啊
    • ¥20 java在应用程序里获取不到扬声器设备
    • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
    • ¥15 Attention is all you need 的代码运行
    • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
    • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: