Skip to content

Instantly share code, notes, and snippets.

@chwnam
Created June 10, 2015 16:39
Show Gist options
  • Save chwnam/d5eff9bbc2bb2a1787a8 to your computer and use it in GitHub Desktop.
Save chwnam/d5eff9bbc2bb2a1787a8 to your computer and use it in GitHub Desktop.
커스텀 포스트의 카테고리 필터에 대한 기록
<?php
/*
Plugin Name: Taxonomy Dropdown
Plugin URI: http://blog.changwoo.pe.kr
Description: 커스텀 포스트의 카테고리 필터 드롭다운 상자에 대해
Version: 1.0
Author: changwoo
Author URI: http://blog.changwoo.pe.kr
*/
add_action( 'init', 'taxdd_register_post_type' );
function taxdd_register_post_type() {
register_taxonomy( 'collection-type', 'collection', array(
'label' => 'Collection Types',
'hierarchical' => TRUE,
) );
register_post_type( 'collection', array(
'label' => 'collections',
'public' => TRUE,
'taxonomies' => array( 'collection-type' )
) );
}
register_activation_hook( __FILE__, 'taxdd_on_activated' );
function taxdd_on_activated() {
taxdd_register_post_type();
$book = wp_insert_term( 'Book', 'collection-type', array( 'slug' => 'book' ) );
$cd = wp_insert_term( 'CD', 'collection-type', array( 'slug' => 'cd' ) );
$stamp = wp_insert_term( 'Stamp', 'collection-type', array( 'slug' => 'stamp' ) );
$book_id = wp_insert_post( array(
'post_content' => 'It is a book.',
'post_name' => 'taxdd_book',
'post_title' => 'A book collection',
'post_status' => 'publish',
'post_type' => 'collection',
) );
$cd_id = wp_insert_post( array(
'post_content' => 'It is a cd.',
'post_name' => 'taxdd_cd',
'post_title' => 'A cd collection',
'post_status' => 'publish',
'post_type' => 'collection',
) );
$stamp_id = wp_insert_post( array(
'post_content' => 'It is a stamp.',
'post_name' => 'taxdd_stamp',
'post_title' => 'A stamp collection',
'post_status' => 'publish',
'post_type' => 'collection',
) );
wp_set_post_terms( $book_id, array( $book['term_id'] ), 'collection-type' );
wp_set_post_terms( $cd_id, array( $cd['term_id'] ), 'collection-type' );
wp_set_post_terms( $stamp_id, array( $stamp['term_id'] ), 'collection-type' );
}
register_deactivation_hook( __FILE__, 'taxdd_on_deactivated' );
function taxdd_on_deactivated() {
taxdd_register_post_type();
$query = new WP_Query( array(
'post_type' => 'collection',
'nopaging' => TRUE,
'fields' => 'ids',
) );
$posts = &$query->get_posts();
foreach( $posts as $post ) {
wp_delete_post( $post );
}
$taxonomies = get_terms( 'collection-type', array( 'hide_empty' => FALSE ) );
foreach( $taxonomies as $tax ) {
wp_delete_term( $tax->term_id, 'collection-type' );
}
}
add_action( 'restrict_manage_posts', 'taxdd_restrict_manage_posts' );
function taxdd_restrict_manage_posts() {
if( !isset( $_GET['post_type']) || $_GET['post_type'] != 'collection' ) {
return;
}
$term = get_term_by( 'id', $_GET['cat'], 'collection-type' );
wp_dropdown_categories(
array(
'show_option_all' => 'All Collections',
'taxonomy' => 'collection-type',
'show_count' => 0,
'selected' => $term->term_id,
'hierarchical' => TRUE,
'name' => 'cat',
'value_field' => 'term_id',
)
);
// slug로 필터링하는 방법. 택소노미의 query_var와 name 속성을 맞췄다.
// 다만 이렇게 하면 option 태그의 selected 속성이 출력되지 않는다.
// $term = get_term_by( 'id', $_GET['collection-type'], 'collection-type' );
// wp_dropdown_categories(
// array(
// 'show_option_all' => 'All Collections',
// 'taxonomy' => 'collection-type',
// 'show_count' => 0,
// 'selected' => $term->slug,
// 'hierarchical' => TRUE,
// 'name' => 'collection-type',
// 'value_field' => 'slug',
// )
// );
}
add_action( 'parse_tax_query', 'taxdd_parse_tax_query' );
function taxdd_parse_tax_query( $wp_query ) {
$wp_query->tax_query->queries[0]['taxonomy'] = 'collection-type';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment