php - How to Upload CSV Data into Custom Post Type Data with Metabox programmatically

admin2025-01-07  6

I have a Custom Post Type called movies which had only title enabled from post support and 3 custom field of metabax as box office, year, and director like

now I have like 150 rows CSV formatted stored data whith same structure as

name, boxoffice,year,director

can you please let me know how I can load them into WP database to keep both Custom Post Type and metadat in the database using PHP

I have a Custom Post Type called movies which had only title enabled from post support and 3 custom field of metabax as box office, year, and director like

now I have like 150 rows CSV formatted stored data whith same structure as

name, boxoffice,year,director

can you please let me know how I can load them into WP database to keep both Custom Post Type and metadat in the database using PHP

Share Improve this question asked Aug 28, 2018 at 23:54 Mona CoderMona Coder 4062 gold badges6 silver badges20 bronze badges 3
  • 1 Is the idea that you want to be able to upload and process CSV files regularly, or allow others to do it, or just that you want to be handle one or a couple uploads? It may well be easier to "load them into the WP db" using WP functions acting directly on the data and producing posts and post meta (which reside in the database), than to upload the data into the the db and then extract it or re-extract it. – CK MacLeod Commented Aug 29, 2018 at 1:49
  • Hi, thanks for reply . This actually only one time loading data to the database and for first time( I am not getting any SQL data from existing database) – Mona Coder Commented Aug 29, 2018 at 18:00
  • 1 I was thinking about leaving the outlines of my own approach to this problem as an answer, but, looking for the quickest way to parse a CSV file, I ran into this post: sitepoint.com/… You could probably skip much of it and just get to the use of fgetcsv() and following code, but, depending on your level of coding knowledge, you might actually find it easier to install the entire thing even for one-time use. – CK MacLeod Commented Aug 30, 2018 at 8:02
Add a comment  | 

1 Answer 1

Reset to default 0
if(isset($_POST["submit"]))
    {
        $rows = array();
        if($_FILES['file']['name'])
        {
            $filename = explode(".", $_FILES['file']['name']);
            if($filename[1] == 'csv')
            {
                $handle = fopen($_FILES['file']['tmp_name'], "r");   
                $count=0;
                while(($data = fgetcsv($handle, 1000, ",")) !== FALSE)
                {
                     query_posts(array( 
                            'post_type' => 'custom-post-type',
                        ) ); 
                    while(have_posts()) : the_post();
                    $post_id->ID;
                    $rows[] = array(
                        'variable1' =>  $data[1] . "," . $data[2] . "," . $data[3] . "," . $data[4],
//above line for using multiple cells of csv file to a single variable
                        'variable2'   =>  $data[5],
                        'variable3'  => $data[6],
                        'variable4'                 => $data[7],
                        'variable5'                 => $data[8],
                        'variable6'         => $data[9]
                    );
                endwhile; 
                    $count++;
                }
                foreach($rows as $row => $rowValue  ){
                    add_row('rates', $rowValue, $post_id);  
                }   
                fclose($handle);
            }
        }
    }
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736265059a1016.html

最新回复(0)