I'm writing a plugin which uses wp_insert_post() to insert posts programatically and my permalink setting is /%category%/%postname%/

Of all the post inserted, wp_posts.guid shows 2 types of output. Some post will be following /%category%/%postname%/ and some /?p=POST_ID. When I use get_permalink(), I also get mixed results much like what's stored in wp_posts.guid

I wonder what causes this and how can I force all the posts to follow my permalink configuration?

At the same time, how do I force all link of /p=POST_ID to redirect according to the permalink setting, which in this case /%category%/%postname%/?

Thank you

EDIT: I later found out that those with default permalink are actually 'draft' posts. Sorry for the confusion.

  • guide is for feed and has nothing to do with permalinks. – Mamaduka Apr 3 '12 at 15:32

Permalinks in WP do not actually have anything to do with wp_posts.guid any more. I believe that historically they did, and have been left in because of RSS feeds - I.e. changing them would cause peoples feeds to think that the posts had not been read.

Your permalink structure is stored within your wp_options table (add echo '<pre>'; print_r($wp_rewrite); echo '</pre>'; to your code to see the rules).

get_permalink() should be used within The Loop, unless you are parsing a page ID to it (get_permalink(15)), so that may be causing the issue.

May I also suggest that you reset your permalinks to default, save them, then set them to 'Day and name', save them, and see if that works. The reason for this is to flush your rules, and then create some correct rules. I suggest 'Day and name' as there is an issue in the current version on WP (3.3.1) where setting permalinks to 'Postname' do not correctly create the rules.

If the above gives you working rules then try your custom structure and it should work.

| improve this answer | |
  • Thanks for the tips. I did pass id to get_permalink(). What get_permalink(ID) returns is exactly what's in wp_posts.guid, that's why I thought it's related. – faulty Apr 4 '12 at 1:59
  • I get the same result after following your steps, then I later found your that those with default permalinks are actually draft posts. Sorry for the confusion. – faulty Apr 4 '12 at 2:14
  • 1
    No problem, glad you found the problem. – David Gard Apr 11 '12 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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