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 developing a new website, but I can't quite figure out a solution to my permalinks problem.

My main website is http://w3spine.com, I'm sending posts to my second WP installation at http://w3lol.com where I want posts permalinks to be consistent numbers like w3lol.com/50, w3lol.com/51, w3lol.com/52, etc.

I cannot use post ID since there are a whole bunch of other stuff that are stored in wp_posts and the IDs skip some numbers (ex. if given post has id 50 in the best case scenario the next one is with id 54 (50 is the post id, 51 and 52 are revisions put there by default and 53 is the next post's attached image), not to mention that if I happen to do a revision on some post, or add a page or anything that number will increase.

It is not a problem to use the post slug as permalink, but the regular users will find it difficult/impossible to keep track of their previous sessions, plus I was hoping to get that short URL look.

I'm willing to accept any kind of solution that will achieve the effect I'm going for.

PS. By using the slug as permalink (%postname%) I mean that every post has it's own name and I cannot change it to 50, 51, 52, etc. which is obvious solution that on the other hand will create another problem.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

I don't really have the time or will to write and debug the code to make this work, but I can point you down a path to how you might make this work. A word of warning: it's certainly not simple.

  1. You have to save the current number somewhere so you can retrieve and increment it every time a post is added, I would do this in an option.

  2. You have to associate the number to each post via a custom field so they can be queried. This would be done via the save_post action hook. Prefix the custom field key with an underscore to hide it from the admin UI.

  3. You have to insert this number into the post permalink when WordPress generates it, this is done via the post_link filter (sadly there is no codex example, but you can find some examples on this site, I've written a few myself).

  4. When incoming requests are received, you need a rewrite rule to convert it into query vars. I would add a custom query var that you set in your rewrite rule so when the query is parsed you can look for this query var and rebuild the query (example of adding a query var or add_rewrite_tag). You may need to set a dummy post id or something to get past the parse_request and parse_query stages.

  5. When the actual SQL query is generated, you have to change the query to load the post based on the value of the custom field matching your custom query var, you can do this on the pre_get_posts action. See WP_Query for info on setting query vars for a custom field query.

share|improve this answer
    
I managed to do it! First off, it was pointed out to me that if I leave posts with numeric permalinks they will eventually conflict with years and other slugs. My solution was to edit the Social Network Auto Poster plugin's WP option to send slugs as well (it is not set by default). I added an entry in wp_options that holds the number of posts sent via this plugin WP option, which if the post is successfully transfered is incremented by 1. Now the posts slugs are build like this: i1, i2, i3, etc. That first part of your answer really gave me a push in the right direction :) –  Сър Георги Демирев Jan 12 '13 at 2:29

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.