ํƒœ๊ทธ ๋ณด๊ด€๋ฌผ: custom-taxonomy

custom-taxonomy

๋ถ„๋ฅ˜๋ฒ• ๋ฐ post_type์œผ๋กœ ์šฉ์–ด ๊ฐ€์ ธ ์˜ค๊ธฐ ์ฒด๊ณ„ โ€˜ํƒœ๊ทธโ€™๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. get_terms ()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ„๋ฅ˜

2 ๊ฐœ์˜ ๋งž์ถค ๊ฒŒ์‹œ๋ฌผ ์œ ํ˜• โ€˜๋ถ๋งˆํฌโ€™๋ฐ โ€˜์Šค ๋‹ˆํŽซโ€™๊ณผ ๊ณต์œ  ๋ถ„๋ฅ˜ ์ฒด๊ณ„ โ€˜ํƒœ๊ทธโ€™๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. get_terms ()๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ„๋ฅ˜ ์ฒด๊ณ„์˜ ๋ชจ๋“  ์šฉ์–ด ๋ชฉ๋ก์„ ์ƒ์„ฑ ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๋ชฉ๋ก์„ ๊ฒŒ์‹œ๋ฌผ ์œ ํ˜•์œผ๋กœ ์ œํ•œํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋‚ด๊ฐ€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ฐพ๊ณ ์žˆ๋Š” ๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

get_terms(array('taxonomy' => 'tag', 'post_type' => 'snippet'));

์ด๊ฒƒ์„ ๋‹ฌ์„ฑ ํ•  ์ˆ˜์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๊นŒ? ์•„์ด๋””์–ด๋Š” ๋Œ€๋‹จํžˆ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค !!

์•„, ๋‚œ WP 3.1.1์—์žˆ๋‹ค



๋‹ต๋ณ€

ํ•˜๋‚˜์˜ SQL ์ฟผ๋ฆฌ๋กœ ๋น„์Šทํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

static public function get_terms_by_post_type( $taxonomies, $post_types ) {

    global $wpdb;

    $query = $wpdb->prepare(
        "SELECT t.*, COUNT(*) from $wpdb->terms AS t
        INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id
        INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id
        INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id
        WHERE p.post_type IN('%s') AND tt.taxonomy IN('%s')
        GROUP BY t.term_id",
        join( "', '", $post_types ),
        join( "', '", $taxonomies )
    );

    $results = $wpdb->get_results( $query );

    return $results;

}

๋‹ต๋ณ€

๊ทธ๋ž˜์„œ ๋‚ด๊ฐ€ ์ž‘์—…์ค‘ ์ธ ํ”„๋กœ์ ํŠธ์— ๋Œ€ํ•ด ๊ทธ๋Ÿฐ ๊ฒƒ์ด ํ•„์š”ํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‚˜๋Š” ๋‹จ์ˆœํžˆ ์‚ฌ์šฉ์ž ์ •์˜ ์œ ํ˜•์˜ ๋ชจ๋“  ๊ฒŒ์‹œ๋ฌผ์„ ์„ ํƒํ•˜๋Š” ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•œ ๋‹ค์Œ ์‚ฌ์šฉ์ค‘์ธ ๋ถ„๋ฅ˜ ์ฒด๊ณ„์˜ ์‹ค์ œ ์šฉ์–ด๊ฐ€ ๋ฌด์—‡์ธ์ง€ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฐ ๋‹ค์Œ ๋ถ„๋ฅ˜๋ฒ•์˜ ๋ชจ๋“  ์šฉ์–ด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ get_terms() ๋‹ค์Œ ๋‘ ๋ชฉ๋ก ๋ชจ๋‘์—์žˆ๋Š” ๋งŒ ์‚ฌ์šฉํ•˜๊ณ  ํ•จ์ˆ˜๋กœ ๋ฌถ์–ด์„œ ์™„์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ID๋ณด๋‹ค ๋” ๋งŽ์€ ๊ฒƒ์ด ํ•„์š”ํ–ˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฆ„์ด ํ•„์š”ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— $fieldsํ•จ์ˆ˜์— ๋ฌด์—‡์„ ๋ฐ˜ํ™˜ ํ•ด์•ผํ•˜๋Š”์ง€ ์•Œ๋ ค์ฃผ๊ธฐ ์œ„ํ•ด ์ด๋ฆ„์ด ์ง€์ •๋œ ์ƒˆ๋กœ์šด ์ธ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค . ๊ทธ๋Ÿฐ ๋‹ค์Œ get_terms๋งŽ์€ ์ธ์ˆ˜ ๋ฅผ ํ—ˆ์šฉํ•˜๊ณ  ๋‚ด ํ•จ์ˆ˜๋Š” ๊ฒŒ์‹œ๋ฌผ ์œ ํ˜•์—์„œ ์‚ฌ์šฉ๋˜๋Š” ์šฉ์–ด๋กœ ์ œํ•œ๋˜์–ด ํ•˜๋‚˜ ์ด์ƒ์˜ if๋ช…๋ น๋ฌธ์„ ์ถ”๊ฐ€ ํ•˜๊ณ  ๊ฑฐ๊ธฐ์— ๋„๋‹ฌํ–ˆ์Šต๋‹ˆ๋‹ค.

ํ•จ์ˆ˜:

/* get terms limited to post type
 @ $taxonomies - (string|array) (required) The taxonomies to retrieve terms from.
 @ $args  -  (string|array) all Possible Arguments of get_terms http://codex.wordpress.org/Function_Reference/get_terms
 @ $post_type - (string|array) of post types to limit the terms to
 @ $fields - (string) What to return (default all) accepts ID,name,all,get_terms.
 if you want to use get_terms arguments then $fields must be set to 'get_terms'
*/
function get_terms_by_post_type($taxonomies,$args,$post_type,$fields = 'all'){
    $args = array(
        'post_type' => (array)$post_type,
        'posts_per_page' => -1
    );
    $the_query = new WP_Query( $args );
    $terms = array();
    while ($the_query->have_posts()){
        $the_query->the_post();
        $curent_terms = wp_get_object_terms( $post->ID, $taxonomy);
        foreach ($curent_terms as $t){
          //avoid duplicates
            if (!in_array($t,$terms)){
                $terms[] = $c;
            }
        }
    }
    wp_reset_query();
    //return array of term objects
    if ($fields == "all")
        return $terms;
    //return array of term ID's
    if ($fields == "ID"){
        foreach ($terms as $t){
            $re[] = $t->term_id;
        }
        return $re;
    }
    //return array of term names
    if ($fields == "name"){
        foreach ($terms as $t){
            $re[] = $t->name;
        }
        return $re;
    }
    // get terms with get_terms arguments
    if ($fields == "get_terms"){
        $terms2 = get_terms( $taxonomies, $args );
        foreach ($terms as $t){
            if (in_array($t,$terms2)){
                $re[] = $t;
            }
        }
        return $re;
    }
}

์šฉ๋ฒ•:

์šฉ์–ด ID ๋ชฉ๋ก ๋งŒ ํ•„์š”ํ•œ ๊ฒฝ์šฐ :

$terms = get_terms_by_post_type('tag','','snippet','ID');

์šฉ์–ด ์ด๋ฆ„ ๋ชฉ๋ก ๋งŒ ํ•„์š”ํ•œ ๊ฒฝ์šฐ :

$terms = get_terms_by_post_type('tag','','snippet','name');

์šฉ์–ด ๊ฐ์ฒด ๋ชฉ๋ก ๋งŒ ํ•„์š”ํ•œ ๊ฒฝ์šฐ :

$terms = get_terms_by_post_type('tag','','snippet');

๊ทธ๋ฆฌ๊ณ  get_terms์˜ ์ถ”๊ฐ€ ์ธ์ˆ˜๋ฅผ orderby, order, hierarchical๊ณผ ๊ฐ™์ด ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ โ€ฆ

$args = array('orderby' => 'count', 'order' => 'DESC',  'hide_empty' => 1);
$terms = get_terms_by_post_type('tag',$args,'snippet','get_terms');

์ฆ๊ฒจ!

์ตœ์‹  ์ •๋ณด:

์šฉ์–ด ๊ฐœ์ˆ˜๋ฅผ ํŠน์ • ๊ฒŒ์‹œ๋ฌผ ์œ ํ˜• ๋ณ€๊ฒฝ์œผ๋กœ ์ˆ˜์ •ํ•˜๋ ค๋ฉด ๋‹ค์Œ ๋‹จ๊ณ„๋ฅผ ๋”ฐ๋ฅด์‹ญ์‹œ์˜ค.

foreach ($current_terms as $t){
          //avoid duplicates
            if (!in_array($t,$terms)){
                $terms[] = $t;
            }
        }

์—:

foreach ($current_terms as $t){
    //avoid duplicates
    if (!in_array($t,$terms)){
        $t->count = 1;
        $terms[] = $t;
    }else{
        $key = array_search($t, $terms);
        $terms[$key]->count = $terms[$key]->count + 1;
    }
}

๋‹ต๋ณ€

๋‚œ ๋‹น์‹ ์ด ํ†ต๊ณผ ํ•  ์ˆ˜์žˆ๋Š” ๊ธฐ๋Šฅ์„ ์ผ๋‹ค post_type์—์„œ $args๋ฐ›๋Š” ๋ฐฐ์—ดget_terms() ๊ธฐ๋Šฅ :

HT์—์„œ @braydon์œผ๋กœ SQL ์ž‘์„ฑ

 /**
 * terms_clauses
 *
 * filter the terms clauses
 *
 * @param $clauses array
 * @param $taxonomy string
 * @param $args array
 * @return array
**/
function terms_clauses($clauses, $taxonomy, $args)
{
    global $wpdb;

    if ($args['post_type'])
    {
        $clauses['join'] .= " INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id";
        $clauses['where'] .= " AND p.post_type='{$args['post_type']}'";
    }
    return $clauses;
}
add_filter('terms_clauses', 'terms_clauses', 10, 3);

๋‹ต๋ณ€

์ข‹์€ ์งˆ๋ฌธ๊ณผ ํ™•์‹คํ•œ ๋‹ต๋ณ€.

terms_clauses ํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ @jessica์˜ ์ ‘๊ทผ ๋ฐฉ์‹์ด ์ •๋ง ๋งˆ์Œ์— ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. get_terms ํ•จ์ˆ˜๋ฅผ ๋งค์šฐ ํ•ฉ๋ฆฌ์ ์ธ ๋ฐฉ์‹์œผ๋กœ ํ™•์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

๋‚ด ์ฝ”๋“œ๋Š” @braydon์˜ ์ผ๋ถ€ SQL์„ ์‚ฌ์šฉํ•˜์—ฌ ์ค‘๋ณต์„ ์ค„์ด๊ธฐ ์œ„ํ•ด ๊ทธ๋…€์˜ ์•„์ด๋””์–ด๋ฅผ ๊ณ„์†ํ•ฉ๋‹ˆ๋‹ค. ๋˜ํ•œ post_types ๋ฐฐ์—ด์„ ํ—ˆ์šฉํ•ฉ๋‹ˆ๋‹ค.

/**
 * my_terms_clauses
 *
 * filter the terms clauses
 *
 * @param $clauses array
 * @param $taxonomy string
 * @param $args array
 * @return array
 **/
function my_terms_clauses($clauses, $taxonomy, $args)
{
  global $wpdb;

  if ($args['post_types'])
  {
    $post_types = $args['post_types'];

    // allow for arrays
    if ( is_array($args['post_types']) ) {
      $post_types = implode("','", $args['post_types']);
    }
    $clauses['join'] .= " INNER JOIN $wpdb->term_relationships AS r ON r.term_taxonomy_id = tt.term_taxonomy_id INNER JOIN $wpdb->posts AS p ON p.ID = r.object_id";
    $clauses['where'] .= " AND p.post_type IN ('". esc_sql( $post_types ). "') GROUP BY t.term_id";
  }
  return $clauses;
}
add_filter('terms_clauses', 'my_terms_clauses', 99999, 3);

get_terms์—๋Š” GROUPY BY์— ๋Œ€ํ•œ ์ ˆ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— WHERE ์ ˆ ๋์— ์ถ”๊ฐ€ํ•ด์•ผํ–ˆ์Šต๋‹ˆ๋‹ค. ํ•„ํ„ฐ ์šฐ์„  ์ˆœ์œ„๊ฐ€ ๋งค์šฐ ๋†’๊ฒŒ ์„ค์ •๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.


๋‹ต๋ณ€

์œ„ ์ฝ”๋“œ์˜ Gavin ๋ฒ„์ „๊ณผ ์ž‘๋™ํ•˜๋„๋ก get_terms ์ธ์ˆ˜๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์—†์—ˆ์ง€๋งŒ ๋งˆ์ง€๋ง‰์œผ๋กœ ๋ณ€๊ฒฝํ•˜์—ฌ ์ˆ˜ํ–‰ํ–ˆ์Šต๋‹ˆ๋‹ค.

$terms2 = get_terms( $taxonomy );

์—

$terms2 = get_terms( $taxonomy, $args );

Bainternet์˜ ์›๋ž˜ ๊ธฐ๋Šฅ๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.


๋‹ต๋ณ€

@Bainternet : ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค! ์ž‘๋™ํ•˜์ง€ ์•Š์•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ธฐ๋Šฅ์„ ์•ฝ๊ฐ„ ๋ณ€๊ฒฝํ•ด์•ผํ–ˆ์Šต๋‹ˆ๋‹ค (์ผ๋ถ€ ์˜คํƒ€). ์œ ์ผํ•œ ๋ฌธ์ œ๋Š” ์ด์ œ ์šฉ์–ด ๊ฐœ์ˆ˜๊ฐ€ ๊บผ์ ธ ์žˆ๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์นด์šดํŠธ๋Š” ๊ฒŒ์‹œ๋ฌผ ์œ ํ˜•์„ ๊ณ ๋ คํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ get_terms ()๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

function get_terms_by_post_type($post_type,$taxonomy,$fields='all',$args){
    $q_args = array(
        'post_type' => (array)$post_type,
        'posts_per_page' => -1
    );
    $the_query = new WP_Query( $q_args );

    $terms = array();

    while ($the_query->have_posts()) { $the_query->the_post();

        global $post;

        $current_terms = get_the_terms( $post->ID, $taxonomy);

        foreach ($current_terms as $t){
            //avoid duplicates
            if (!in_array($t,$terms)){
                $t->count = 1;
                $terms[] = $t;
            }else{
                $key = array_search($t, $terms);
                $terms[$key]->count = $terms[$key]->count + 1;
            }
        }
    }
    wp_reset_query();

    //return array of term objects
    if ($fields == "all")
        return $terms;
    //return array of term ID's
    if ($fields == "ID"){
        foreach ($terms as $t){
            $re[] = $t->term_id;
        }
        return $re;
    }
    //return array of term names
    if ($fields == "name"){
        foreach ($terms as $t){
            $re[] = $t->name;
        }
        return $re;
    }
    // get terms with get_terms arguments
    if ($fields == "get_terms"){
        $terms2 = get_terms( $taxonomy, $args );

        foreach ($terms as $t){
            if (in_array($t,$terms2)){
                $re[] = $t;
            }
        }
        return $re;
    }
}

ํŽธ์ง‘ : ์ˆ˜์ • ์‚ฌํ•ญ์ด ์ถ”๊ฐ€๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์–ด๋–ป๊ฒŒ ๋“  ๊ทธ๊ฒƒ์€ ์—ฌ์ „ํžˆ โ€‹โ€‹๋‚˜๋ฅผ ์œ„ํ•ด ์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์นด์šดํŠธ์— ์—ฌ์ „ํžˆ ์ž˜๋ชป๋œ ๊ฐ’์ด ํ‘œ์‹œ๋ฉ๋‹ˆ๋‹ค.


๋‹ต๋ณ€

์ค‘๋ณต์„ ํ”ผํ•˜์‹ญ์‹œ์˜ค :

//avoid duplicates
    $mivalor=$t->term_id;
    $arr=array_filter($terms, function ($item) use ($mivalor) {return isset($item->term_id) && $item->term_id == $mivalor;});

    if (empty($arr)){
    $t->count=1;
            $terms[] = $t;
        }else{
            $key = array_search($t, $terms);
            $terms[$key]->count = $terms[$key]->count + 1;
        }