<?php

/**
 * Start custom post types
 * ----------------------------------------------------------------------------
 */
 
 // Register Custom Post Type
function custom_post_type_resource_arcHIVE() {

	$labels = array(
		'name'                => _x( 'All resources', 'Post Type General Name', 'text_domain' ),
		'singular_name'       => _x( 'Resource', 'Post Type Singular Name', 'text_domain' ),
		'menu_name'           => __( 'Resources', 'text_domain' ),
		'parent_item_colon'   => __( 'Parent resources:', 'text_domain' ),
	  'all_items'           => __( 'All the resources', 'text_domain' ),
		'view_item'           => __( 'Show the resource', 'text_domain' ),
		'add_new_item'        => __( 'Add a new resource', 'text_domain' ),
		'add_new'             => __( 'Add new resources', 'text_domain' ),
		'edit_item'           => __( 'Edit the resource', 'text_domain' ),
		'update_item'         => __( 'Save the resource', 'text_domain' ),
		'search_items'        => __( 'Search the resource', 'text_domain' ),
		'not_found'           => __( 'We couldn\'t find any resource ', 'text_domain' ),
		'not_found_in_trash'  => __( 'We couldn\'t find any resource in the bin', 'text_domain' ),
	);
	$rewrite = array(
		'slug'                       => 'resources/%type_resource_arcHIVE%',
		'with_front'                 => true,
		'hierarchical'               => true,
	);
	$args = array(
		'label'               => __( 'resource_arcHIVE', 'text_domain' ),
		'description'         => __( 'Resources arcHIVE', 'text_domain' ),
		'labels'              => $labels,
		'supports'            => array( 'title', 'editor', 'thumbnail', 'trackbacks', 'revisions', 'custom-fields', 'page-attributes' ),
		'taxonomies'          => array('post_tag','type_resource_arcHIVE'),
		'hierarchical'        => true,
		'public'              => true,
		'show_ui'             => true,
		'show_in_menu'        => true,
		'show_in_nav_menus'   => true,
		'show_in_admin_bar'   => true,
		'menu_position'       => 15,
		'menu_icon'           => 'dashicons-book',
		'can_export'          => true,
		'has_archive'         => 'resources',
		'exclude_from_search' => false,
		'publicly_queryable'  => true,
		'capability_type'     => 'post',
        	'rewrite'             => $rewrite,
	);
	register_post_type( 'resource_arcHIVE', $args );

}

// Hook into the 'init' action
add_action( 'init', 'custom_post_type_resource_arcHIVE', 0 );

/**
 * Start custom taxonomies
 * ----------------------------------------------------------------------------
 */
 
 // Register Custom Taxonomy
function custom_taxonomy_type_resource_arcHIVE() {

	$labels = array(
		'name'                       => _x( 'Type of resources arcHIVE', 'Taxonomy General Name', 'text_domain' ),
		'singular_name'              => _x( 'Type of resource arcHIVE', 'Taxonomy Singular Name', 'text_domain' ),
		'menu_name'                  => __( 'Type resources arcHIVE', 'text_domain' ),
		'all_items'                  => __( 'All type resources arcHIVE', 'text_domain' ),
		'parent_item'                => __( 'Parent type resource arcHIVE', 'text_domain' ),
		'parent_item_colon'          => __( 'Parent type resource arcHIVE:', 'text_domain' ),
		'new_item_name'              => __( 'New type resource arcHIVE', 'text_domain' ),
		'add_new_item'               => __( 'Add New type resource arcHIVE', 'text_domain' ),
		'edit_item'                  => __( 'Edit type resource arcHIVE', 'text_domain' ),
		'update_item'                => __( 'Update type of resource', 'text_domain' ),
		'separate_items_with_commas' => __( 'Separate type resource arcHIVE with commas', 'text_domain' ),
		'search_items'               => __( 'Search type of resource', 'text_domain' ),
		'add_or_remove_items'        => __( 'Add or remove type of resource arcHIVE', 'text_domain' ),
		'choose_from_most_used'      => __( 'Choose from the most used type of resource arcHIVE', 'text_domain' ),
		'not_found'                  => __( 'Type of resource arcHIVE Not Found', 'text_domain' ),
	);
  $rewrite = array(
		'slug'                       => 'resources',
	);
	$args = array(
		'labels'                     => $labels,
		'hierarchical'               => true,
		'public'                     => true,
		'show_ui'                    => true,
		'show_admin_column'          => true,
		'show_in_nav_menus'          => true,
		'show_tagcloud'              => true,
        'rewrite'                    => $rewrite,
	);
	register_taxonomy( 'type_resource_arcHIVE', array( 'resource_arcHIVE' ), $args );
}

// Hook into the 'init' action
add_action( 'init', 'custom_taxonomy_type_resource_arcHIVE', 0 );

// changing the permalink
// http://wordpress.stackexchange.com/questions/108642/permalinks-custom-post-type-custom-taxonomy-post

function arcHIVE_show_permalinks( $post_link, $id = 0 ){
    $post = get_post($id);
    if ( is_object( $post ) && $post->post_type == 'resource_arcHIVE' ){
        $terms = wp_get_object_terms( $post->ID, 'type_resource_arcHIVE' );
        if( $terms ){
            return str_replace( '%type_resource_arcHIVE%' , $terms[0]->slug , $post_link );
        }
    }
    return $post_link;
}
add_filter( 'post_type_link', 'arcHIVE_show_permalinks', 1, 2 );

?>