Ready to get started?Download WordPress


[resolved] Importing + populating post_meta = trouble? (2 posts)

  1. dille
    Posted 8 years ago #

    I've run into a weird situation, and I have no clue about what's going wrong.

    For a customer, I'm importing information from a proprietary system into WordPress. The customers' system is multilingual (Dutch and English), and I'm using categories to preserve that distinction (I could use post_meta for that, I guess, but categories are slightly easier to filter output on). Since the proprietary database runs on the same server as the WordPress install, I fetch the info from the database, and loop through the results. On each iteration, I split the information in two parts (one for each language), and do two wp_insert_post()'s, after which I set the correct categories (using wp_set_post_cats() and the post ID returned by wp_insert_post()) and post_meta.

    Somehow, however, the post_ID's get mixed up. From what I've been able to gather, by echo()'ing stuff every few lines, the returned post_ID isn't always a value you'd expect. Sometimes, two iterations will be on the same post_ID (which means that I've lost one language), sometimes it will skip a few id's (so it might do 5, 6, 9, 7, 8), and most of the time it actually does what it's supposed to do. So in the end, I have a (WP) database that's mostly correct, some ID's aren't consecutive (which isn't a problem), and some information will be missing (which is one hell of a problem).

    What baffles me is that I can't figure out where things go wrong. I explicitly set the variable that the post_ID will be returned into to 0 on each iteration, so the doubles are returned by wp_insert_post(). I *am* pretty sure that all posts are INSERTed (not UPDATEd), so the ID returned SHOULD be the mysql_insert_id.

    I've also tried inserting a usleep() after each insert, but even if I let the script wait for half a second, the doubles are there. I think I can rule out issues with MySQL lagging behind (even IF it did, that shouldn't give me this weirdness, but I wanted to make sure.

    Looking at the code (thank you, Owen Winkler, for the XRef at redalt.com!) doesn't give me the impression that those ID's are cached somewhere, so right now I'm stuck.

    Am I missing something? Has anyone seen this behaviour? And most important: how the hell am I going to solve this?

  2. dille
    Posted 8 years ago #

    Well, actually, I was able to solve my own problem.

    As it turns out, the problem occurred in posts that contained apostrophes ('). I assumed that those would be escaped by the various filtering functions that the data is trown through, but I was wrong. That resulted in MySQL errors, and since these are pretty much suppressed within WP, no info to work with =]

    A quick $wpdb->escape() before I feed the data to wp_insert_post() and all is well.

    What irks me, though, is that this isn't captured by the filtering functions. I can post content with apostrophes in them, so where's the big difference when importing? Does this qualify as a bug?

Topic Closed

This topic has been closed to new replies.

About this Topic