WordPress.org

Ready to get started?Download WordPress

Forums

CSV Importer
Making custom changes (3 posts)

  1. alexleonard
    Member
    Posted 3 years ago #

    Hi there,

    I've been finding this plugin extremely useful the last while but wanted to make a customisation and I'm not entirely sure how to go about it.

    I need to prevent duplicate posts from being imported.

    In my CSV I will always have a unique ID as one of the custom fields. I wanted to check this unique ID against all posts currently in the WordPress database.

    I'm fairly sure for the PHP code I'll just need to extract the CSV custom field ID, and then create an array of the matching custom field value of all current posts, then I can check if there's a match using in_array. If it matches then I want to skip to the next line in the CSV and repeat that check.

    I'm guessing it would go somewhere in here:

    foreach ($csv->connect() as $csv_data) {
    	if ($post_id = $this->create_post($csv_data, $options)) {
    		$imported++;
    		$comments += $this->add_comments($post_id, $csv_data);
    		$this->create_custom_fields($post_id, $csv_data);
    	} else {
    		$skipped++;
    	}
    }

    but am not entirely sure. Can you give any pointers on how I might achieve this?

    Cheers,
    Alex

  2. dvkob
    Member
    Plugin Author

    Posted 3 years ago #

    alexleonard: Your approach sounds good. You can put that logic either in the loop you posted or inside the create_post() method. query_posts() function will probably be of help.

  3. alexleonard
    Member
    Posted 3 years ago #

    Hey dvkob,

    Thanks for the quick response. Good to know my thinking is heading in the right direction.

    Although it's looking like I may actually, due to some quite strange requirements, have to allow duplicates to be imported, but not display them in my loops if two separate required unique fields are not met. Quite confusing but I'm sure I'll get there in the end!

    Thanks again,
    Alex

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic