Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does WordPress add a duplicate numeric slug upon post/page view?

Tags:

php

wordpress

This is my first post here, so I apologize in advance for any mishaps.

I've been searching for hours trying to figure this one out, but I simply can't seem to understand why this is happening.

The site I'm setting up is a child site (not in a multisite sense, but as a separate site/domain with the same branding). Some of the posts on my site will originate from the parent/main site (but will be made as new posts through copy-paste), and I want the original article ID as part of the permalinks.

E.g. http://www.example.com/hello-world/12345/, where 12345 is the article ID of the article on the parent/main site.

To accomplish this, I've added a custom field to my posts where I can add the article ID of the original article with external_article_id as Field Name. I've then tried to manipulate the permalinks with 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 the permalink to the posts it appears exactly as I want it, both in the editor and on the site. However, when I either click a link or enter the address manually, I get redirected automatically to http://www.example.com/hello-world/12345/12345/. It duplicates the additional numerical slug, and also happens when I replace $custom[0] with a hard-coded numeric value. This applies to all posts, and my permalink structure (in the settings) is set to /%postname%/.

I even tried setting the permalink structure to /%postname%/%ext_article_id%/ and replace %ext_article_id% with $custom[0], but with the exact same outcome. I also tried using the same code on another WordPress site, except this time with pages instead of posts, also with the exact same outcome.

Ideally I would like to use something like add_query_arg($custom[0], '', get_permalink($post->ID));, but omit the question mark that comes along with it.

Could someone please explain to me why this is happening, and how I can circumvent this? Do I need to use some other filter, or how can I approach this?

Thank you in advance!

like image 442
René Avatar asked Oct 20 '22 12:10

René


1 Answers

In order to make this work you also need to make WordPress aware of the rewrite_tag and specify an additional permalink structure via add_permastruct. The following code should do the trick:

function append_custom_permalink( $post_link, $post ) {
    if( $post->post_type == 'post' ) {
        $custom = get_post_custom_values( 'external_article_id', $post->ID );
        if( ! empty($custom) ) {
            $post_link = $post_link.$custom[0].'/';
        }
    }
    return $post_link;
}
add_filter('post_link', 'append_custom_permalink', 10, 2);

function sof_30058470_posts_rewrite() {
    add_rewrite_tag('%external_article_id%', '([^/]+)', 'external_article_id=');
    add_permastruct('external_article_id', '/%year%/%monthnum%/%day%/%postname%/%external_article_id%/', false);
}
add_action('init', 'sof_30058470_posts_rewrite', 10, 0);

Make sure to re-save your permalink structure at Settings->Permalinks once you added the code. You may also need to refresh/clear your browser cache.

like image 164
Peter Avatar answered Dec 07 '22 12:12

Peter