Я успешно создал функцию, которая способна импортировать данные из CSV в сообщения пользовательского типа сообщения.Я могу разобрать данные в собственные поля заголовка и описания, но функция не может разобрать данные в мета-поле с несколькими полями.
Вот фрагмент кода функции, которая выполняет синтаксический анализ данных CSV в postfields:
// 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>';
Таким образом, функция update_post_meta () в приведенном выше фрагменте кода не предоставляет данные zoneid в настраиваемое поле fredningszone_data [id].
Способ построения моегонастраиваемое поле и будет отображаться в следующем фрагменте кода:
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>
Буду признателен, если у кого-нибудь будет предложение о том, как мне разобрать данные в одном из полей моего настраиваемого метабокса с помощьюнесколько полей.