I'm using wp_insert_post to programmatically add content to a site. This works fine with posts, categories and tags, but not with pages.

When I attempt to add a page (i.e. post with post_type=page), the rewrite rules do not get properly regenerated (or something happens to them after wp_insert_post). Specifically, rules related to category and tag urls get lost, as listed below:

This means that tag and category archive urls get broken. Just clicking on Settings > Permalinks will repair them. Have tried to fix by using $wp_rewrite->flush_rules() immediately after wp_insert_post but this has not fixed the problem...

Do you do any rules rewriting manually with WP_Rewrite or are you using WordPress' standard set of rules? Does the same happen when you publish a page manually via the admin interface?
–
kovsheninApr 29 '11 at 11:46

Am not doing any manual rewriting of rules nor do I want this code to be dependent on a particular permalink structure, needs to work for any permalink settings. This does NOT happen when publishing pages manually via the Add Page admin UI...
–
marpaApr 30 '11 at 3:46

1

Your permalink structure does matter, as does the number of pages you have on the site. If you are using a bad permalink structure, then verbose_page_rules can be activated. And if that's the case, and you have a lot of pages, then the script might timeout or break in some other fashion when trying to generate the new permalink rules. This would only happen with inserting Pages too, because rewrite rules only get regenerated that way with Pages and verbose_page_rules enabled. See this post: ottopress.com/2010/category-in-permalinks-considered-harmful
–
OttoJun 10 '11 at 16:44

1

Did you make sure you call global $wp_rewrite before attempting to flush rules? Flushing rules should do the trick; its what's done on the Settings -> Permalinks pages. For some reason your code isn't actually flushing.
–
goldenapplesAug 19 '11 at 22:20

I've encountered the same issue I think. I was using wp_insert_post in an ajax callback. The post_name field was getting blanked out. I eventually traced it back to calling wp_publish_post shortly there after. I commented out the publish and set post_status prior to insert and it all worked better.