Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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:

[category/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?category_name=$matches[1]&feed=$matches[2]
[category/(.+?)/(feed|rdf|rss|rss2|atom)/?$] => index.php?category_name=$matches[1]&feed=$matches[2]
[category/(.+?)/page/?([0-9]{1,})/?$] => index.php?category_name=$matches[1]&paged=$matches[2]
[category/(.+?)/?$] => index.php?category_name=$matches[1]
[tag/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$] => index.php?tag=$matches[1]&feed=$matches[2]
[tag/([^/]+)/(feed|rdf|rss|rss2|atom)/?$] => index.php?tag=$matches[1]&feed=$matches[2]

...etc

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

share|improve this question
2  
What is your permalink structure? –  John P Bloch Apr 29 '11 at 1:34
    
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? –  kovshenin Apr 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... –  marpa Apr 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 –  Otto Jun 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. –  goldenapples Aug 19 '11 at 22:20

2 Answers 2

Hard to say what your issue is without seeing more code, but I would bet that your issue will be resolved if you add this after inserting your post:

do_action('save_post', $post_ID, $post);

WordPress uses this action to flush cache, rewrite rules, etc. Make sure to use the appropriate variables for $post_ID and $post, of course. For good measure, you should also add:

do_action('wp_insert_post', $post_ID, $post);

Just so any plugins, themes, etc. which use this hook will know to do whatever it is they do.

Cheers~

share|improve this answer
    
wp_insert_post calls those actions itself. –  WraithKenny Aug 24 '12 at 22:02

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.

share|improve this answer
    
Basically, say away from calling extra stuff after wp_insert_post. –  WraithKenny Aug 24 '12 at 22:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.