This can be done in a theme or a plugin, but makes more sense as a plugin. This is a re-usable piece of functionality and so should not be locked into the theme.
*** it should be noted that the taxonomy filter in the back-end will not work correctly if the items are set to “private”
settings => permalinks. This is a very straight-forward plugin and should not need any attention over time, many developers would put this code straight into the theme, but I think that is wrong.
Version: 1.75
Author URI: https://plasterdog.com
*/
add_action( 'init', 'custom_post_type', 0 );
// Register Custom Post Type
function custom_post_type() {
$labels = array(
'name' => _x( 'Examples', 'Post Type General Name', 'text_domain' ),
'singular_name' => _x( 'Example', 'Post Type Singular Name', 'text_domain' ),
'menu_name' => __( 'Examples', 'text_domain' ),
'name_admin_bar' => __( 'Examples', 'text_domain' ),
'archives' => __( 'Example Archives', 'text_domain' ),
'attributes' => __( 'Example Attributes', 'text_domain' ),
'parent_item_colon' => __( 'Parent Item:', 'text_domain' ),
'all_items' => __( 'All Examples', 'text_domain' ),
'add_new_item' => __( 'Add New Example', 'text_domain' ),
'add_new' => __( 'Add New Example', 'text_domain' ),
'new_item' => __( 'New Example', 'text_domain' ),
'edit_item' => __( 'Edit Example', 'text_domain' ),
'update_item' => __( 'Update Example', 'text_domain' ),
'view_item' => __( 'View Example', 'text_domain' ),
'view_items' => __( 'View Examples', 'text_domain' ),
'search_items' => __( 'Search Examples', 'text_domain' ),
'not_found' => __( 'Not found', 'text_domain' ),
'not_found_in_trash' => __( 'Not found in Trash', 'text_domain' ),
'featured_image' => __( 'Featured Image', 'text_domain' ),
'set_featured_image' => __( 'Set featured image', 'text_domain' ),
'remove_featured_image' => __( 'Remove featured image', 'text_domain' ),
'use_featured_image' => __( 'Use as featured image', 'text_domain' ),
'insert_into_item' => __( 'Insert into example', 'text_domain' ),
'uploaded_to_this_item' => __( 'Uploaded to this item', 'text_domain' ),
'items_list' => __( 'Example list', 'text_domain' ),
'items_list_navigation' => __( 'Example list navigation', 'text_domain' ),
'filter_items_list' => __( 'Filter items list', 'text_domain' ),
);
$args = array(
'label' => __( 'Example', 'text_domain' ),
'description' => __( 'Example Description', 'text_domain' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', ),
'taxonomies' => array( 'jobtype' ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_admin_bar' => true,
'show_in_nav_menus' => true,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
);
register_post_type( 'example', $args );
}
// Register Custom Taxonomy
function example_taxonomy() {
$labels = array(
'name' => _x( 'jobtypes', 'Taxonomy General Name', 'text_domain' ),
'singular_name' => _x( 'jobtype', 'Taxonomy Singular Name', 'text_domain' ),
'menu_name' => __( 'Jobtype', 'text_domain' ),
'all_items' => __( 'All Jobtype', 'text_domain' ),
'parent_item' => __( 'Parent Jobtype', 'text_domain' ),
'parent_item_colon' => __( 'Parent Jobtype', 'text_domain' ),
'new_item_name' => __( 'New Jobtype Name', 'text_domain' ),
'add_new_item' => __( 'Add NewJobtype', 'text_domain' ),
'edit_item' => __( 'EditIJobtype', 'text_domain' ),
'update_item' => __( 'Update Jobtype', 'text_domain' ),
'view_item' => __( 'View Jobtype', 'text_domain' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'text_domain' ),
'add_or_remove_items' => __( 'Add or remove items', 'text_domain' ),
'choose_from_most_used' => __( 'Choose from the most used', 'text_domain' ),
'popular_items' => __( 'Popular Items', 'text_domain' ),
'search_items' => __( 'Search Jobtype', 'text_domain' ),
'not_found' => __( 'Not Found', 'text_domain' ),
'no_terms' => __( 'No items', 'text_domain' ),
'items_list' => __( 'Jobtype list', 'text_domain' ),
'items_list_navigation' => __( 'Jobtype list navigation', 'text_domain' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
);
register_taxonomy( 'jobtype', array( 'example' ), $args );
}
add_action( 'init', 'example_taxonomy', 0 );
//adds the taxonomy filter to the back-end display | example code taken frome here: http://thestizmedia.com/custom-post-type-filter-admin-custom-taxonomy/
add_action('restrict_manage_posts', 'tsm_filter_post_type_by_taxonomy');
function tsm_filter_post_type_by_taxonomy() {
global $typenow;
$post_type = 'example'; // change to your post type
$taxonomy = 'jobtype'; // change to your taxonomy
if ($typenow == $post_type) {
$selected = isset($_GET[$taxonomy]) ? $_GET[$taxonomy] : '';
$info_taxonomy = get_taxonomy($taxonomy);
wp_dropdown_categories(array(
'show_option_all' => __("Show All {$info_taxonomy->label}"),
'taxonomy' => $taxonomy,
'name' => $taxonomy,
'orderby' => 'name',
'selected' => $selected,
'show_count' => true,
'hide_empty' => true,
));
};
}
/** Filter posts by taxonomy in admin */
add_filter('parse_query', 'tsm_convert_id_to_term_in_query');
function tsm_convert_id_to_term_in_query($query) {
global $pagenow;
$post_type = 'example'; // change to your post type
$taxonomy = 'jobtype'; // change to your taxonomy
$q_vars = &$query->query_vars;
if ( $pagenow == 'edit.php' && isset($q_vars['post_type']) && $q_vars['post_type'] == $post_type && isset($q_vars[$taxonomy]) && is_numeric($q_vars[$taxonomy]) && $q_vars[$taxonomy] != 0 ) {
$term = get_term_by('id', $q_vars[$taxonomy], $taxonomy);
$q_vars[$taxonomy] = $term->slug;
}
}

