WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] importer has problem with custom fields. (5 posts)

  1. vincej
    Member
    Posted 3 years ago #

    I built a handy dandy importer from a tab-delimited file into wordpress.
    It creates or updates records in wordpress.
    The logic is as follows.
    1. read in a text file, parse it into single records.
    2. check to see if there is an ID in input record. if so.
    a. update record in wordpress with new values.
    b. otherwise, create a new record with the new values.

    Works great, but I am getting duplicate result for the custom fields on Update procedure. two custom fields, price, and sku, involved.
    The result I get is two custom fields for "price" and two for "sku".

    the code is as follows.
    http://wordpress.pastebin.com/ZJdesLDE

    As you can see in both cases, update or create new post for WP, I check to see if the update post meta works first, if not it should add a custom field with value. somehow this is NOT working properly... am I missing a step?

    Any help appreciated. as I now have about 1000 records with duplicate "price" and "sku" fields with values. Critical to solve, as this updater would be used, for instance to modify records pricing, and present these modified records as NEW postings for our clients to view.

    ??
    cheers.

    Vince.

  2. Mark / t31os
    Moderator
    Posted 3 years ago #

    I think update_post_meta works like update_option, insomuch that if the data exists it does nothing, so rather then using add_post_meta here..

    if (!update_post_meta($my_post['ID'] , 'sku', $sku)) add_post_meta($my_post['ID'] , 'sku', $sku);

    Try..

    if (!update_post_meta($my_post['ID'] , 'sku', $sku)) update_post_meta($my_post['ID'] , 'sku', $sku);

    So the data is only entered if it doesn't exist already (because it'll return false if the entry exists, which triggers your add statement).

    Or even the following might work without the condition..

    update_post_meta($my_post['ID'] , 'sku', $sku);

    Info:
    http://codex.wordpress.org/Function_Reference/update_post_meta

  3. vincej
    Member
    Posted 3 years ago #

    Mark, thanks. I see what you mean... but it's MY logic that might be faulty.
    the docs seem to indicate the "update_post_meta" is an all in one.
    as per http://codex.wordpress.org/Function_Reference/update_post_meta

    do you mean that
    update_post_meta($my_post['ID'] , 'sku', $sku);
    will either update (field exists) or add the custom field (field does not yet exist) in all cases?

    At this time, i'm doing a delete_post_meta first, then add_post_meta to ensure the imports are working properly. At least that'll take care of the duplicates I've created. Would be nice to just use the update from here on in.
    Would the answer to the above be a YES, then?

    thanks.

  4. Mark / t31os
    Moderator
    Posted 3 years ago #

    I believe so yes. Update if exists, else create... :)

  5. vincej
    Member
    Posted 3 years ago #

    thanks muchly.Mark.

    oh, and sorry for the long code... am new here. only leant about the postbin thingy recently when reading another posting.

    cheers.
    Vince.

Topic Closed

This topic has been closed to new replies.

About this Topic