Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I use custom permalink structure:

/%tag%/%postname%/

and it is not working. I get url - http://mydomain.com/%tag%/post/. So there is no tag name, just %tag%. If i place %category% instead ot %tag%, everithing is ok.

Can somebody solve this problem?

share|improve this question
1  
Did you add a tag to that specific post? –  redconservatory Jan 14 '11 at 23:26
    
make sure you tageted that post or page and if that dosent help then i have know idea but why would you want that? in the codex codex.wordpress.org/Using_Permalinks they say clearly that"Starting Permalinks with %tag% is strongly not recommended for performance reasons" in bold. –  Bainternet Jan 15 '11 at 6:02
    
That Codex entry looks to be outdated. From what I can see in the source and on trac, a decision was made not to support %tag% in permalinks. I'll look around to make sure, and take that section out of the Codex if this is true. –  goldenapples Jan 18 '11 at 3:48
    
It's not a bug that 'tag' isn't supported. I believe it's intentional. Besides, %tag% isn't mentioned on this page: codex.wordpress.org/Using_Permalinks (obviously, because it's not supported). –  its_me Apr 6 '12 at 11:01

2 Answers 2

I got the following code to work in WP 3.0.1 and WP 3.1 RC 2, but with an important difference. Before 3.1, the tag structure is explicitly defined as %tag%, but in 3.1 this becomes %post_tag%. So my code handles both situations. It adds a hook to get_permalink() and inserts a tag if needed.

define( 'WPSE_7004_DEFAULT_TAG', 'untagged' );
if ( version_compare( $wp_version, '3.0.9' ) <= 0 ) {
    // This is pre-3.1
    define( 'WPSE_7004_TAG_STRUCT', '%tag%' );
} else {
    define( 'WPSE_7004_TAG_STRUCT', '%post_tag%' );
}

add_filter( 'post_link', 'wpse7004_post_link', 10, 3 );
function wpse7004_post_link( $permalink, $post, $leavename )
{
    if ( FALSE !== strpos( $permalink, WPSE_7004_TAG_STRUCT ) ) {
        $tags = get_the_tags( $post->ID );
        if ( $tags ) {
            usort( $tags, '_usort_terms_by_ID' );
            $tag = $tags[0]->slug;
        } else {
            // No tag. Use default tag name, otherwise rewrite rule matching goes wrong.
            $tag = WPSE_7004_DEFAULT_TAG;
        }
        $permalink = str_replace( WPSE_7004_TAG_STRUCT, $tag, $permalink );
    }

    return $permalink;
}

add_filter( 'request', 'wpse7004_request' );
function wpse7004_request( $query_vars )
{
    if ( array_key_exists( 'tag', $query_vars ) && WPSE_7004_DEFAULT_TAG == $query_vars['tag'] ) {
        unset( $query_vars['tag'] );
    }
    return $query_vars;
}
share|improve this answer

@Atari asked for a solution.. People who do not have one should not butt in!

I have the same issue. All other %category% %postname% %post_id% etc. resolve fine, but %tag% remains %tag% in the final permalink!

I think it is a bug. I came across a patch which was for an older version. I did not try it yet ( I do not know how to use it either :P ) because I am using the latest version (3.0.4 I think).. but the problem is still there!

I do not want to start the url with %tag% but I need it down the URL. The point is that it should work.. regardless of how I use it!

@Atari if you find a solution please do share it :)

here is the patch I found.. I would appreciate anyone telling me how to use it too :)


*** link-template.php   2009-12-14 05:09:55.000000000 -0500
--- link-template_patched.php   2010-06-21 14:29:24.000000000 -0400
***************
*** 88,93 ****
--- 88,94 ----
                $leavename? '' : '%postname%',
                '%post_id%',
                '%category%',
+         '%tag%',
                '%author%',
                $leavename? '' : '%pagename%',
        );
***************
*** 128,134 ****
--- 129,151 ----
                                $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
                        }
                }
+
+               if ( strpos($permalink, '%tag%') !== false ) {
+                       $tags = get_the_tags($post->ID);
+
+                       if ( $tags ) {
+                               usort($tags, '_usort_terms_by_ID'); // order by ID
+                               $tag = $tags[0]->slug;
+                       }

+                       // show default tag in permalinks, without
+                       // having to assign it explicitly
+                       if ( empty($tag) ) {
+                               $default_tag = get_tag( get_option( 'default_category' ) );
+                               $tag = is_wp_error( $default_tag ) ? '' : $default_tag->slug;
+                       }
+               }
+
                $author = '';
                if ( strpos($permalink, '%author%') !== false ) {
                        $authordata = get_userdata($post->post_author);
***************
*** 147,152 ****
--- 164,170 ----
                        $post->post_name,
                        $post->ID,
                        $category,
+             $tag,
                        $author,
                        $post->post_name,
                );
share|improve this answer
    
Yes it is a bug in WordPress that, IIRC, should be fixed in WordPress 3.1. –  mtekk Jan 17 '11 at 21:06
    
It won't be fixed in 3.1, from what I see. %tag% is not currently supported in the core rewrite structure. I can't find right now the ticket that that patch was proposed on, so I can't say why it was rejected. –  goldenapples Jan 18 '11 at 3:53
    
That patch is an SVN patch... if you want to test it out yourself on your own WordPress install (and you don't have an SVN client running), you can look in /wp-includes/link-template.php for the context around the lines marked with a + and add those lines (the line numbers surrounded by stars refer to the trunk version at the time the patch was proposed, but it should give you idea what part of the file to search). –  goldenapples Jan 18 '11 at 3:55
3  
And, at the risk of restating the obvious... hacking core is generally considered a bad idea if you're doing it for any reason other than testing out possible patches... You'll lose all your changes the next time you upgrade. This particular permalink problem can be solved in your functions.php file without hacking core (I believe? I won't try to answer it right now, though... rewrite rules are really confusing to me and there are people on here who understand them much better.) –  goldenapples Jan 18 '11 at 3:58

protected by toscho Sep 19 '12 at 12:08

Thank you for your interest in this question. Because it has attracted low-quality answers, posting an answer now requires 10 reputation on this site.

Would you like to answer one of these unanswered questions instead?