WordPress.org

Support

Support » Plugins and Hacks » Hacks » [Resolved] importer has problem with custom fields.

[Resolved] importer has problem with custom fields.

  • 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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • 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

    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.

    I believe so yes. Update if exists, else create… 🙂

    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.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] importer has problem with custom fields.’ is closed to new replies.
Skip to toolbar