2

i am having one serious url issue.

there is news custom post type. it's slug is news.

News taxonomy slug: news_category

News taxonomy url is: http://domain.com/news_category/press-release

i want to change the url of the news_category to news news .

so url of news taxonomy would be:

             http://domain.com/news/press-release

i tried to rewrite the slug news_category to news but that's creating conflict issue. news shows news url but all the posts of news post type are returning 404 not found

News post's url:

     http://domain.com/news/POST_TITLE

as posts url also contain the news slug both conflicts. getting 404 error.

is it possible to fix it? how can i fix it?

any help would be greatly appreciated.

4
  • Of course it will not work because WordPress will always look for item "POST_TITLE" from custom post type news when it sees news in URL. And since WordPress will not find anything, WordPress will return 404 error.
    – Robert hue
    Commented Oct 1, 2014 at 8:25
  • can you please help me to get solution? how to fix it? Commented Oct 1, 2014 at 9:40
  • There is nothing to do. Both cannot work simultaneously. Either you can have category slug news or post type news. You should change slug for one.
    – Robert hue
    Commented Oct 1, 2014 at 9:46
  • is it possible to set taxonomy term before the custom post type's url. for expample: domain.com/news/press-release/post_title is it possible? Commented Oct 1, 2014 at 11:09

1 Answer 1

0

Try this code. It may help you.

 class Tax_Seo_Perma {

  public function __construct() {
    // Hooks
    add_filter('rewrite_rules_array',array($this, 'create_rewrite_rules'));
    add_filter('wp_loaded', array($this, 'flush_rules'));
    add_filter('post_link', array($this, 'modify_link_addresses'), 10, 3);
    //add_filter('post_type_link', 'tsp_write_link_addresses', 10, 3);
}

public function flush_rules(){
    global $wp_rewrite;
    $wp_rewrite->flush_rules(); 
}

public function create_rewrite_rules($rewrite) {
    global $wp_rewrite;

    // loop through custom taxonomies
    $args = array(
        'public'   => true,
        '_builtin' => false 
    );
    $output             = 'names'; // or objects
    $operator           = 'and'; // 'and' or 'or'
    $custom_taxonomies  = get_taxonomies($args, $output, $operator); 
    if ($custom_taxonomies) {
        foreach ($custom_taxonomies as $tax_name ) {
            $tax_token = '%'.$tax_name.'%';
            $wp_rewrite->add_rewrite_tag($tax_token, '(.+)', $tax_name.'=');
        }
    }

    // read current permalink structure and set the same structre
    $keywords_rewrite = $wp_rewrite->generate_rewrite_rules($wp_rewrite->root.$wp_rewrite->permalink_structure);
    return ( $rewrite + $keywords_rewrite );
}

public function modify_link_addresses($permalink, $post_id, $leavename)
{
    global $blog_id;
    global $wp_rewrite;
    // this is user's permalink structure set in options
    $permastruct = $wp_rewrite->permalink_structure;

    $args = array(
        'public'   => true,
        '_builtin' => false 
    );
    $output             = 'names'; // or objects
    $operator           = 'and'; // 'and' or 'or'
    $custom_taxonomies  = get_taxonomies($args, $output, $operator);

    if ($custom_taxonomies) {
        foreach ($custom_taxonomies as $tax_name ) {
            $tax_token = '%'.$tax_name.'%';

            $tax_terms = get_the_terms( $post->id, $tax_name );
            //var_dump($tax_terms);
            if ( !empty($tax_terms) )
            {
                foreach($tax_terms as $a_term)
                {
                    $long_slug = $a_term->slug;
                    if( false != (int)$a_term->parent ) { // a's parent p exists
                        $p_term = get_term( (int)$a_term->parent, $tax_name );//var_dump($p_term->slug);
                        $long_slug = ($p_term->slug) ? $p_term->slug .'+'. $long_slug : $long_slug;
                    } 
                    if( false != (int)$a_term->parent AND false != (int)$p_term->parent ) { // p's parent g exists
                        $g_term = get_term( (int)$p_term->parent, $tax_name );
                        $long_slug = ($g_term->slug) ? $g_term->slug .'+'. $long_slug : $long_slug;
                    }

                    $permalink = str_replace($tax_token, $long_slug, $permalink);
                    break;
                }
            } else {$permalink = str_replace($tax_token, 'no-'.$tax_name, $permalink); }
        }
    }

    return $permalink;
 }
 }
1
  • i will try this code and let you know it works or not ASAP. Thanks Commented Oct 3, 2014 at 8:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.