I am building a website for a client with an existing website and I need to set the URLs to be exactly like they are in his old website.
I have created a custom post type with a custom taxonomy for this website.
This is the code for registering the taxonomy:
// Args for the phone-category taxonomy
$labels = array(
'name' => _x( 'Categories', 'Taxonomy General Name', 'ps_dev' ),
'singular_name' => _x( 'Category', 'Taxonomy Singular Name', 'ps_dev' ),
'search_items' => __( 'Search categories', 'ps_dev' ),
'all_items' => __( 'All categories', 'ps_dev' ),
'parent_item' => __( 'Parent category', 'ps_dev' ),
'parent_item_colon' => __( 'Parent category:', 'ps_dev' ),
'edit_item' => __( 'Edit category', 'ps_dev' ),
'update_item' => __( 'Update category', 'ps_dev' ),
'add_new_item' => __( 'Add new category', 'ps_dev' ),
'new_item_name' => __( 'New category name', 'ps_dev' ),
'menu_name' => __( 'Category', 'ps_dev' )
);
// Register the taxonomy
register_taxonomy( 'phone-category', array( 'phones' ), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_in_rest' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => '/phone', 'with_front' => false )
) );
And this is the code for registering the new custom post type:
// Create the phones CPT
$labels = array(
'name' => _x( 'Phones', 'Post Type General Name', 'ps_dev' ),
'singular_name' => _x( 'Phone', 'Post type Singular Name', 'ps_dev' ),
'menu_name' => __( 'Phones', 'ps_dev' ),
'parent_item_colon' => __( 'Parent Phone', 'ps_dev' ),
'all_items' => __( 'All Phones', 'ps_dev' ),
'view_item' => __( 'View Phone', 'ps_dev' ),
'add_new_item' => __( 'Add New Phone', 'ps_dev' ),
'add_new' => __( 'Add New', 'ps_dev' ),
'edit_item' => __( 'Edit Phone', 'ps_dev' ),
'update_item' => __( 'Update Phone', 'ps_dev' ),
'search_items' => __( 'Search Phone', 'ps_dev' ),
'not_found' => __( 'Not Found', 'ps_dev' ),
'not_found_in_trash' => __( 'Not found in Trash', 'ps_dev' ),
);
$args = array(
'label' => __( 'phones', 'ps_dev' ),
'description' => __( 'Phone numbers' , 'ps_dev' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions', 'custom-fields' ),
'taxonomies' => array( 'phones-category' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_ui_menu' => true,
'show_in_nav_menu' => true,
'show_in_admin_bar' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_seach' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => true,
'rewrite' => array( 'slug' => '/phone/%post_name%/', 'with_front' => false )
);
register_post_type( 'phones', $args );
As can be seen in the code above, the URL structure I need for the CPT is /phone/%post_name%/
and the URL structure I need for the taxonomy is /phone/%taxonomy_title%/
So for example, if there is a phone
item called test
, its URL should be /phone/test/
.
If we have a phone-category
taxonomy called taxonomy_test
, its URL should be /phone/taxonomy_test
.
The taxonomy URL works well. The issue is with the CPT's URL.
I am trying to use this code:
add_filter( 'post_type_link', 'phone_custom_permalink' );
function phone_custom_permalink( $post_link, $post ) {
if ( $post->post_type != 'phones' ) {
return $post_link;
}
return str_replace('/%post_name%/', $post->post_name, $post_link );
}
But when I open the phones
post's edit page, it throws an error:
phone_custom_permalink(), 1 passed in D:\Programs\Xampp\htdocs\theindex\wp-includes\class-wp-hook.php on line 309 and exactly 2 expected in D:\Programs\Xampp\htdocs\theindex\wp-content\themes\hello-elementor-child\post-types\phones.php:90 Stack trace: #0 D:\Programs\Xampp\htdocs\theindex\wp-includes\class-wp-hook.php(309): phone_custom_permalink('http://theindex...') #1 D:\Programs\Xampp\htdocs\theindex\wp-includes\plugin.php(189): WP_Hook->apply_filters('http://theindex...', Array) #2 D:\Programs\Xampp\htdocs\theindex\wp-includes\link-template.php(370): apply_filters('post_type_link', 'http://theindex...', Object(WP_Post), false, false) #3 D:\Programs\Xampp\htdocs\theindex\wp-includes\link-template.php(201): get_post_permalink(Object(WP_Post), false, false) #4 D:\Programs\Xampp\htdocs\theindex\wp-includes\rest-api\endpoints\class-wp-rest-posts-controller.php(1776): get_permalink(Object(WP_Post)) #5 D:\Programs\Xampp\htdocs\theindex\wp-includes\rest-a in D:\Programs\Xampp\htdocs\theindex\wp-content\themes\hello-elementor-child\post-types\phones.php on line 90
I tried to follow some guides but in each one, I am still getting this error. When I change it to this:
add_filter( 'post_type_link', 'phone_custom_permalink' );
function phone_custom_permalink( $post_link, $post=0 ) { // The change is here
if ( $post->post_type != 'phones' ) {
return $post_link;
}
return str_replace('/%post_name%/', $post->post_name, $post_link );
}
I am not getting the error, but it just does not work (the URL in the post edit page is with /&post_name%/
.
What am I doing wrong?
I am building a website for a client with an existing website and I need to set the URLs to be exactly like they are in his old website.
I have created a custom post type with a custom taxonomy for this website.
This is the code for registering the taxonomy:
// Args for the phone-category taxonomy
$labels = array(
'name' => _x( 'Categories', 'Taxonomy General Name', 'ps_dev' ),
'singular_name' => _x( 'Category', 'Taxonomy Singular Name', 'ps_dev' ),
'search_items' => __( 'Search categories', 'ps_dev' ),
'all_items' => __( 'All categories', 'ps_dev' ),
'parent_item' => __( 'Parent category', 'ps_dev' ),
'parent_item_colon' => __( 'Parent category:', 'ps_dev' ),
'edit_item' => __( 'Edit category', 'ps_dev' ),
'update_item' => __( 'Update category', 'ps_dev' ),
'add_new_item' => __( 'Add new category', 'ps_dev' ),
'new_item_name' => __( 'New category name', 'ps_dev' ),
'menu_name' => __( 'Category', 'ps_dev' )
);
// Register the taxonomy
register_taxonomy( 'phone-category', array( 'phones' ), array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_in_rest' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => '/phone', 'with_front' => false )
) );
And this is the code for registering the new custom post type:
// Create the phones CPT
$labels = array(
'name' => _x( 'Phones', 'Post Type General Name', 'ps_dev' ),
'singular_name' => _x( 'Phone', 'Post type Singular Name', 'ps_dev' ),
'menu_name' => __( 'Phones', 'ps_dev' ),
'parent_item_colon' => __( 'Parent Phone', 'ps_dev' ),
'all_items' => __( 'All Phones', 'ps_dev' ),
'view_item' => __( 'View Phone', 'ps_dev' ),
'add_new_item' => __( 'Add New Phone', 'ps_dev' ),
'add_new' => __( 'Add New', 'ps_dev' ),
'edit_item' => __( 'Edit Phone', 'ps_dev' ),
'update_item' => __( 'Update Phone', 'ps_dev' ),
'search_items' => __( 'Search Phone', 'ps_dev' ),
'not_found' => __( 'Not Found', 'ps_dev' ),
'not_found_in_trash' => __( 'Not found in Trash', 'ps_dev' ),
);
$args = array(
'label' => __( 'phones', 'ps_dev' ),
'description' => __( 'Phone numbers' , 'ps_dev' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'revisions', 'custom-fields' ),
'taxonomies' => array( 'phones-category' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_ui_menu' => true,
'show_in_nav_menu' => true,
'show_in_admin_bar' => true,
'can_export' => true,
'has_archive' => false,
'exclude_from_seach' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => true,
'rewrite' => array( 'slug' => '/phone/%post_name%/', 'with_front' => false )
);
register_post_type( 'phones', $args );
As can be seen in the code above, the URL structure I need for the CPT is /phone/%post_name%/
and the URL structure I need for the taxonomy is /phone/%taxonomy_title%/
So for example, if there is a phone
item called test
, its URL should be /phone/test/
.
If we have a phone-category
taxonomy called taxonomy_test
, its URL should be /phone/taxonomy_test
.
The taxonomy URL works well. The issue is with the CPT's URL.
I am trying to use this code:
add_filter( 'post_type_link', 'phone_custom_permalink' );
function phone_custom_permalink( $post_link, $post ) {
if ( $post->post_type != 'phones' ) {
return $post_link;
}
return str_replace('/%post_name%/', $post->post_name, $post_link );
}
But when I open the phones
post's edit page, it throws an error:
phone_custom_permalink(), 1 passed in D:\Programs\Xampp\htdocs\theindex\wp-includes\class-wp-hook.php on line 309 and exactly 2 expected in D:\Programs\Xampp\htdocs\theindex\wp-content\themes\hello-elementor-child\post-types\phones.php:90 Stack trace: #0 D:\Programs\Xampp\htdocs\theindex\wp-includes\class-wp-hook.php(309): phone_custom_permalink('http://theindex...') #1 D:\Programs\Xampp\htdocs\theindex\wp-includes\plugin.php(189): WP_Hook->apply_filters('http://theindex...', Array) #2 D:\Programs\Xampp\htdocs\theindex\wp-includes\link-template.php(370): apply_filters('post_type_link', 'http://theindex...', Object(WP_Post), false, false) #3 D:\Programs\Xampp\htdocs\theindex\wp-includes\link-template.php(201): get_post_permalink(Object(WP_Post), false, false) #4 D:\Programs\Xampp\htdocs\theindex\wp-includes\rest-api\endpoints\class-wp-rest-posts-controller.php(1776): get_permalink(Object(WP_Post)) #5 D:\Programs\Xampp\htdocs\theindex\wp-includes\rest-a in D:\Programs\Xampp\htdocs\theindex\wp-content\themes\hello-elementor-child\post-types\phones.php on line 90
I tried to follow some guides but in each one, I am still getting this error. When I change it to this:
add_filter( 'post_type_link', 'phone_custom_permalink' );
function phone_custom_permalink( $post_link, $post=0 ) { // The change is here
if ( $post->post_type != 'phones' ) {
return $post_link;
}
return str_replace('/%post_name%/', $post->post_name, $post_link );
}
I am not getting the error, but it just does not work (the URL in the post edit page is with /&post_name%/
.
What am I doing wrong?
Share Improve this question asked Feb 15, 2022 at 7:59 OmerOmer 132 bronze badges 01 Answer
Reset to default 1I think you just have to do :
add_filter( 'post_type_link', 'phone_custom_permalink', 10, 2 );
To tell the hook that you are passing it 2 arguments.