search-extras.php 3.52 KB
<?php



add_action( 'add_attachment', 'add_attachment_mime_type' );
function add_attachment_mime_type( $attachment_id ){
   $post =  get_post( $attachment_id );
   $post_mime_type = explode(".", $post->_wp_attached_file);
   update_post_meta( $attachment_id, 'file_type', end($post_mime_type) );

}

   function wporg_custom_post_type() {
      register_post_type('documents',
         array(
            'labels'      => array(
               'name'          => __( 'Documents', 'textdomain' ),
               'singular_name' => __( 'Documents', 'textdomain' ),
            ),
            'public'      => true,
            'has_archive' => true,
            'rewrite'     => array( 'slug' => 'documents' ), // my custom slug
         )
      );
   }
   //add_action('init', 'wporg_custom_post_type');

function wptp_add_categories_to_attachments() {
	register_taxonomy_for_object_type( 'category', 'attachment' );
	// register_taxonomy_for_object_type( 'continent', 'attachment' );
	// register_taxonomy_for_object_type( 'country', 'attachment' );
	// register_taxonomy_for_object_type( 'region', 'attachment' );
	
}
//add_action( 'init' , 'wptp_add_categories_to_attachments' );

// apply tags to attachments 
function wptp_add_tags_to_attachments() {
	register_taxonomy_for_object_type( 'post_tag', 'attachment' );
}
add_action( 'init' , 'wptp_add_tags_to_attachments' );

// register new taxonomy which applies to attachments 
function wptp_add_taxonomy() {
	$labels = array(
		'name'              => 'Continent',
		'singular_name'     => 'Continent',
		'search_items'      => 'Search Continent',
		'all_items'         => 'All Continent',
		'parent_item'       => 'Parent Continent',
		'parent_item_colon' => 'Parent Continent:',
		'edit_item'         => 'Edit Continent',
		'update_item'       => 'Update Continent',
		'add_new_item'      => 'Add New Continent',
		'new_item_name'     => 'New Continent Name',
		'menu_name'         => 'Continent',
	);
	$args = array(
		'labels' => $labels,
		'hierarchical' => true,
		'query_var' => 'true',
		'rewrite' => 'true',
		'show_admin_column' => 'true',
	);
	register_taxonomy( 'continent',[ 'attachment','documents'], $args );


   $labels = array(
		'name'              => 'Region',
		'singular_name'     => 'Region',
		'search_items'      => 'Search Region',
		'all_items'         => 'All Region',
		'parent_item'       => 'Parent Region',
		'parent_item_colon' => 'Parent Region:',
		'edit_item'         => 'Edit Region',
		'update_item'       => 'Update Region',
		'add_new_item'      => 'Add New Region',
		'new_item_name'     => 'New Region Name',
		'menu_name'         => 'Region',
	);
	$args = array(
		'labels' => $labels,
		'hierarchical' => true,
		'query_var' => 'true',
		'rewrite' => 'true',
		'show_admin_column' => 'true',
	);
	
	register_taxonomy( 'region',[ 'attachment','documents'], $args );

   $labels = array(
		'name'              => 'Country',
		'singular_name'     => 'Country',
		'search_items'      => 'Search Country',
		'all_items'         => 'All Country',
		'parent_item'       => 'Parent Country',
		'parent_item_colon' => 'Parent Country:',
		'edit_item'         => 'Edit Country',
		'update_item'       => 'Update Country',
		'add_new_item'      => 'Add New Country',
		'new_item_name'     => 'New Country Name',
		'menu_name'         => 'Country',
	);
	$args = array(
		'labels' => $labels,
		'hierarchical' => true,
		'query_var' => 'true',
		'rewrite' => 'true',
		'show_admin_column' => 'true',
	);
	register_taxonomy( 'country',[ 'attachment','documents'], $args );

}
//add_action( 'init', 'wptp_add_taxonomy' , 0 );