Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8350191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:14:26+00:00 2026-06-09T08:14:26+00:00

The code below works flawlessly, except it will only post the first image that

  • 0

The code below works flawlessly, except it will only post the first image that contains main_. I need it to pick up any column value that contains the words “main_’ thus giving me the ability to post multiple images. So if I have 3 or more main_img_url, the loop should know this and add them accordingly. I noted below where the image handler starts and ends.

Here is an example CSV:

post_title, post_content, main_img_url, main_img_url

Here is my code:

function app_csv_to_array($file = '', $delimiter = ',') {
    if(!file_exists($file) || !is_readable($file))
        return false;
    $header = NULL;
    $data = array();
    if(false !== $handle = fopen($file, 'r')) {
        while(false !== $row = fgetcsv($handle, 1000, $delimiter)) {

            if($header)
                $data[] = array_combine($header, $row);
            else
                $header = $row;
        }
        fclose($handle);
    }
    return $data;
}


    function process($file) {
        $rows = $this->app_csv_to_array($file);
        foreach($rows as $row) {
            $post['post_title'] = sanitize_text_field($row['post_title']);
            $post['post_content'] = sanitize_text_field($row['post_content']);
            $post['post_status'] = 'publish';
            $post['post_author'] = 658;

        $post_id = wp_insert_post($post);
        //////////////// I want the loop to start here   //////////////////
            foreach($row as $key => $val) {
              if(strstr($key, 'main_')) {
                $tmp = download_url( $file );
                preg_match('/[^\?]+\.(jpg|JPG|jpe|JPE|jpeg|JPEG|gif|GIF|png|PNG)/', $file, $matches);
                $filename = strtolower(basename($matches[0]));
                  } 
            }
//////////////// ends here   //////////////////
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:14:27+00:00Added an answer on June 9, 2026 at 8:14 am

    Your problem is inside app_csv_to_array method and specifically in the array_combine.

    Based on the documentation array_combine will create one associative array from two input arrays having the values from the first for its keys and the values from the second for its values

    As your keys are the headers of the csv file:

    post_title, post_content, main_img_url, main_img_url
    

    the resulting array that will eventually be returned will have only one slot for main_img_url as its key.

    On way to circumvent this problem is to make sure that your headers in the csv file are unique, allowing at the same time the strstr($key, 'main_') to match your columns content. Something like:

    post_title, post_content, main_img_url1, main_img_url2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below works great. I have a MySQL database that contains book titles
The code below works well but grabs only the UIView which is VISIBLE on
The code below works great except the email has all the text on one
This code below works in all web browsers except IE: <input type="text" name="passwordLogin" value="Password"
The code below works as intended, but it's not quite what I need. I
Everything in the code below works except for the binding on the ContextMenu. This
My code below works in Firefox to toggle a border on an image,but does
The code below works insomuch that I can successfully download the directory recursively. But,
The code below works (it will eventually pull records from a database via SQL),
The code below works for the class that I hard coded XCCustomers in my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.