0

I have added the custom taxonamy for my faq site. I want to remove the default custom taxonamy showing in url for archive page. I have tried some codes, but it's break my pagination.

   function my_taxonomies_faqs() {
  $labels = array(
    'name'              => _x( 'FAQ Categories', 'taxonomy general name' ),
    'singular_name'     => _x( 'FAQ Category', 'taxonomy singular name' ),
    'search_items'      => __( 'Search FAQ Categories' ),
    'all_items'         => __( 'All FAQ Categories' ),
    'parent_item'       => __( 'Parent FAQ Category' ),
    'parent_item_colon' => __( 'Parent FAQ Category:' ),
    'edit_item'         => __( 'Edit FAQ Category' ), 
    'update_item'       => __( 'Update FAQ Category' ),
    'add_new_item'      => __( 'Add New FAQ Category' ),
    'new_item_name'     => __( 'New FAQ Category' ),
    'menu_name'         => __( 'FAQ Categories' ),
  );
  $args = array(
    'labels' => $labels,
    'public' => true,
    'hierarchical' => true,
    'show_in_rest' => true,
    'query_var' => true,
    'rewrite' => array('slug' => 'faq-categories', 'hierarchical' => true, 'with_front'=> false),    
  ); 
  register_taxonomy( 'faq-category', 'faq-post', $args );
}

Default URL Format:

www.example.com/faq/custom-tax/parent-cat/ 

I want to show like this below URL format:

 - www.example.com/faq - Homepage
 - www.example.com/faq/parent-cat/ - Level 1
 - www.example.com/faq/parent-cat/page/2 
 - www.example.com/faq/parent-cat/child-cat - level 2
 - www.example.com/faq/parent-cat/child-cat/page/2

Already i have tried this solution. But my child category pagination is shows the 404 Error. Link - https://rudrastyh.com/wordpress/remove-taxonomy-slug-from-urls.html

Any possible way to remove the default custom taxonamy in URL with pagination support?

0

One of the general problems after creating new custom post type or taxonomy is showing 404 page after accessing to new urls that are generated by your custom post type or taxonomy.

In many of cases, the problem is related to flush_rewrite_rules(). After creating new urls, you have to do flush rewrite rules to rewriting urls by WordPress. If you don't use it, you will get 404 when you access to them.

So we have several option to solve this:

1. Simple way:

You can simply go to setting -> permalinks section in admin dashboard and then (without change any settings) click save changes button. With clicking save changes, WordPress core automatically call flush_rewrite_rules() in its process. Now check your urls and they are working.

2. Professional way:

Based on best practices in WordPress, it is better that you define your custom taxonomy and post types inside plugins.

So the best place to flush_rewrite_rules() is when the plugin is activated.

Notice: Please notice that you only need it only the first time that you define it. It is mentioned also in WordPress developer documentation of register_post_type and also in this link:

ATTENTION: This is *only* done during plugin activation hook in this example!
 You should *NEVER EVER* do this on every page load!!

You can see the sample of this, in my plugin boilerplate in github.

Note: If you want only to check it, you can use it after registering your taxonomy (only one time) and then check urls (then you can remove it), but if you are creating plugin which is used by many people, you have to do it when your plugin is activated.

I hope your problem is related to this simple trick and this way solves you problem.

| improve this answer | |
  • Hi Mehdi, It's not working. Every time i have manually go and resave the settings while change the custom post slug changes. – Rajkumar S Mar 12 at 5:18
  • Dear Rajkumar, I see your code again. I think you have wrong setting for 'rewrite' option. Can you test it: 'rewrite' => array( 'slug' => 'faq' ) without anything extra. Because of being custom taxonomy, no need to remove anything. This is my full code for $args: ` 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'show_in_menu' => true, 'show_in_rest' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'faq' ), ` It works like your needs – Mehdi Soltani Mar 12 at 10:26
  • And also after changing $arg, for first reloading, please do flush_rewrite_rules. then check it again. Thanks – Mehdi Soltani Mar 12 at 10:33
  • My home page is - www.example.com/faq. So if i add like this 'rewrite' => array( 'slug' => 'faq' ). The URL pattern is modified to www.example.com/faq/faq/parent-cat/child-cat. I have tried with this flush_rewrite_rules it is not workout for my case. – Rajkumar S Mar 13 at 4:04
  • @RajkumarS - So the problem is related to filter hooks which you use to remove custom taxonomy. I think it is better to put them in your question and update your question again. Also where do you put your filters and call them? I appreciate if you give me extra information about it. – Mehdi Soltani Mar 13 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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