Support » Fixing WordPress » length of post name in permalinks

  • Hi.

    I’m in the process of trying to migrate from MT (on TypePad) to a WordPress installation (WP 1.5) .

    The one problem I still have left, trying to make the old permalinks still working, is that TypePad shortened the name of individual posts, while WP puts the entire title in the URL.

    So what I need is either a way to completly convert WP to a similar method of trimmed names (which is good since final URLs are shorter, but they’re also not as elegant, and I expect this would require more work), or a way to allow finding posts coming from the old URLs based on the first part of the name (which is what I’ve been trying to do so far, without success).

    The concept seems simple enough. When searching for the post in WP the code will need to know it’s a special case of legacy URL (which will always be reached anyway through an .htaccess redirect, so I should be able to add a parameter to the query string), and change the database query to select a post based on year, month, and the first 15 characters of the name (which is unique in my case).

    But I can’t seem to find the right places in WP PHP code to do that. After playing with url_to_postid, before the DB query (which I eventually realized doesn’t happen always, so is the wrong place) I so far came to the conclusion that the right place is the WP-Query->get_posts method. But I have no PHP/Apache experience, and can’t quite figure out how to check for URL query string parameters (I’ve add a special parameter to the URL through .htaccess redirect, but the $q variables doesn’t seem to hold it, despite the fact that as far as I see it’s supposed to hold everything). If I hard code an existing name into the $q[‘name’] variable before the SQL query, it works, but checking for my new parameters, or even simple checks for the length of the $q[‘name’] parameter, never does anything.

    I tried to both redirect and just rewrite the URL explicitly, but none of these passed my ‘legacy’ parameter

    RewriteRule ^([0-9]{4})/([0-9]{1,2})/([0-9a-zA-z\-]{1,15})\.html$ /blog/$1/$2/$3/?legacy=true [r=permanent,L]

    #RewriteRule ^([0-9]{4})/([0-9]{1,2})/([0-9a-zA-z\-]{1,15})\.html$ /blog/index.php?&legacy=true&year=$1&monthnum=$2&name=$3 [QSA,L]

    Though both passed everything else properly.

    in the get_posts method, if I do
    $q[‘name’]= “made-up-post-name-that-exists”;
    just for checking, it works, but if I try to put a changed DB query, when building the WHERE clause, inside
    if (” != $q[‘legacy’]){}
    it never gets called. Actually, even something like checking the strlen() on the $q[‘name’] parameter doesn’t trigger the if condition, when I know I send larger URLs than the condition I send for.

    So if anyone can tell me what am I missing, or if there’s a simpler way and I’m just barking up the wrong tree, I’d be grateful.

    Plus, though that’s a different problem, which I should be able to sort out once I actually get to it, would it work later to just inside the SQL WHERE clause check for SUBSTRING(post_name,1,15) instead of post_name, or should I replace the = with a LIKE command instead?

    Again, thank you very for any assistance you could provide, and I hope that I didn’t provide too much info on what I’ve been trying so far…

Viewing 8 replies - 1 through 8 (of 8 total)
  • You could always type in the post slug yourself, and that will be what WP will use to generate the permalink. If you leave it blank, it defaults to the entire title (or the post ID if there is no title).

    I’ll do that if I can’t find an automated way to do it, but the thought of manually checking and changing names on 200 posts doesn’t exactly thrill me. I’d prefer to spend even twice the time on something that would do that automatically…

    But yes, it does so far seem as the only option I can get to work, so I may just have to do it. Thanks.

    OK, Thanks.
    It finally hit me I don’t need to manually change the posts, but can just run an SQL to change the post_name field on the wp_posts table to include only the first 15 chars of the imported posts.
    So this ends this problem for me.

    What is the URL of your blog?

    do you want a link like this:
    or this:

    The second link is better for you for the search engines.

    I want to use links with titles, like your second one. This is what I used before, and I see no reason to downgrade to less…

    In any case, I finally did a replace SQL like I mentioned in my previous post in this thread. So now all the older imported posts are accessible with the shorter title links, which is about what I wanted.

    Thank you.

    How did you replace the SQL of your old database?

    Hi, sorry for the late response, forgot to check back here again after everything sorted out, not very polite of me.

    Maybe I didn’t phrase myself clearly. I didn’t mean that I changed the SQL I’m using to pull the posts, but that I used an UPDATE statement to go over all the imported posts, and change the post name to the first 15 chars.
    Something like (Not the actual statement I used, I use here 200 as a close place holder for the last imported post, and I didn’t test this exact one, but it should be similar except for maybe some typos)

    UPDATE wp_posts SET post_name=SUBSTRING(post_name,1,15) WHERE id<200

    And of course if someone uses this in a case where the import isn’t the first thing done with the blog, then the limits in the WHERE clause have to be either ID from both sides, or date based, or something else that will fit better. In this case, I didn’t have any previously inserted posts, so that was easy.

    Like I said, the only reason I didn’t do it to begin with is that when the idea of changing the post slug came up, I thought of the work of doing it manually…

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘length of post name in permalinks’ is closed to new replies.