1

While parsing data from a CVS file into a custom post type, i would like to parse data into the fields of a meta box, that I created.

The meta box fields are normally updated like this:

  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>

..but when i parse data from the CSV via another funtion, that I build, I cannot access the fields of the meta box. See code below:

Following code is a snippet from the import plugin, that I am trying to build, where the CSV data won't parse to fields of meta box. See below:

$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>';

How can I parse data into field of custom meta box?

0

Okay. I solved the problem myself. I was not populating the the array of a meta_key the correct way.

Below, you will find a working solution:

$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"]);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.