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

custom-taxonomy

wp_query๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ถ„๋ฅ˜๋ฒ•์œผ๋กœ ์ฃผ๋ฌธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๊นŒ? ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ๋‚ด ๋ฌธ์ œ๋Š” ๋ถ„๋ฅ˜๋ฒ•์œผ๋กœ ์ฃผ๋ฌธํ•˜๊ณ  ์‹ถ์ง€๋งŒ

๋‚ด ์งˆ๋ฌธ์€ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค. WP_Query๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ tax_query๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ถ„๋ฅ˜๋ฒ•์œผ๋กœ ํ•„ํ„ฐ๋งํ•˜๋Š” ์ผ๋ถ€ ์‚ฌ์šฉ์ž ์ •์˜ ์œ ํ˜• ๊ฒŒ์‹œ๋ฌผ์„ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค.

์ด์ œ ๋‚ด ๋ฌธ์ œ๋Š” ๋ถ„๋ฅ˜๋ฒ•์œผ๋กœ ์ฃผ๋ฌธํ•˜๊ณ  ์‹ถ์ง€๋งŒ ์›น์—์„œ ๋ฌธ์„œ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ณ  ๊ฒ€์ƒ‰ํ•˜๋ฉด ํ•ด๊ฒฐ์ฑ…์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

WP_Query์˜ orderby๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์‚ฌ์šฉ์ž ์ •์˜ ๋ฉ”ํƒ€ ํ•„๋“œ์กฐ์ฐจ๋„ ์—ฌ๋Ÿฌ ํ•„๋“œ๋กœ ์ฃผ๋ฌธํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๋ถ„๋ฅ˜๋ฒ•์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š” ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์˜ฌ๋ฐ”๋ฅธ ๋ฐฉํ–ฅ์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ๊ฐ€ ์žˆ์Šต๋‹ˆ๊นŒ?

๋‹ค๋“ค ๊ฐ์‚ฌ ํ•ด์š”.



๋‹ต๋ณ€

์•„๋‹ˆ์š”, ๋ถ„๋ฅ˜ ๋ฐฉ์‹์œผ๋กœ ์ฃผ๋ฌธํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ํŠน์ • ์œ ํ˜•์˜ ๊ด€์ ์—์„œ๋Š” ์‹ค์ œ๋กœ ์˜๋ฏธ๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

๋ถ„๋ฅ˜๋ฒ•์€ ์‚ฌ๋ฌผ์„ ๊ทธ๋ฃนํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฒŒ์‹œ๋ฌผ์— ๋ถ„๋ฅ˜ ์ฒด๊ณ„๊ฐ€ ์žˆ๋‹ค๋Š” ๊ฒƒ์€ ์‹ค์ œ๋กœ ํ•ด๋‹น ๋ถ„๋ฅ˜ ์ฒด๊ณ„์—์„œ ๊ฒŒ์‹œ๋ฌผ๊ฐ„์— ๊ณต์œ ๋˜๋Š” ์šฉ์–ด๋ฅผ ๊ฐ–๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋ถ„๋ฅ˜ ์ฒด๊ณ„์— ๊ฐ ๊ฒŒ์‹œ๋ฌผ์— ํ•œ ์šฉ์–ด ๋งŒ ์‚ฌ์šฉ ๋œ ๊ฒฝ์šฐ ๋ถ„๋ฅ˜ ์ฒด๊ณ„๋ฅผ ๋ฌด์˜๋ฏธํ•˜๊ฒŒ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์šฉ์–ด๊ฐ€ ์›๋ž˜๋Œ€๋กœ ๊ณต์œ ๋œ๋‹ค๋ฉด, ๊ทธ๊ฒƒ์„ ์ˆœ์„œ๋Œ€๋กœ ์ฃผ๋ฌธํ•˜๋ฉด ํŠน๋ณ„ํžˆ ์œ ์šฉํ•œ ๊ฒƒ์ด ์—†์Šต๋‹ˆ๋‹ค.

์ด๋Ÿฌํ•œ ์ƒํ™ฉ์—์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•  ๊ฒƒ์€ ํฌ์ŠคํŠธ ๋ฉ”ํƒ€์ž…๋‹ˆ๋‹ค. ๊ฒŒ์‹œ๋ฌผ ๋ฉ”ํƒ€๋ณ„๋กœ ์ฃผ๋ฌธํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ ๊ฐ ๊ฒŒ์‹œ๋ฌผ๋งˆ๋‹ค ๊ณ ์œ ํ•ฉ๋‹ˆ๋‹ค.

ํŽธ์ง‘ : ์ฆ‰, ํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‚ฌ์šฉ์ž ์ง€์ • SQL ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•˜์—ฌ ๋ถ„๋ฅ˜๋ฒ•์œผ๋กœ ์ฃผ๋ฌธํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์ˆ˜์ •๋˜์ง€ ์•Š์€ WP_Query์—์„œ ์ˆ˜ํ–‰ ํ•  ์ˆ˜๋Š” ์—†์Šต๋‹ˆ๋‹ค : http://scribu.net/wordpress/sortable-taxonomy-columns.html

๊ทธ๋Ÿฌ๋‚˜ ์ด๋Ÿฐ ์ข…๋ฅ˜์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ ํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ ๋ฐ์ดํ„ฐ ๋””์ž์ธ ๊ตฌ์กฐ๊ฐ€ ์ฒ˜์Œ์— ์ž˜๋ชป๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๋ถ„๋ฅ˜์—์„œ โ€œ์šฉ์–ดโ€๋Š” ์‹ค์ œ โ€œ๋ฐ์ดํ„ฐโ€๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ์šฉ์–ด ์ž์ฒด๋Š” ๊ณ ์œ  ํ•œ ์˜๋ฏธ๊ฐ€ ์—†์œผ๋ฉฐ ์„ค๋ช…ํ•˜๋Š” ํŠน์ • ๊ทธ๋ฃน์— ๋Œ€ํ•œ ๋ ˆ์ด๋ธ” ์ผ๋ฟ์ž…๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ๋“ค์„ ์˜๋ฏธ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋กœ ์ทจ๊ธ‰ํ•œ๋‹ค๋ฉด ๊ทผ๋ณธ์ ์ธ ๋””์ž์ธ ๊ฒฐํ•จ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

๋ถ„๋ฅ˜๋ฒ•์€ ์šฉ์–ด๋ฅผ ์ง€์ •ํ•˜์—ฌ ํ•ญ๋ชฉ์„ ๊ทธ๋ฃนํ™”ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ ๊ทธ๋ฃนํ™”๋Š” ๋ถ„๋ฅ˜๋ฒ•์˜ ์š”์ ์ด๋ฉฐ, ์šฉ์–ด๋Š” ๊ทธ๋ฃนํ™”์— ๋Œ€ํ•œ ์˜ˆ์œ ์–ผ๊ตด์ž…๋‹ˆ๋‹ค. ๊ฒŒ์‹œ๋ฌผ์— ํ• ๋‹น ํ•  ์˜๋ฏธ์žˆ๋Š” ๋ฉ”ํƒ€ ๋ฐ์ดํ„ฐ๊ฐ€์žˆ๋Š” ๊ฒฝ์šฐ ๋Œ€์‹  ๊ฒŒ์‹œ๋ฌผ ๋ฉ”ํƒ€๋ฅผ ์‚ฌ์šฉํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. post meta๋Š” ํ‚ค์™€ ๊ฐ’์„ ๋ชจ๋‘ ์‚ฌ์šฉํ•˜์—ฌ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ฃผ๋ฌธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค . ๋ถ„๋ฅ˜๋ฒ•์„ ์‚ฌ์šฉํ•˜๋ฉด ์‹ค์ œ๋กœ ํ‚ค๋งŒ ์ €์žฅํ•˜๋ฉฐ ํ•ด๋‹น ๊ฐ’์€ ํ•ด๋‹น ์šฉ์–ด๋ณ„๋กœ ๊ทธ๋ฃนํ™” ๋œ ๊ฒŒ์‹œ๋ฌผ์ž…๋‹ˆ๋‹ค.

์˜ฌ๋ฐ”๋ฅธ ์ ‘๊ทผ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด ์žฅ๊ธฐ์ ์œผ๋กœ ํ›จ์”ฌ ์‰ฌ์›Œ์ง‘๋‹ˆ๋‹ค. ๋‚˜๋Š” ๋ถ„๋ฅ˜๋ฒ•์œผ๋กœ ์ด์ƒํ•œ ์ผ์„ ํ•  ์ˆ˜ ์—†๋‹ค๊ณ  ๋งํ•˜์ง€๋Š” ์•Š์ง€๋งŒ ์žฅ๊ธฐ์ ์œผ๋กœ ์ž˜๋ชป ์‚ฌ์šฉํ•˜์—ฌ ์Šค์Šค๋กœ๋ฅผ ํž˜๋“ค๊ฒŒ ๋งŒ๋“ค๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.


๋‹ต๋ณ€

์ด ์งˆ๋ฌธ์— ๋Œ€ํ•œ ๋‹ต๋ณ€์€ ํ—ˆ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์„ธ๊ธˆ์œผ๋กœ ์ฃผ๋ฌธํ•˜๋Š” ๊ฒƒ์ด โ€œ์ด์น˜๊ฐ€ ์—†๋‹คโ€๊ณ  ๊ฐ€์ •ํ•˜๋Š” ๊ฒƒ์€ ๋น„๋…ผ๋ฆฌ์ ์ž…๋‹ˆ๋‹ค. ๊ทธ๊ฐ€ ํ•œ ๋Œ€๋‹ต์€ ์ดํ•ด๊ฐ€๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋ฉ”๋‰ด ๊ฒŒ์‹œ๋ฌผ ์œ ํ˜•์„ ๊ณ ๋ คํ•˜์‹ญ์‹œ์˜ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ โ€œFood Categoriesโ€์˜ ๊ด€์„ธ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์‹ํ’ˆ ์นดํ…Œ๊ณ ๋ฆฌ ์„ธ๊ธˆ์—๋Š” โ€œ์กฐ์‹โ€, โ€œ์ ์‹ฌโ€๋ฐ โ€œ์ €๋…โ€์ด๋ผ๋Š” ์šฉ์–ด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. tax_query ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฟผ๋ฆฌ๋ฅผ ์ œ์ถœํ•˜๋ฉด ๋ชจ๋“  ์šฉ์–ด๊ฐ€ ํฌํ•จ ๋œ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์ด ์ƒ์„ฑ๋˜์ง€๋งŒ ๊ฒŒ์‹œ ๋‚ ์งœ์ˆœ์œผ๋กœ ์ •๋ ฌ๋ฉ๋‹ˆ๋‹ค.

์šฉ์–ด์™€ ๊ด€๋ จํ•˜์—ฌ ์ด๋“ค ์ค‘ ์˜ฌ๋ฐ”๋ฅธ ์ˆœ์„œ๋ฅผ ์–ป์€ ๋‹ค์Œ ๊ฒŒ์‹œ๋ฌผ์„ ๋‹ค์–‘ํ•œ ๋ฒ”์ฃผ๋กœ ๋ถ„๋ฆฌํ•˜์—ฌ ํ”„๋ŸฐํŠธ ์—”๋“œ์— ์ ์ ˆํ•˜๊ฒŒ ํ‘œ์‹œํ•˜๋ ค๋ฉด ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์„ ๋ฐ˜๋ณต ํ•œ ๋‹ค์Œ ๊ฐ ๊ฒŒ์‹œ๋ฌผ์„ ์ฟผ๋ฆฌํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์šฉ์–ด๋ฅผ ์ฐพ๊ณ  ํ˜„์žฌ ์šฉ์–ด์™€ ๋น„๊ตํ•˜๊ณ  ๋ฐฐ์—ด๋กœ ํ•„ํ„ฐ๋ง ํ•œ ํ›„ ๊ณ„์† ์ง„ํ–‰ํ•˜๋ ค๋ฉด ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฐ ๋‹ค์Œ ํ‘œ์‹œ๋ฅผ ์œ„ํ•ด ์ƒˆ ์–ด๋ ˆ์ด๋ฅผ ๋‹ค์‹œ ๋ฐ˜๋ณตํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์ด๊ฒƒ์€ ์ƒ์‚ฐ์ ์ด์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

WP์— โ€œpost__inโ€์˜ต์…˜๊ณผ ๊ฐ™์ด โ€œtax__inโ€orderby ์˜ต์…˜์ด ์žˆ์œผ๋ฉด ์ข‹์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ ‡์ง€ ์•Š์€ ๊ฒฝ์šฐ ์œ„์˜ ์–ด๋ฆฌ์„์€ ํ”„๋กœ์„ธ์Šค๋ฅผ ์ˆ˜ํ–‰ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. orderby ๋ฉ”์†Œ๋“œ๋ฅผ ์กฐ์ •ํ•˜๊ณ  ๊ฐ๊ฐ ์šฉ์–ด๋ฅผ ๊ฒฐ๊ณผ ์„ธํŠธ์— ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด โ€˜posts_orderbyโ€™ํ•„ํ„ฐ ๋ฐ โ€˜posts_joinโ€™ํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฟผ๋ฆฌ๋ฅผ ์‚ฌ์šฉ์ž ์ •์˜ํ•˜์‹ญ์‹œ์˜ค. ๋˜๋Š” ํ•ด๋‹น ์šฉ์–ด์™€ ๊ด€๋ จํ•˜์—ฌ html ์„น์…˜ ๋‚ด์—์„œ ํ•„ํ„ฐ๋ง ํ•  ๊ฐ ์šฉ์–ด์— ๋Œ€ํ•ด ์ƒˆ ์ฟผ๋ฆฌ๋ฅผ ์ž‘์„ฑํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

๊ฐ€์žฅ ํšจ์œจ์ ์ธ ๋ฐฉ๋ฒ•์€ ํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฟผ๋ฆฌ ๋ฌธ์ž์—ด์„ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฐ€์žฅ ์‰ฌ์šด ๋ฐฉ๋ฒ•์€ ์„ธ ๊ฐœ์˜ ๋ณ„๋„ ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. WP API๋Š” ์„ธ๊ธˆ ๋ณ„ ์ฃผ๋ฌธ ๋˜๋Š” ๋ชจ๋“  ์ œํ•œ์ ์ธ ์ฟผ๋ฆฌ ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ์ฒ˜๋ฆฌํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ํŠน์ • ์กฐ๊ฑด์— ๋”ฐ๋ผ ์ฟผ๋ฆฌ๋ฅผ ์ œํ•œํ•˜๋Š” ๊ฒฝ์šฐ ๋งŽ์€ ์‚ฌ๋žŒ๋“ค์ด ๋™์ผํ•œ ์กฐ๊ฑด์œผ๋กœ ์ฃผ๋ฌธํ•ด์•ผ ํ•  ๊ฐ€๋Šฅ์„ฑ์ด ๋†’์Šต๋‹ˆ๋‹ค.


๋‹ต๋ณ€

์˜ˆ,ํ•˜์ง€๋งŒ ๊ฝค ๊ด€๋ จ์ด ์žˆ์Šต๋‹ˆ๋‹ค โ€ฆ

ํ…Œ๋งˆ์—์„œ functions.php์— ์ถ”๊ฐ€ํ•˜์‹ญ์‹œ์˜ค.

function orderby_tax_clauses( $clauses, $wp_query ) {
    global $wpdb;
    $taxonomies = get_taxonomies();
    foreach ($taxonomies as $taxonomy) {
        if ( isset( $wp_query->query['orderby'] ) && $taxonomy == $wp_query->query['orderby'] ) {
            $clauses['join'] .=<<<SQL
LEFT OUTER JOIN {$wpdb->term_relationships} ON {$wpdb->posts}.ID={$wpdb->term_relationships}.object_id
LEFT OUTER JOIN {$wpdb->term_taxonomy} USING (term_taxonomy_id)
LEFT OUTER JOIN {$wpdb->terms} USING (term_id)
SQL;
            $clauses['where'] .= " AND (taxonomy = '{$taxonomy}' OR taxonomy IS NULL)";
            $clauses['groupby'] = "object_id";
            $clauses['orderby'] = "GROUP_CONCAT({$wpdb->terms}.name ORDER BY name ASC) ";
            $clauses['orderby'] .= ( 'ASC' == strtoupper( $wp_query->get('order') ) ) ? 'ASC' : 'DESC';
        }
    }
    return $clauses;
}

    add_filter('posts_clauses', 'orderby_tax_clauses', 10, 2 );

์ด๊ฒƒ์€ ๋‚ด๊ฐ€ ์ฐพ์€ ๊ฒƒ๋“ค๊ณผ ๋‚ด๊ฐ€ ํ•œ ์ผ๋“ค๋กœ ํ”„๋ž‘์ผ„์Šˆํƒ€์ธ์ž…๋‹ˆ๋‹ค. ์„ค๋ช…ํ•˜๊ธฐ๋Š” ์–ด๋ ต์ง€๋งŒ ๊ฒฐ๋ก ์€ ์ด๊ฒƒ์ž…๋‹ˆ๋‹ค.? orderby = (taxonomy query var) & order = ASC (๋˜๋Š” DESC)๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


๋‹ต๋ณ€

๋‚˜๋Š” ์—ฌ๊ธฐ์„œ ๊ฒŒ์ž„์— ๋Šฆ๊ฒŒ ์˜ฌ ๊ฒƒ์ด๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์ด๊ฒƒ์„ํ•˜๋Š” ๋” ๊ฐ„๋‹จํ•œ WordPressy ๋ฐฉ๋ฒ•์ด์žˆ๋‹ค.

ํ‰์†Œ์ฒ˜๋Ÿผ ์„ธ๊ธˆ ๋ฌธ์˜๋ฅผ ์ž‘์„ฑํ•˜์‹ญ์‹œ์˜ค.

$tax_query = array();
$tax_query['relation']="OR";
$tax_query[] = array(
    'taxonomy' => 'product_cat',
    'field'    => 'slug',
    'terms'    => $cat_terms,
);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

query_posts ๋˜๋Š” WP_Query์— ๋Œ€ํ•œ ์ธ์ˆ˜ ์„ค์ •

$args = array(
    'post_type'=>'post',
    'posts_per_page'=>12,
    'paged'=>$paged,
    'tax_query' => $tax_query,
);

query_posts / WP_Query ํ˜ธ์ถœ์„ํ•˜๊ธฐ ์ „์— orderby ํ•„ํ„ฐ์— ์—ฐ๊ฒฐํ•˜์—ฌ ์žฌ์ •์˜ํ•˜์‹ญ์‹œ์˜ค.

add_filter('posts_orderby', 'edit_posts_orderby');
function edit_posts_orderby($orderby_statement) {
    $orderby_statement = " term_taxonomy_id ASC ";
    return $orderby_statement;
}
query_posts($args);
remove_filter('posts_orderby', 'edit_posts_orderby');

๋‚˜์ค‘์— ํ•„ํ„ฐ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ์„ ์žŠ์ง€ ๋งˆ์‹ญ์‹œ์˜ค โ€ฆ

์ด๊ฒƒ์€ tax_query๊ฐ€ ๋‹น์‹ ์„ ์œ„ํ•ด ์กฐ์ธ ๋“ฑ์„ ์ƒ์„ฑํ•˜๋Š” b / c์—์„œ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค. ์กฐ์ธ์—์„œ ํ•„๋“œ ์ค‘ ํ•˜๋‚˜๋ฅผ ์ˆœ์„œ๋กœ ์ง€์ •ํ•˜๋ฉด๋ฉ๋‹ˆ๋‹ค.


๋‹ต๋ณ€

๊ธ€์Ž„, ์นดํ…Œ๊ณ ๋ฆฌ / ํƒ์‹œ ๋…ธ๋ฏธ๋ณ„๋กœ ์‚ฌ์šฉ์ž ์ •์˜ ๊ฒŒ์‹œ๋ฌผ ์œ ํ˜•์„ ์ •๋ ฌ ํ•œ ๊ฒฝํ—˜์„ ๊ณต๊ฐœํ•˜๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค.

์ธํ„ฐ๋„ท

  1. WordPress์—์„œ ์‹คํ–‰๋˜๋Š” ์—ฌํ–‰์‚ฌ ์›น ์‚ฌ์ดํŠธ
  2. โ€˜rutaโ€™๋ผ๋Š” ๋งž์ถค ๊ฒŒ์‹œ๋ฌผ ์œ ํ˜•์˜ ๊ธฐ๋ณธ ์ฝ˜ํ…์ธ 
  3. ์ด ๊ตฌ์กฐ์˜ ๋ถ„๋ฅ˜ ์ฒด๊ณ„ ์—ฌํ–‰ ์œ ํ˜•> ๋Œ€๋ฅ™> ๊ตญ๊ฐ€

๊ฒฝ์šฐ

๋ณด๊ด€ ์นดํ…Œ๊ณ ๋ฆฌ ๋ชฉ๋ก ํŽ˜์ด์ง€์—์„œ ๊ณ ๊ฐ์€ ๊ฒŒ์‹œ๋ฌผ์„ ์ •๋ ฌํ•˜๊ธฐ๋ฅผ ์›ํ–ˆ์Šต๋‹ˆ๋‹ค.

  1. ๊ฐ ๋Œ€๋ฅ™์˜ ๊ฒฝ๋กœ ์ˆ˜์— ๋”ฐ๋ผ ์ˆœ์„œ๊ฐ€ ์ง€์ •๋œ ๋Œ€๋ฅ™.
  2. ์•ŒํŒŒ๋ฒณ์ˆœ์œผ๋กœ ์ฃผ๋ฌธ ๋œ ๊ตญ๊ฐ€์ž…๋‹ˆ๋‹ค.

๋‹จ๊ณ„

๋จผ์ € ์ˆ˜์ •๋˜์ง€ ์•Š์€ ์•„์นด์ด๋ธŒ ํŽ˜์ด์ง€ ์ฟผ๋ฆฌ์—์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์š”์ฒญ์„ ๋ฐ›์•˜์Šต๋‹ˆ๋‹ค.

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID 
FROM wp_posts 
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) 
WHERE 1=1 
AND ( wp_term_relationships.term_taxonomy_id IN (5,6,7,8,9,10,11,12,13,15,16,17,18,19,20,21,22,23,25,26,28,29,31,32,33,35,38,95,101,102,193) )
AND wp_posts.post_type IN ('ruta', 'nav_menu_item') 
AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 45 
AND wp_posts.post_status = 'private') 
GROUP BY wp_posts.ID 
ORDER BY wp_posts.post_date DESC LIMIT 0, 20

๋‘˜์งธ , Sequel Pro์—์„œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋Œ€ํ•ด SQL ์ฝ”๋“œ๋ฅผ ํŽธ์ง‘ํ•˜์—ฌ ๋‚ด ์š”๊ตฌ๋ฅผ ์ถฉ์กฑํ–ˆ์Šต๋‹ˆ๋‹ค. ๋‚˜๋Š” ์ด๊ฒƒ์„ ๊ฐ€์ง€๊ณ  ๋‚˜์˜จ๋‹ค.

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID, tt1.parent AS pare,
    (
    SELECT COUNT(*) 
    FROM  wp_posts
    INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
    INNER JOIN wp_term_taxonomy AS tt1 ON ( tt1.term_taxonomy_id =      wp_term_relationships.term_taxonomy_id )
    INNER JOIN wp_term_taxonomy AS tt2 ON ( tt2.term_taxonomy_id =  tt1.term_taxonomy_id )
    WHERE 1=1  
    AND tt1.parent = pare
    ) AS Total
FROM  wp_posts
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN wp_term_taxonomy AS tt1 ON ( tt1.term_taxonomy_id =      wp_term_relationships.term_taxonomy_id )
INNER JOIN wp_terms ON ( tt1.term_id = wp_terms.term_id )
WHERE 1=1  
AND ( wp_term_relationships.term_taxonomy_id IN (5,6,7,8,9,10,11,12,13,15,16,17,18,19,20,21,22,23,25,26,28,29,31,32,33,35,38,95,101,102,193) ) 
AND wp_posts.post_type IN ('ruta', 'nav_menu_item') 
AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 45 
AND wp_posts.post_status = 'private') 
GROUP BY wp_posts.ID 
ORDER BY
total DESC,
wp_terms.name  

์…‹์งธ , posts_fields, posts_join ๋ฐ posts_orderby์˜ ์„ธ ๊ฐ€์ง€ ํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ functions.php ํŒŒ์ผ์— ์ฟผ๋ฆฌ๋ฅผ ์—ฐ๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.

functions.php์˜ ์ฝ”๋“œ :

function xc_query_fields( $fields ) {

   $fields = "wp_posts.ID, wp_posts.post_title, wp_terms.name, tt1.parent AS pare,
    (
    SELECT COUNT(*) 
    FROM  wp_posts
    INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
    INNER JOIN wp_term_taxonomy AS tt1 ON ( tt1.term_taxonomy_id = wp_term_relationships.term_taxonomy_id )
    INNER JOIN wp_term_taxonomy AS tt2 ON ( tt2.term_taxonomy_id = tt1.term_taxonomy_id )
    WHERE 1=1  
    AND tt1.parent = pare
    )
    AS Total";
     return $fields;
}


function xc_query_joins( $join ) {
$join .= "INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
   INNER JOIN wp_term_taxonomy AS tt1 ON ( tt1.term_taxonomy_id = wp_term_relationships.term_taxonomy_id )
   INNER JOIN wp_terms ON ( tt1.term_id = wp_terms.term_id )";
 return $join;
}


function xc_query_orderby( $join ) {
    $join = "total DESC, wp_terms.name ";
    return $join;
 }

๋งˆ์ง€๋ง‰์œผ๋กœ ์ผ๋ถ€ ์กฐ๊ฑด์— ๋”ฐ๋ผ pre_get_post ํ›„ํฌ์—์„œ ํ•„ํ„ฐ๋ฅผ ํŠธ๋ฆฌ๊ฑฐํ–ˆ์Šต๋‹ˆ๋‹ค.

function filtra_queries( $query )
{

  if (  is_archive()  && $query->is_main_query() && !is_admin()  ) {

$rutes = array('viajes-privados', 'asia', 'africa', 'oceania', 'america', 'oriente-proximo');

if  ( in_array( $query->get('category_name'), $rutes ) ) 
  {
  add_filter( 'posts_fields', 'xc_query_fields' );
  add_filter( 'posts_join', 'xc_query_joins' );
  add_filter( 'posts_orderby', 'xc_query_orderby' );
}// end if in_array

  }// end if is_archive

}
 add_filter('pre_get_posts', 'filtra_queries');

์ด๊ฒƒ์ด ๋ˆ„๊ตฐ๊ฐ€๋ฅผ ๋„์šธ ์ˆ˜ ์žˆ๊ธฐ๋ฅผ ๋ฐ”๋ž๋‹ˆ๋‹ค.


๋‹ต๋ณ€

๋‚˜๋Š” ๋งค์šฐ ๋น„์Šทํ•œ ๋ฌธ์ œ๋ฅผ ๊ฒช์—ˆ๋‹ค. ์ปค์Šคํ…€ ๋ถ„๋ฅ˜๋ฒ• (๋ฌธ์ œ)์— ๋”ฐ๋ผ ์ปค์Šคํ…€ ํฌ์ŠคํŠธ ํƒ€์ž… ์•„์นด์ด๋ธŒ (์žก์ง€ ๊ธฐ์‚ฌ)๋ฅผ ์ฃผ๋ฌธํ•˜๊ณ  ์‹ถ๋‹ค. ๋‚˜๋Š” ๋‚ด ์‚ฌ์ดํŠธ์—์„œ ์ง์ ‘ SQL ์ฟผ๋ฆฌ๋ฅผ ์ˆ˜ํ–‰ํ•˜์ง€ ์•Š์œผ๋ฉฐ ์ผ๋ฐ˜์ ์œผ๋กœ ๋‹ค๋ฅธ ๋‹ต๋ณ€๊ณผ ๊ฐ™์€ ๊ฒฝ์šฐ ์ ‘๊ทผ ๋ฐฉ์‹์„ ๋‹ค์‹œ ์ƒ๊ฐํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

๋ฌธ์ œ :

1) WordPress์—์„œ๋Š” ์ง€๋Šฅ์ ์ธ ๋ฐฉ์‹์œผ๋กœ ๋ถ„๋ฅ˜๋ฒ•์„ ์ฃผ๋ฌธํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

2) WordPress๋Š” orderbyํฌ์ŠคํŠธ ์œ ํ˜• WP_Query์—์„œ ๋ถ„๋ฅ˜๋ฒ•์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค (Otto ๊ฐ€ ์–ธ๊ธ‰ ํ•œ๋Œ€๋กœ).

์†”๋ฃจ์…˜ :

1) ๋ถ„๋ฅ˜๋ฒ• ๋ถ„๋ฅ˜๋Š” ํ˜„์žฌ Custom Taxonomy Order NE ํ”Œ๋Ÿฌ๊ทธ์ธ์—์„œ ๊ฐ€์žฅ ์ž˜ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค . WYSIWYG๋ฅผ ํ†ตํ•ด ๋ถ„๋ฅ˜๋ฒ•์„ ์ฃผ๋ฌธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.์ด wp-admin๋ฐฉ๋ฒ•์€ ์•„๋‹ˆ์ง€๋งŒ ๋” ๋‚˜์€ ๊ฒƒ์„ ์ฐพ์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.

ํ”Œ๋Ÿฌ๊ทธ์ธ์„ ์„ค์ •ํ•˜๋ฉด ์—ฌ๊ธฐ์—์„œ ํ•œ ๊ฒƒ๊ณผ ๋น„์Šทํ•œ ๊ฒƒ์„ ์–ป์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์˜ต์…˜์„ ๊ธฐ๋กํ•ด ๋‘ก๋‹ˆ๋‹ค Auto-sort Queries of this Taxonomy-์ดํ•˜๋Š” ์„ค์ • Custom Order as Defined Above; ์ด๊ฒƒ์€ ๋‹น์‹ ์—๊ฒŒ ๋‹น์‹ ์ด ํ•„์š”๋กœํ•˜๋Š” ์ฃผ๋ฌธ์„ ์–ป์Šต๋‹ˆ๋‹ค. ์Šคํฌ๋ฆฐ ์ƒท :

2) ๋ถ„๋ฅ˜ ๋œ ๋ถ„๋ฅ˜๋ฒ•์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ ์šฉ์–ด๋ฅผ ์‹คํ–‰ํ•˜๋Š” ์ผ๋ จ์˜ WP_Query ํ˜ธ์ถœ์„ ์ž‘์„ฑํ•˜์—ฌ ๋ถ„๋ฅ˜๋ฒ•์œผ๋กœ ์ •๋ ฌ ๋œ ์•„์นด์ด๋ธŒ๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์‚ฌ์šฉ get_terms()ํ•˜๋Š” ๋ชจ๋“  ์„ธ๊ธˆ ์šฉ์–ด์˜ ๋ฐฐ์—ด์„ ๋งŒ๋“ค๋ ค๋ฉด ๋‹ค์Œ์„ ์‹คํ–‰ํ•˜์—ฌ foreach๊ฐ ๊ธฐ๊ฐ„ ๋™์•ˆ. ์ด๋ ‡๊ฒŒํ•˜๋ฉด WP_Query๊ฐ ์šฉ์–ด ํ•ญ๋ชฉ์— ๋Œ€ํ•ด ํŠน์ • ์šฉ์–ด์— ๋Œ€ํ•œ ๋ชจ๋“  ๊ฒŒ์‹œ๋ฌผ์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๋ถ„๋ฅ˜ ์šฉ์–ด๋กœ ์ •๋ ฌ ๋œ ์•„์นด์ด๋ธŒ๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ๋งŒ๋“ญ๋‹ˆ๋‹ค. ์ด๋ฅผ ๊ฐ€๋Šฅํ•˜๊ฒŒํ•˜๋Š” ์ฝ”๋“œ :

  // Get your terms and put them into an array
  $issue_terms = get_terms([
    'taxonomy' => 'issues',
    'hide_empty' => false,
  ]);

  // Run foreach over each term to setup query and display for posts
  foreach ($issue_terms as $issue_term) {
    $the_query = new WP_Query( array(
      'post_type' => 'post',
      'tax_query' => array(
        array(
          'taxonomy' => 'issues',
          'field' => 'slug',
          'terms' => array( $issue_term->slug ),
          'operator' => 'IN'
        )
      )
    ) );

    // Run loop over each query
    while($the_query->have_posts()) :
      $the_query->the_post();

      // YOUR TEMPLATE OUTPUT FOR EACH POST

    endwhile;
  }

์ด ์‚ฌ์ดํŠธ ๊ด€๋ จ ์ฝ๊ธฐ :
๋ชจ๋“  ๊ฒŒ์‹œ๋ฌผ์„ ์‚ฌ์šฉ์ž ์ •์˜ ๋ถ„๋ฅ˜๋กœ ๊ทธ๋ฃนํ™”ํ•˜์—ฌ ์‚ฌ์šฉ์ž ์ •์˜ ๊ฒŒ์‹œ๋ฌผ ์œ ํ˜•์œผ๋กœ ํ‘œ์‹œ


๋‹ต๋ณ€

์—ฌ๊ธฐ์˜ ๋ชจ๋“  ์†”๋ฃจ์…˜์ด ์™œ ๊ทธ๋ ‡๊ฒŒ ๋งŽ์ด ๊ณผ์ž‰ ์ฒ˜๋ฆฌํ•˜๋Š”์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ข‹์•„, ๊ทธ๊ฒƒ์€ 10 ๋…„ ์ „์ด์ง€๋งŒ ํ˜„์žฌ ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ ์ค‘์ด๋ฉฐ ์ž‘๋™ํ•ฉ๋‹ˆ๋‹ค.

   <?php // Default
    $wheels_args = array(
        'post_type' => 'wheels',
        'posts_per_page' => '96',
        'orderby' => 'taxonomy, name', // Just enter 2 parameters here, seprated by comma
        'order'=>'ASC'
    );
    $loop = new WP_Query($wheels_args);
    ?>

์ด๋ ‡๊ฒŒํ•˜๋ฉด ๋จผ์ € CPT ๋ถ„๋ฅ˜ ์ฒด๊ณ„๊ฐ€ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ๋ถ„๋ฅ˜๋˜๋ฉฐ ๋ถ„๋ฅ˜ ์ฒด๊ณ„ ๊ทธ๋ฃน ๋‚ด์—์„œ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ •๋ ฌ๋ฉ๋‹ˆ๋‹ค.