Skip to content

Instantly share code, notes, and snippets.

@deepak-rajpal
Created September 4, 2015 12:08
Show Gist options
  • Save deepak-rajpal/7a170727aa99966b2a24 to your computer and use it in GitHub Desktop.
Save deepak-rajpal/7a170727aa99966b2a24 to your computer and use it in GitHub Desktop.
Regsiter Custom Post Types - Taxonomies - Add Taxonomies Column (with Dropdown Sorting) in admin to sort data
<?php
/* Starts: Register Custom Post Type for tab management */
function tab_type_post() {
$labels = array(
'name' => _x( 'Tab-Posts', 'post type general name' ),
'singular_name' => _x( 'Tab-Post', 'post type singular name' ),
'add_new' => _x( 'Add New', 'book' ),
'add_new_item' => __( 'Add New Tab-Post' ),
'edit_item' => __( 'Edit Tab-Post' ),
'new_item' => __( 'New Tab-Post' ),
'all_items' => __( 'All Tab-Posts' ),
'view_item' => __( 'View Tab-Post' ),
'search_items' => __( 'Search Tab-Posts' ),
'not_found' => __( 'No Tab-Posts found' ),
'not_found_in_trash' => __( 'No Tab-Posts found in the Trash' ),
'parent_item_colon' => '',
'menu_name' => 'Tab-Posts'
);
$args = array(
'labels' => $labels,
'description' => 'Holds our Tab-Posts and Tab-Post specific data',
'public' => true,
'menu_position' => 5,
'publicly_queryable' => true,
'query_var' => true,
// 'rewrite' => array('slug' => 'tabs'),
'supports' => array( 'title', 'editor', 'thumbnail' , 'custom-fields' , 'post-formats' ),
'has_archive' => true,
);
register_post_type( 'tab_posts', $args );
}
add_action( 'init', 'tab_type_post' );
// hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_tab_taxonomies', 0 );
/* ====Create two taxonomies, Tab Types and writers for the post type "book======= " */
function create_tab_taxonomies() {
// Add new taxonomy, make it hierarchical (like categories)
$labels = array(
'name' => _x( 'Tab Types', 'taxonomy general name' ),
'singular_name' => _x( 'Tab Type', 'taxonomy singular name' ),
'search_items' => __( 'Search Tab Types' ),
'all_items' => __( 'All Tab Types' ),
'parent_item' => __( 'Parent Tab Type' ),
'parent_item_colon' => __( 'Parent Tab Type:' ),
'edit_item' => __( 'Edit Tab Type' ),
'update_item' => __( 'Update Tab Type' ),
'add_new_item' => __( 'Add New Tab Type' ),
'new_item_name' => __( 'New Tab Type' ),
'menu_name' => __( 'Tab Type' ),
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true
// 'rewrite' => array( 'slug' => 'Tab Type' ),
);
register_taxonomy( 'tab_type', array( 'tab_posts' ), $args );
}
/* =====Add Tab type column to identify in admin which tab_posts related to which tab_types in list==== */
add_action("manage_posts_custom_column", "show_destinations_column");
add_filter("manage_edit-tab_posts_columns", "tab_columns");
function tab_columns($columns){
$columns = array(
"cb" => "<input type=\"checkbox\" />",
"title" => "Title",
"tab_type" => "Tab Type",
'date' => 'Date',
);
return $columns;
}
function show_destinations_column($column){
global $post;
switch ($column) {
case "tab_type":
$term_list = get_the_term_list($post->ID,'tab_type','',', ','');
echo strip_tags($term_list); // Dont want to show links
break;
}
}
/* Add a tab_type drop down in Tab list in admin as well. Easy to select all tabs under that tab category */
function restrict_tabs_by_types() {
global $typenow;
$post_type = 'tab_posts'; // change HERE
$taxonomy = 'tab_type'; // change HERE
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,
));
};
}
add_action('restrict_manage_posts', 'restrict_tabs_by_types');
function convert_id_to_term_in_query($query) {
global $pagenow;
$post_type = 'tab_posts'; // change HERE
$taxonomy = 'tab_type'; // change HERE
$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;
}
}
add_filter('parse_query', 'convert_id_to_term_in_query');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment