This is my first post here, so I apologize in advance for any failures.
I was looking for a watch, trying to figure it out, but I just can't figure out why this is happening.
The site I am setting up is a sub site (not in a multi-user sense, but as a separate site / domain with the same branding). Some of the posts on my site will come from the parent / main site (but will be made as new posts via copy-paste), and I want the original article ID to be part of the permalinks.
eg. http://www.example.com/hello-world/12345/ , where 12345 is the article ID of the article on the parent / main site.
To do this, I added a custom field to my posts, where I can add the article id of the source article with external_article_id as the field name. Then I tried to manipulate permalinks using the following code:
add_filter('post_link', 'append_custom_permalink', 10, 2); function append_custom_permalink($url, $post) { $newurl = $url; if ($post->post_type == 'post') { $custom = get_post_custom_values('external_article_id', $post->ID); if (!empty($custom)) $newurl = $url . $custom[0] . '/'; } return $newurl; }
Whenever I output a permalink to messages, it appears exactly as I want, both in the editor and on the site. However, when I either click the link or enter the address manually, I am automatically redirected to http://www.example.com/hello-world/12345/12345/ . It duplicates an extra numeric bundle, and also happens when I replace $custom[0] with a hard-coded numeric value. This applies to all messages, and my constant structure (in the settings) is set to /%postname%/ .
I even tried setting the permalink structure to /%postname%/%ext_article_id%/ and replacing %ext_article_id% with $custom[0] , but with the same result. I also tried using the same code on another WordPress site, except this time with pages instead of posts, and also with the same result.
Ideally, I would like to use something like add_query_arg($custom[0], '', get_permalink($post->ID)); but omit the question mark that comes with it.
Can someone explain to me why this is happening, and how can I get around this? Do I need to use any other filter or how can I approach this?
Thank you in advance!