If you’ll remember, WordPress updated its permalink structure back around version 3 or 3.1. Prior to this a lot of folks were using the /%category%/%postname%/ structure. It was really slow and caused an excess amount of queries in order to generate the pages, so WordPress began pushing for the /%postname%/ structure.
This morning I had to go in and make this change on a friend’s blog that I’ve done some work for and he quickly pointed out that all of his social sharing counters were gone. His 100+ Facebook likes and Twitter tweets had disappeared from each of his posts.
The problem was that the posts were looking at the urls with the category in them and one those urls were removed it though those new pages (new urls) had not been shared. There is an article out there that explained how to replace the url that was built with dates but I had to modify the code a bit in order to solve the issue for of replacing the /%category%/ section in the URL structure.
I thought I’d paste that code in case it helps anyone else.
// date we swapped the permalinks
$url_change_date = strtotime("9/12/2012");
// date $post->ID is published
$date_posted = strtotime(get_the_date());
// get page link
$sharing_url = get_permalink();
// if post published before permalinks were changed
if ($date_posted < $url_change_date):
$category = get_the_category($post->ID);
$category_slug = $category['0']->category_nicename;
// replace the new share URL with the old share URL IF created before permalinks were changed
$sharing_url = str_replace('://yourWebsiteUrl.com/', '://yourWebsiteUrl.com/' . $category_slug . '/', $sharing_url);
endif;
For implementation just be sure to replace where your URL is being displayed inside your Like/Tweet/Google+ code.
<p><a href="http://twitter.com/share" data-url="<?php echo $sharing_url; ?>" data-text="<?php the_title(); ?>" data-count="horizontal" data-via="yourTwitterName">Tweet</a>
suggested dosage by doctors fit patients, and 5mg of the elderly and malnourished people, as well as people with renal system failure. The drug is recommended to be taken