register CPT + register taxonomy + custom permalink

PHOTO EMBED

Tue May 25 2021 09:27:30 GMT+0000 (UTC)

Saved by @Sikor

<?php
function support_init() {
  $labels = array(
    'name'          => _x('Support', 'post type general name', 'engine'),
    'menu_name'     => _x('Support', 'admin menu', 'engine'),
  );

  $args = array(
    'labels'             => $labels,
    'public'             => true, //false for disable single page and permalink
    'publicly_queryable' => true, //false for disable single page and permalink
    'has_archive'        => false, // true for create archive page
    'show_ui'            => true,
    'show_in_menu'       => true,
    'query_var'          => true,
    'rewrite'            => array('slug' => 'support', 'with_front'=> false),
    //'with_front'=> false for ignore wordpress permalink settings in post url (if we want diffrent permalink structure than regular blog)
    'capability_type'    => 'post', // page, post
    'hierarchical'       => false,
    'menu_position'      => 5,
    'menu_icon'          => 'dashicons-admin-tools',
    'supports'           => array('title', 'editor', 'thumbnail', 'author', 'revisions' 'excerpt', 'comments'),
  );
  register_post_type('support', $args);
}
add_action('init', 'support_init');


/* REGISTER TAXONOMY */
function support_taxonomies() {
	$labels = array(
		'name' => _x('Categories', '', 'engine'),
		'singular_name' => _x('Categories', '', 'engine'),
		'menu_name' => __('Categories', 'engine'),
	);
	$args = array(
		'hierarchical' => true,
		'labels' => $labels,
		'show_ui' => true,
		'show_admin_column' => true,
		'query_var' => true,
		'rewrite' => array('slug' => 'support_cat', 'with_front'=> false),
      	//'with_front'=> false for ignore wordpress permalink pettings in category url
	);
	register_taxonomy('support_cat', 'support', $args);
}
add_action('init', 'support_taxonomies', 0);


/* OPTIONAL: CUSTOM TAXONOMY SLUG URL */
// show category name for CPT in single post url

function support_taxonomy_slug_link( $post_link, $id = 0 ){
    $post = get_post($id);
    if ( is_object( $post ) ){
        $terms = wp_get_object_terms( $post->ID, 'support_cat' );
        if( $terms ){
            return str_replace( '%support_cat%' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;
}
add_filter( 'post_type_link', 'support_taxonomy_slug_link', 1, 3 );

// then change rewrite in function support_init() for this and will show /support/category_name/post_name
'rewrite' => array('slug' => 'support/%support_cat%'),
content_copyCOPY