1

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

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
    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
    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
    Aug 30, 2018 at 8:02

1 Answer 1

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);
            }
        }
    }

Your Answer

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

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