%tag% in permalink not working

I use custom permalink structure:

/%tag%/%postname%/

Read More

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?

Related posts

Leave a Reply

2 comments

  1. @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 😛 ) 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,
                    );
    
  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;
    }