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

enter image description here

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

  • 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

1 Answer 1

        $rows = array();
            $filename = explode(".", $_FILES['file']['name']);
            if($filename[1] == 'csv')
                $handle = fopen($_FILES['file']['tmp_name'], "r");   
                while(($data = fgetcsv($handle, 1000, ",")) !== FALSE)
                            'post_type' => 'custom-post-type',
                        ) ); 
                    while(have_posts()) : the_post();
                    $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]
                foreach($rows as $row => $rowValue  ){
                    add_row('rates', $rowValue, $post_id);  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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