How to create wordpress custom post type permalink structure

create wordpress custom post type permalink structure

WordPress tutorial for, How to create wordpress custom post type permalink structure. Here we creating the Product post type with permalink.  we given code.

How to create wordpress custom post type permalink structure

Please open the functions.php file and put following code in that file.

01/*
02 * product custom post type added with new permalink
03 */
04function productposttype_with_custom_permalinks() {
05register_post_type('product', array(
06'label' => __('My product'),
07 'singular_label' => __('product'),
08 'show_ui' => true,
09 'capability_type' => 'post',
10 'hierarchical' => false,
11 "supports" => array("title", "editor", "thumbnail", "author", "comments"),
12 'taxonomies' => array('category', 'post_tag'), // this is IMPORTANT
13'rewrite' => array('slug' => 'product'),
14 'public' => true
15    ));
16 
17//register_taxonomy( 'product-category', 'product', array ('hierarchical' => true, 'label' => __('product Categories'), 'rewrite' => array( 'slug' => 'product-category', 'with_front' => false ),));  // portfolio categories
18 
19add_rewrite_tag('%product%', '([^/]+)');
20$extra_post_types = get_post_types(array('_builtin' => false, 'publicly_queryable' => true));
21if (empty($extra_post_types))
22return;
23add_rewrite_tag('%post_type%', '(' . implode('|', $extra_post_types) . ')');
24add_permastruct('product', '/%post_type%/%year%/%monthnum%/%day%/%product%/', true, 1);
25}
26 
27/*
28 * Funcation onload added the product custom post type
29 */
30add_action( 'init', 'productposttype_with_custom_permalinks' );
31 
32/*
33 * product custom post added the permalink hook for enable the custom permalink
34 * product custom post type
35 */
36function productposttype_with_product_permalink( $link, $post, $leavename, $sample ){
37  if( 'product' != $post->post_type )
38    return $link;
39  $rewritecode = array(
40    '%year%',
41    '%monthnum%',
42    '%day%',
43    '%hour%',
44    '%minute%',
45    '%second%',
46    $leavename? '' : '%postname%',
47    '%post_id%',
48    '%post_type%',
49    $leavename? '' : '%pagename%',
50    $leavename? '' : '%product%',
51  );
52  $unixtime = strtotime($post->post_date);
53  $date = explode(' ', date('Y m d H i s', $unixtime));
54  $replace_array = array(
55    $date[0],
56    $date[1],
57    $date[2],
58    $date[3],
59    $date[4],
60    $date[5],
61    $post->post_name,
62    $post->ID,
63    $post->post_type,
64    $post->post_name,
65    $post->post_name,
66  );
67  $path = str_replace($rewritecode, $replace_array, $link);
68  return $path;
69}
70 
71add_action( 'post_type_link', 'productposttype_with_product_permalink', 10, 4 );

If you have any issues or problem with permalink then please write to me.

How to create wordpress custom post type permalink structure
How to create wordpress custom post type permalink structure

Published by

Purab

I am Purab from India, Software development is my profession and teaching is my passion. Programmers blog dedicated to the JAVA, Python, PHP, DevOps and Opensource Frameworks. Purab's Github Repo Youtube Chanel Video Tutorials Connect to on LinkedIn

One thought on “create wordpress custom post type permalink structure”

Leave a Reply

Your email address will not be published.