0
votes

The current permalink structure for the posts that I am trying to modify includes a date, and it is configured in the settings page like: "/%year%/%monthnum%/%day%/%category%/%postname%/"

An example post url: http://example.com/2016/09/11/category/child-category/long-post-slug-is-printed-here/

My problem is that I can't figure out how to modify this permalink structure for posts that are tagged with a specific tag, so for example, if I have a tag "permalink-struc-2", and I add it to my post, I would like to then rewrite the permalink to: http://example.com/category/child-category/long-post-slug-is-printed-here/ The date should be stripped from the post permalink.

I've tried using a wp filter "post_link"

add_filter('post_link', array($this, 'change_permalink_date_structure'), 1, 3);

But what ends up happening is that the url is changed, but when I try accessing the post from the shortened url, it is still redirected to the original one - > http://example.com/2016/09/11/category/child-category/long-post-slug-is-printed-here/

Could anyone help on this issue please?

Thanks

2
can you please tell me what actually want to achive?Vasim Shaikh
I want to change the permalink structure for posts tagged with a specific tagdulesaga
what is you current Url??Vasim Shaikh
I posted the URLs in the description of the question, I can't post the real url's because the project is confidential.dulesaga

2 Answers

0
votes

You can try to use htaccess mod rewrite. You will have to add one rule for each url. For example:

 RewriteRule /category/child-category/long-post-slug-is-printed-here/ /2016/09/11/category/child-category/long-post-slug-is-printed-here/ [L]
0
votes

Thanks to everyone that tried to help on this, I managed to find a way to do what I needed. Basically the I wrote a rewrite rule for wp with add_rewrite_rule() It works for post url's that don't have the date:

  $rewrite_rule_regex = '^(?!([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/)(.+?)/([^/]+)(?:/([0-9]+))?/?$';

add_rewrite_rule($rewrite_rule_regex, 'index.php?category_name=$matches[4]&name=$matches[5]&page=$matches[6]&istaggedcalendar=1', 'bottom');

Also, this new rewrite rule had to have a higher priority than the last (universal) wp rewrite rule (it didn't work if my rewrite rule was last, and if it was set to "top" some other rewrite rules didn't work):

add_filter( 'rewrite_rules_array', array($this, 'rewrite_rulles_array_look'));

// * set the priority of our rewrite rule to not interfere with other pages

function rewrite_rules_array_look($r_array) {

  $rew_rule = $r_array[$this->rewrite_rule_regex];

  unset($r_array[$this->rewrite_rule_regex]);

  $res = array_slice($r_array, 0, count($r_array)-1, true) +

      array($this->rewrite_rule_regex => $rew_rule) +

      array_slice($r_array, count($r_array)-1, count($r_array) , true) ;


  return $res;

}