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.

/*
 * product custom post type added with new permalink
 */
function productposttype_with_custom_permalinks() {
register_post_type('product', array(
'label' => __('My product'),
 'singular_label' => __('product'),
 'show_ui' => true,
 'capability_type' => 'post',
 'hierarchical' => false,
 "supports" => array("title", "editor", "thumbnail", "author", "comments"),
 'taxonomies' => array('category', 'post_tag'), // this is IMPORTANT
'rewrite' => array('slug' => 'product'),
 'public' => true
    ));

//register_taxonomy( 'product-category', 'product', array ('hierarchical' => true, 'label' => __('product Categories'), 'rewrite' => array( 'slug' => 'product-category', 'with_front' => false ),));  // portfolio categories

add_rewrite_tag('%product%', '([^/]+)');
$extra_post_types = get_post_types(array('_builtin' => false, 'publicly_queryable' => true));
if (empty($extra_post_types))
return;
add_rewrite_tag('%post_type%', '(' . implode('|', $extra_post_types) . ')');
add_permastruct('product', '/%post_type%/%year%/%monthnum%/%day%/%product%/', true, 1);
}

/*
 * Funcation onload added the product custom post type
 */
add_action( 'init', 'productposttype_with_custom_permalinks' );

/*
 * product custom post added the permalink hook for enable the custom permalink
 * product custom post type
 */
function productposttype_with_product_permalink( $link, $post, $leavename, $sample ){
  if( 'product' != $post->post_type )
    return $link;
  $rewritecode = array(
    '%year%',
    '%monthnum%',
    '%day%',
    '%hour%',
    '%minute%',
    '%second%',
    $leavename? '' : '%postname%',
    '%post_id%',
    '%post_type%',
    $leavename? '' : '%pagename%',
    $leavename? '' : '%product%',
  );
  $unixtime = strtotime($post->post_date);
  $date = explode(' ', date('Y m d H i s', $unixtime));
  $replace_array = array(
    $date[0],
    $date[1],
    $date[2],
    $date[3],
    $date[4],
    $date[5],
    $post->post_name,
    $post->ID,
    $post->post_type,
    $post->post_name,
    $post->post_name,
  );
  $path = str_replace($rewritecode, $replace_array, $link);
  return $path;
}

add_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.