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;
}