$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>url rewriting - Custom Post Type rewrite redirects to homepage|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

url rewriting - Custom Post Type rewrite redirects to homepage

matteradmin7PV0评论

I have a custom post type registered called employees. When registering this custom post type I used:

...
'rewrite' => array('slug' => 'people'),
...

Now, when I hover over a link to one of my employees I see the url as this:

www.example/people/john

So that seems correct. But I click the link, I see Chrome navigate to just www.example/john and then that doesn't exist so it redirects to www.example (the homepage).

How do I make it navigate to /people/john and stop it from redirecting to the homepage?

I have a custom post type registered called employees. When registering this custom post type I used:

...
'rewrite' => array('slug' => 'people'),
...

Now, when I hover over a link to one of my employees I see the url as this:

www.example/people/john

So that seems correct. But I click the link, I see Chrome navigate to just www.example/john and then that doesn't exist so it redirects to www.example (the homepage).

How do I make it navigate to /people/john and stop it from redirecting to the homepage?

Share Improve this question asked Aug 23, 2016 at 21:09 Jake WilsonJake Wilson 2832 gold badges4 silver badges11 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 14

The most important args for rewrite redirects when registering new post type are:

  • 'public' => true,
  • 'publicly_queryable' => true,
  • 'query_var' => true,

I have pasted some code below which I have tested and it working fine for the url : www.example/people/john

$labels_employee = 
array('name' => _x( 'Employees', 'Post typegeneral name', 'textdomain' ),
'singular_name' => _x( 'Employee', 'Post type singular name', 'textdomain' ),
'menu_name' => _x( 'Employees', 'Admin Menu text', 'textdomain' ),
'name_admin_bar' => _x( 'Employee', 'Add New on Toolbar', 'textdomain' ),
'add_new' => __( 'Add New', 'textdomain' ),
'add_new_item' => __( 'Add New Employee', 'textdomain' ),
'new_item' => __( 'New Employee', 'textdomain' ),

$args_employee= array( 'labels' => $labels_employee, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'query_var' => true, 'rewrite' => array( 'slug' => 'people' ), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => null, 'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' ), );

register_post_type( 'employee', $args_employee );

if you have register your taxo with register_post_type('employees',$args);

have you create single-employees.php ?

and after in administration / Setting / Permalinks -> Save Changes

I had the same issue, custom taxonomy link was redirecting to index.

Ok, when register a custom taxonomy, make sure that the register_taxonomy code runs always (init hook) and not only on admin (admin_init)

add_action('init', 'woobr_register_settings');
function woobr_register_settings()
{
    $args = array(
        //'labels' => $labels,
        //'labels' => 'Brand',
        'public' => true,
        'publicly_queryable' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'query_var' => true,
        'capability_type' => 'product',
        'has_archive' => true,
        'hierarchical' => false,
        'menu_position' => null,
        'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'),

    );

    register_taxonomy('woobrand', 'product', $args); 
}
Post a comment

comment list (0)

  1. No comments so far