php - Unable to update the meta boxes with multiple fields

admin2025-06-04  2

I successfully build a function, that is able to import data from a CSV into posts of a Custom Post Type (CPT). I am able to parse data into the native Title and Description fields, but it fails, when I 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 
    // THIS IS UNSUCCESSFUL
    update_post_meta($post["id"], $fredningszone_data['id'], $post["zoneid"]);
}

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.

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>

<?php }

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748984170a315383.html

最新回复(0)