taxQuery.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  1. <?php
  2. /**
  3. * @group query
  4. * @group taxonomy
  5. */
  6. class Tests_Query_TaxQuery extends WP_UnitTestCase {
  7. public function test_tax_query_single_query_single_term_field_slug() {
  8. $t = self::factory()->term->create(
  9. array(
  10. 'taxonomy' => 'category',
  11. 'slug' => 'foo',
  12. 'name' => 'Foo',
  13. )
  14. );
  15. $p1 = self::factory()->post->create();
  16. $p2 = self::factory()->post->create();
  17. wp_set_post_terms( $p1, $t, 'category' );
  18. $q = new WP_Query(
  19. array(
  20. 'fields' => 'ids',
  21. 'update_post_meta_cache' => false,
  22. 'update_post_term_cache' => false,
  23. 'tax_query' => array(
  24. array(
  25. 'taxonomy' => 'category',
  26. 'terms' => array( 'foo' ),
  27. 'field' => 'slug',
  28. ),
  29. ),
  30. )
  31. );
  32. $this->assertSame( array( $p1 ), $q->posts );
  33. }
  34. public function test_tax_query_single_query_single_term_field_name() {
  35. $t = self::factory()->term->create(
  36. array(
  37. 'taxonomy' => 'category',
  38. 'slug' => 'foo',
  39. 'name' => 'Foo',
  40. )
  41. );
  42. $p1 = self::factory()->post->create();
  43. $p2 = self::factory()->post->create();
  44. wp_set_post_terms( $p1, $t, 'category' );
  45. $q = new WP_Query(
  46. array(
  47. 'fields' => 'ids',
  48. 'update_post_meta_cache' => false,
  49. 'update_post_term_cache' => false,
  50. 'tax_query' => array(
  51. array(
  52. 'taxonomy' => 'category',
  53. 'terms' => array( 'Foo' ),
  54. 'field' => 'name',
  55. ),
  56. ),
  57. )
  58. );
  59. $this->assertSame( array( $p1 ), $q->posts );
  60. }
  61. /**
  62. * @ticket 27810
  63. */
  64. public function test_field_name_should_work_for_names_with_spaces() {
  65. register_taxonomy( 'wptests_tax', 'post' );
  66. $t = self::factory()->term->create(
  67. array(
  68. 'taxonomy' => 'wptests_tax',
  69. 'slug' => 'foo',
  70. 'name' => 'Foo Bar',
  71. )
  72. );
  73. $p1 = self::factory()->post->create();
  74. $p2 = self::factory()->post->create();
  75. wp_set_object_terms( $p1, $t, 'wptests_tax' );
  76. $q = new WP_Query(
  77. array(
  78. 'fields' => 'ids',
  79. 'tax_query' => array(
  80. array(
  81. 'taxonomy' => 'wptests_tax',
  82. 'terms' => array( 'Foo Bar' ),
  83. 'field' => 'name',
  84. ),
  85. ),
  86. )
  87. );
  88. $this->assertSame( array( $p1 ), $q->posts );
  89. }
  90. public function test_tax_query_single_query_single_term_field_term_taxonomy_id() {
  91. $t = self::factory()->term->create(
  92. array(
  93. 'taxonomy' => 'category',
  94. 'slug' => 'foo',
  95. 'name' => 'Foo',
  96. )
  97. );
  98. $p1 = self::factory()->post->create();
  99. $p2 = self::factory()->post->create();
  100. $tt_ids = wp_set_post_terms( $p1, $t, 'category' );
  101. $q = new WP_Query(
  102. array(
  103. 'fields' => 'ids',
  104. 'update_post_meta_cache' => false,
  105. 'update_post_term_cache' => false,
  106. 'tax_query' => array(
  107. array(
  108. 'taxonomy' => 'category',
  109. 'terms' => $tt_ids,
  110. 'field' => 'term_taxonomy_id',
  111. ),
  112. ),
  113. )
  114. );
  115. $this->assertSame( array( $p1 ), $q->posts );
  116. }
  117. public function test_tax_query_single_query_single_term_field_term_id() {
  118. $t = self::factory()->term->create(
  119. array(
  120. 'taxonomy' => 'category',
  121. 'slug' => 'foo',
  122. 'name' => 'Foo',
  123. )
  124. );
  125. $p1 = self::factory()->post->create();
  126. $p2 = self::factory()->post->create();
  127. wp_set_post_terms( $p1, $t, 'category' );
  128. $q = new WP_Query(
  129. array(
  130. 'fields' => 'ids',
  131. 'update_post_meta_cache' => false,
  132. 'update_post_term_cache' => false,
  133. 'tax_query' => array(
  134. array(
  135. 'taxonomy' => 'category',
  136. 'terms' => array( $t ),
  137. 'field' => 'term_id',
  138. ),
  139. ),
  140. )
  141. );
  142. $this->assertSame( array( $p1 ), $q->posts );
  143. }
  144. public function test_tax_query_single_query_single_term_operator_in() {
  145. $t = self::factory()->term->create(
  146. array(
  147. 'taxonomy' => 'category',
  148. 'slug' => 'foo',
  149. 'name' => 'Foo',
  150. )
  151. );
  152. $p1 = self::factory()->post->create();
  153. $p2 = self::factory()->post->create();
  154. wp_set_post_terms( $p1, $t, 'category' );
  155. $q = new WP_Query(
  156. array(
  157. 'fields' => 'ids',
  158. 'update_post_meta_cache' => false,
  159. 'update_post_term_cache' => false,
  160. 'tax_query' => array(
  161. array(
  162. 'taxonomy' => 'category',
  163. 'terms' => array( 'foo' ),
  164. 'field' => 'slug',
  165. 'operator' => 'IN',
  166. ),
  167. ),
  168. )
  169. );
  170. $this->assertSame( array( $p1 ), $q->posts );
  171. }
  172. public function test_tax_query_single_query_single_term_operator_not_in() {
  173. $t = self::factory()->term->create(
  174. array(
  175. 'taxonomy' => 'category',
  176. 'slug' => 'foo',
  177. 'name' => 'Foo',
  178. )
  179. );
  180. $p1 = self::factory()->post->create();
  181. $p2 = self::factory()->post->create();
  182. wp_set_post_terms( $p1, $t, 'category' );
  183. $q = new WP_Query(
  184. array(
  185. 'fields' => 'ids',
  186. 'update_post_meta_cache' => false,
  187. 'update_post_term_cache' => false,
  188. 'tax_query' => array(
  189. array(
  190. 'taxonomy' => 'category',
  191. 'terms' => array( 'foo' ),
  192. 'field' => 'slug',
  193. 'operator' => 'NOT IN',
  194. ),
  195. ),
  196. )
  197. );
  198. $this->assertSame( array( $p2 ), $q->posts );
  199. }
  200. public function test_tax_query_single_query_single_term_operator_and() {
  201. $t = self::factory()->term->create(
  202. array(
  203. 'taxonomy' => 'category',
  204. 'slug' => 'foo',
  205. 'name' => 'Foo',
  206. )
  207. );
  208. $p1 = self::factory()->post->create();
  209. $p2 = self::factory()->post->create();
  210. wp_set_post_terms( $p1, $t, 'category' );
  211. $q = new WP_Query(
  212. array(
  213. 'fields' => 'ids',
  214. 'update_post_meta_cache' => false,
  215. 'update_post_term_cache' => false,
  216. 'tax_query' => array(
  217. array(
  218. 'taxonomy' => 'category',
  219. 'terms' => array( 'foo' ),
  220. 'field' => 'slug',
  221. 'operator' => 'AND',
  222. ),
  223. ),
  224. )
  225. );
  226. $this->assertSame( array( $p1 ), $q->posts );
  227. }
  228. public function test_tax_query_single_query_multiple_terms_operator_in() {
  229. $t1 = self::factory()->term->create(
  230. array(
  231. 'taxonomy' => 'category',
  232. 'slug' => 'foo',
  233. 'name' => 'Foo',
  234. )
  235. );
  236. $t2 = self::factory()->term->create(
  237. array(
  238. 'taxonomy' => 'category',
  239. 'slug' => 'bar',
  240. 'name' => 'Bar',
  241. )
  242. );
  243. $p1 = self::factory()->post->create();
  244. $p2 = self::factory()->post->create();
  245. $p3 = self::factory()->post->create();
  246. wp_set_post_terms( $p1, $t1, 'category' );
  247. wp_set_post_terms( $p2, $t2, 'category' );
  248. $q = new WP_Query(
  249. array(
  250. 'fields' => 'ids',
  251. 'update_post_meta_cache' => false,
  252. 'update_post_term_cache' => false,
  253. 'tax_query' => array(
  254. array(
  255. 'taxonomy' => 'category',
  256. 'terms' => array( 'foo', 'bar' ),
  257. 'field' => 'slug',
  258. 'operator' => 'IN',
  259. ),
  260. ),
  261. )
  262. );
  263. $this->assertSameSets( array( $p1, $p2 ), $q->posts );
  264. }
  265. public function test_tax_query_single_query_multiple_terms_operator_not_in() {
  266. $t1 = self::factory()->term->create(
  267. array(
  268. 'taxonomy' => 'category',
  269. 'slug' => 'foo',
  270. 'name' => 'Foo',
  271. )
  272. );
  273. $t2 = self::factory()->term->create(
  274. array(
  275. 'taxonomy' => 'category',
  276. 'slug' => 'bar',
  277. 'name' => 'Bar',
  278. )
  279. );
  280. $p1 = self::factory()->post->create();
  281. $p2 = self::factory()->post->create();
  282. $p3 = self::factory()->post->create();
  283. wp_set_post_terms( $p1, $t1, 'category' );
  284. wp_set_post_terms( $p2, $t2, 'category' );
  285. $q = new WP_Query(
  286. array(
  287. 'fields' => 'ids',
  288. 'update_post_meta_cache' => false,
  289. 'update_post_term_cache' => false,
  290. 'tax_query' => array(
  291. array(
  292. 'taxonomy' => 'category',
  293. 'terms' => array( 'foo', 'bar' ),
  294. 'field' => 'slug',
  295. 'operator' => 'NOT IN',
  296. ),
  297. ),
  298. )
  299. );
  300. $this->assertSame( array( $p3 ), $q->posts );
  301. }
  302. /**
  303. * @ticket 18105
  304. */
  305. public function test_tax_query_single_query_multiple_queries_operator_not_in() {
  306. $t1 = self::factory()->term->create(
  307. array(
  308. 'taxonomy' => 'category',
  309. 'slug' => 'foo',
  310. 'name' => 'Foo',
  311. )
  312. );
  313. $t2 = self::factory()->term->create(
  314. array(
  315. 'taxonomy' => 'category',
  316. 'slug' => 'bar',
  317. 'name' => 'Bar',
  318. )
  319. );
  320. $p1 = self::factory()->post->create();
  321. $p2 = self::factory()->post->create();
  322. $p3 = self::factory()->post->create();
  323. wp_set_post_terms( $p1, $t1, 'category' );
  324. wp_set_post_terms( $p2, $t2, 'category' );
  325. $q = new WP_Query(
  326. array(
  327. 'fields' => 'ids',
  328. 'update_post_meta_cache' => false,
  329. 'update_post_term_cache' => false,
  330. 'tax_query' => array(
  331. 'relation' => 'AND',
  332. array(
  333. 'taxonomy' => 'category',
  334. 'terms' => array( 'foo' ),
  335. 'field' => 'slug',
  336. 'operator' => 'NOT IN',
  337. ),
  338. array(
  339. 'taxonomy' => 'category',
  340. 'terms' => array( 'bar' ),
  341. 'field' => 'slug',
  342. 'operator' => 'NOT IN',
  343. ),
  344. ),
  345. )
  346. );
  347. $this->assertSame( array( $p3 ), $q->posts );
  348. }
  349. public function test_tax_query_single_query_multiple_terms_operator_and() {
  350. $t1 = self::factory()->term->create(
  351. array(
  352. 'taxonomy' => 'category',
  353. 'slug' => 'foo',
  354. 'name' => 'Foo',
  355. )
  356. );
  357. $t2 = self::factory()->term->create(
  358. array(
  359. 'taxonomy' => 'category',
  360. 'slug' => 'bar',
  361. 'name' => 'Bar',
  362. )
  363. );
  364. $p1 = self::factory()->post->create();
  365. $p2 = self::factory()->post->create();
  366. $p3 = self::factory()->post->create();
  367. wp_set_object_terms( $p1, $t1, 'category' );
  368. wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' );
  369. $q = new WP_Query(
  370. array(
  371. 'fields' => 'ids',
  372. 'update_post_meta_cache' => false,
  373. 'update_post_term_cache' => false,
  374. 'tax_query' => array(
  375. array(
  376. 'taxonomy' => 'category',
  377. 'terms' => array( 'foo', 'bar' ),
  378. 'field' => 'slug',
  379. 'operator' => 'AND',
  380. ),
  381. ),
  382. )
  383. );
  384. $this->assertSame( array( $p2 ), $q->posts );
  385. }
  386. /**
  387. * @ticket 29181
  388. */
  389. public function test_tax_query_operator_not_exists() {
  390. register_taxonomy( 'wptests_tax1', 'post' );
  391. register_taxonomy( 'wptests_tax2', 'post' );
  392. $t1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
  393. $t2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
  394. $p1 = self::factory()->post->create();
  395. $p2 = self::factory()->post->create();
  396. $p3 = self::factory()->post->create();
  397. wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' );
  398. wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' );
  399. $q = new WP_Query(
  400. array(
  401. 'fields' => 'ids',
  402. 'orderby' => 'ID',
  403. 'order' => 'ASC',
  404. 'tax_query' => array(
  405. array(
  406. 'taxonomy' => 'wptests_tax2',
  407. 'operator' => 'NOT EXISTS',
  408. ),
  409. ),
  410. )
  411. );
  412. $this->assertSameSets( array( $p1, $p3 ), $q->posts );
  413. }
  414. /**
  415. * @ticket 36343
  416. */
  417. public function test_tax_query_operator_not_exists_combined() {
  418. register_post_type( 'wptests_cpt1' );
  419. register_taxonomy( 'wptests_tax1', 'wptests_cpt1' );
  420. register_taxonomy( 'wptests_tax2', 'wptests_cpt1' );
  421. $t1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
  422. $t2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
  423. $t3 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
  424. $p1 = self::factory()->post->create( array( 'post_type' => 'wptests_cpt1' ) );
  425. $p2 = self::factory()->post->create( array( 'post_type' => 'wptests_cpt1' ) );
  426. $p3 = self::factory()->post->create( array( 'post_type' => 'wptests_cpt1' ) );
  427. $p4 = self::factory()->post->create( array( 'post_type' => 'wptests_cpt1' ) );
  428. wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' );
  429. wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax1' );
  430. wp_set_object_terms( $p3, array( $t3 ), 'wptests_tax2' );
  431. $q = new WP_Query(
  432. array(
  433. 'post_type' => 'wptests_cpt1',
  434. 'fields' => 'ids',
  435. 'tax_query' => array(
  436. 'relation' => 'OR',
  437. array(
  438. 'taxonomy' => 'wptests_tax1',
  439. 'operator' => 'NOT EXISTS',
  440. ),
  441. array(
  442. 'taxonomy' => 'wptests_tax1',
  443. 'field' => 'slug',
  444. 'terms' => get_term_field( 'slug', $t1 ),
  445. ),
  446. ),
  447. )
  448. );
  449. unregister_post_type( 'wptests_cpt1' );
  450. unregister_taxonomy( 'wptests_tax1' );
  451. unregister_taxonomy( 'wptests_tax2' );
  452. $this->assertSameSets( array( $p1, $p3, $p4 ), $q->posts );
  453. }
  454. /**
  455. * @ticket 29181
  456. */
  457. public function test_tax_query_operator_exists() {
  458. register_taxonomy( 'wptests_tax1', 'post' );
  459. register_taxonomy( 'wptests_tax2', 'post' );
  460. $t1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
  461. $t2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
  462. $p1 = self::factory()->post->create();
  463. $p2 = self::factory()->post->create();
  464. $p3 = self::factory()->post->create();
  465. wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' );
  466. wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' );
  467. $q = new WP_Query(
  468. array(
  469. 'fields' => 'ids',
  470. 'orderby' => 'ID',
  471. 'order' => 'ASC',
  472. 'tax_query' => array(
  473. array(
  474. 'taxonomy' => 'wptests_tax2',
  475. 'operator' => 'EXISTS',
  476. ),
  477. ),
  478. )
  479. );
  480. $this->assertSameSets( array( $p2 ), $q->posts );
  481. }
  482. /**
  483. * @ticket 29181
  484. */
  485. public function test_tax_query_operator_exists_should_ignore_terms() {
  486. register_taxonomy( 'wptests_tax1', 'post' );
  487. register_taxonomy( 'wptests_tax2', 'post' );
  488. $t1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
  489. $t2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
  490. $p1 = self::factory()->post->create();
  491. $p2 = self::factory()->post->create();
  492. $p3 = self::factory()->post->create();
  493. wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' );
  494. wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' );
  495. $q = new WP_Query(
  496. array(
  497. 'fields' => 'ids',
  498. 'orderby' => 'ID',
  499. 'order' => 'ASC',
  500. 'tax_query' => array(
  501. array(
  502. 'taxonomy' => 'wptests_tax2',
  503. 'operator' => 'EXISTS',
  504. 'terms' => array( 'foo', 'bar' ),
  505. ),
  506. ),
  507. )
  508. );
  509. $this->assertSameSets( array( $p2 ), $q->posts );
  510. }
  511. /**
  512. * @ticket 29181
  513. */
  514. public function test_tax_query_operator_exists_with_no_taxonomy() {
  515. register_taxonomy( 'wptests_tax1', 'post' );
  516. register_taxonomy( 'wptests_tax2', 'post' );
  517. $t1 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax1' ) );
  518. $t2 = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax2' ) );
  519. $p1 = self::factory()->post->create();
  520. $p2 = self::factory()->post->create();
  521. $p3 = self::factory()->post->create();
  522. wp_set_object_terms( $p1, array( $t1 ), 'wptests_tax1' );
  523. wp_set_object_terms( $p2, array( $t2 ), 'wptests_tax2' );
  524. $q = new WP_Query(
  525. array(
  526. 'fields' => 'ids',
  527. 'orderby' => 'ID',
  528. 'order' => 'ASC',
  529. 'tax_query' => array(
  530. array(
  531. 'operator' => 'EXISTS',
  532. ),
  533. ),
  534. )
  535. );
  536. $this->assertEmpty( $q->posts );
  537. }
  538. public function test_tax_query_multiple_queries_relation_and() {
  539. $t1 = self::factory()->term->create(
  540. array(
  541. 'taxonomy' => 'category',
  542. 'slug' => 'foo',
  543. 'name' => 'Foo',
  544. )
  545. );
  546. $t2 = self::factory()->term->create(
  547. array(
  548. 'taxonomy' => 'category',
  549. 'slug' => 'bar',
  550. 'name' => 'Bar',
  551. )
  552. );
  553. $p1 = self::factory()->post->create();
  554. $p2 = self::factory()->post->create();
  555. $p3 = self::factory()->post->create();
  556. wp_set_object_terms( $p1, $t1, 'category' );
  557. wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' );
  558. $q = new WP_Query(
  559. array(
  560. 'fields' => 'ids',
  561. 'update_post_meta_cache' => false,
  562. 'update_post_term_cache' => false,
  563. 'tax_query' => array(
  564. 'relation' => 'AND',
  565. array(
  566. 'taxonomy' => 'category',
  567. 'terms' => array( 'foo' ),
  568. 'field' => 'slug',
  569. ),
  570. array(
  571. 'taxonomy' => 'category',
  572. 'terms' => array( 'bar' ),
  573. 'field' => 'slug',
  574. ),
  575. ),
  576. )
  577. );
  578. $this->assertSame( array( $p2 ), $q->posts );
  579. }
  580. public function test_tax_query_multiple_queries_relation_or() {
  581. $t1 = self::factory()->term->create(
  582. array(
  583. 'taxonomy' => 'category',
  584. 'slug' => 'foo',
  585. 'name' => 'Foo',
  586. )
  587. );
  588. $t2 = self::factory()->term->create(
  589. array(
  590. 'taxonomy' => 'category',
  591. 'slug' => 'bar',
  592. 'name' => 'Bar',
  593. )
  594. );
  595. $p1 = self::factory()->post->create();
  596. $p2 = self::factory()->post->create();
  597. $p3 = self::factory()->post->create();
  598. wp_set_object_terms( $p1, $t1, 'category' );
  599. wp_set_object_terms( $p2, array( $t1, $t2 ), 'category' );
  600. $q = new WP_Query(
  601. array(
  602. 'fields' => 'ids',
  603. 'update_post_meta_cache' => false,
  604. 'update_post_term_cache' => false,
  605. 'tax_query' => array(
  606. 'relation' => 'OR',
  607. array(
  608. 'taxonomy' => 'category',
  609. 'terms' => array( 'foo' ),
  610. 'field' => 'slug',
  611. ),
  612. array(
  613. 'taxonomy' => 'category',
  614. 'terms' => array( 'bar' ),
  615. 'field' => 'slug',
  616. ),
  617. ),
  618. )
  619. );
  620. $this->assertSameSets( array( $p1, $p2 ), $q->posts );
  621. }
  622. public function test_tax_query_multiple_queries_different_taxonomies() {
  623. $t1 = self::factory()->term->create(
  624. array(
  625. 'taxonomy' => 'post_tag',
  626. 'slug' => 'foo',
  627. 'name' => 'Foo',
  628. )
  629. );
  630. $t2 = self::factory()->term->create(
  631. array(
  632. 'taxonomy' => 'category',
  633. 'slug' => 'bar',
  634. 'name' => 'Bar',
  635. )
  636. );
  637. $p1 = self::factory()->post->create();
  638. $p2 = self::factory()->post->create();
  639. $p3 = self::factory()->post->create();
  640. wp_set_object_terms( $p1, $t1, 'post_tag' );
  641. wp_set_object_terms( $p2, $t2, 'category' );
  642. $q = new WP_Query(
  643. array(
  644. 'fields' => 'ids',
  645. 'update_post_meta_cache' => false,
  646. 'update_post_term_cache' => false,
  647. 'tax_query' => array(
  648. 'relation' => 'OR',
  649. array(
  650. 'taxonomy' => 'post_tag',
  651. 'terms' => array( 'foo' ),
  652. 'field' => 'slug',
  653. ),
  654. array(
  655. 'taxonomy' => 'category',
  656. 'terms' => array( 'bar' ),
  657. 'field' => 'slug',
  658. ),
  659. ),
  660. )
  661. );
  662. $this->assertSameSets( array( $p1, $p2 ), $q->posts );
  663. }
  664. /**
  665. * @ticket 29738
  666. */
  667. public function test_tax_query_two_nested_queries() {
  668. register_taxonomy( 'foo', 'post' );
  669. register_taxonomy( 'bar', 'post' );
  670. $foo_term_1 = self::factory()->term->create(
  671. array(
  672. 'taxonomy' => 'foo',
  673. )
  674. );
  675. $foo_term_2 = self::factory()->term->create(
  676. array(
  677. 'taxonomy' => 'foo',
  678. )
  679. );
  680. $bar_term_1 = self::factory()->term->create(
  681. array(
  682. 'taxonomy' => 'bar',
  683. )
  684. );
  685. $bar_term_2 = self::factory()->term->create(
  686. array(
  687. 'taxonomy' => 'bar',
  688. )
  689. );
  690. $p1 = self::factory()->post->create();
  691. $p2 = self::factory()->post->create();
  692. $p3 = self::factory()->post->create();
  693. wp_set_object_terms( $p1, array( $foo_term_1 ), 'foo' );
  694. wp_set_object_terms( $p1, array( $bar_term_1 ), 'bar' );
  695. wp_set_object_terms( $p2, array( $foo_term_2 ), 'foo' );
  696. wp_set_object_terms( $p2, array( $bar_term_2 ), 'bar' );
  697. wp_set_object_terms( $p3, array( $foo_term_1 ), 'foo' );
  698. wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' );
  699. $q = new WP_Query(
  700. array(
  701. 'fields' => 'ids',
  702. 'update_post_meta_cache' => false,
  703. 'update_post_term_cache' => false,
  704. 'tax_query' => array(
  705. 'relation' => 'OR',
  706. array(
  707. 'relation' => 'AND',
  708. array(
  709. 'taxonomy' => 'foo',
  710. 'terms' => array( $foo_term_1 ),
  711. 'field' => 'term_id',
  712. ),
  713. array(
  714. 'taxonomy' => 'bar',
  715. 'terms' => array( $bar_term_1 ),
  716. 'field' => 'term_id',
  717. ),
  718. ),
  719. array(
  720. 'relation' => 'AND',
  721. array(
  722. 'taxonomy' => 'foo',
  723. 'terms' => array( $foo_term_2 ),
  724. 'field' => 'term_id',
  725. ),
  726. array(
  727. 'taxonomy' => 'bar',
  728. 'terms' => array( $bar_term_2 ),
  729. 'field' => 'term_id',
  730. ),
  731. ),
  732. ),
  733. )
  734. );
  735. _unregister_taxonomy( 'foo' );
  736. _unregister_taxonomy( 'bar' );
  737. $this->assertSameSets( array( $p1, $p2 ), $q->posts );
  738. }
  739. /**
  740. * @ticket 29738
  741. */
  742. public function test_tax_query_one_nested_query_one_first_order_query() {
  743. register_taxonomy( 'foo', 'post' );
  744. register_taxonomy( 'bar', 'post' );
  745. $foo_term_1 = self::factory()->term->create(
  746. array(
  747. 'taxonomy' => 'foo',
  748. )
  749. );
  750. $foo_term_2 = self::factory()->term->create(
  751. array(
  752. 'taxonomy' => 'foo',
  753. )
  754. );
  755. $bar_term_1 = self::factory()->term->create(
  756. array(
  757. 'taxonomy' => 'bar',
  758. )
  759. );
  760. $bar_term_2 = self::factory()->term->create(
  761. array(
  762. 'taxonomy' => 'bar',
  763. )
  764. );
  765. $p1 = self::factory()->post->create();
  766. $p2 = self::factory()->post->create();
  767. $p3 = self::factory()->post->create();
  768. wp_set_object_terms( $p1, array( $foo_term_1 ), 'foo' );
  769. wp_set_object_terms( $p1, array( $bar_term_1 ), 'bar' );
  770. wp_set_object_terms( $p2, array( $foo_term_2 ), 'foo' );
  771. wp_set_object_terms( $p2, array( $bar_term_2 ), 'bar' );
  772. wp_set_object_terms( $p3, array( $foo_term_1 ), 'foo' );
  773. wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' );
  774. $q = new WP_Query(
  775. array(
  776. 'fields' => 'ids',
  777. 'update_post_meta_cache' => false,
  778. 'update_post_term_cache' => false,
  779. 'tax_query' => array(
  780. 'relation' => 'OR',
  781. array(
  782. 'taxonomy' => 'foo',
  783. 'terms' => array( $foo_term_2 ),
  784. 'field' => 'term_id',
  785. ),
  786. array(
  787. 'relation' => 'AND',
  788. array(
  789. 'taxonomy' => 'foo',
  790. 'terms' => array( $foo_term_1 ),
  791. 'field' => 'term_id',
  792. ),
  793. array(
  794. 'taxonomy' => 'bar',
  795. 'terms' => array( $bar_term_1 ),
  796. 'field' => 'term_id',
  797. ),
  798. ),
  799. ),
  800. )
  801. );
  802. _unregister_taxonomy( 'foo' );
  803. _unregister_taxonomy( 'bar' );
  804. $this->assertSameSets( array( $p1, $p2 ), $q->posts );
  805. }
  806. /**
  807. * @ticket 29738
  808. */
  809. public function test_tax_query_one_double_nested_query_one_first_order_query() {
  810. register_taxonomy( 'foo', 'post' );
  811. register_taxonomy( 'bar', 'post' );
  812. $foo_term_1 = self::factory()->term->create(
  813. array(
  814. 'taxonomy' => 'foo',
  815. )
  816. );
  817. $foo_term_2 = self::factory()->term->create(
  818. array(
  819. 'taxonomy' => 'foo',
  820. )
  821. );
  822. $bar_term_1 = self::factory()->term->create(
  823. array(
  824. 'taxonomy' => 'bar',
  825. )
  826. );
  827. $bar_term_2 = self::factory()->term->create(
  828. array(
  829. 'taxonomy' => 'bar',
  830. )
  831. );
  832. $p1 = self::factory()->post->create();
  833. $p2 = self::factory()->post->create();
  834. $p3 = self::factory()->post->create();
  835. $p4 = self::factory()->post->create();
  836. wp_set_object_terms( $p1, array( $foo_term_1 ), 'foo' );
  837. wp_set_object_terms( $p1, array( $bar_term_1 ), 'bar' );
  838. wp_set_object_terms( $p2, array( $foo_term_2 ), 'foo' );
  839. wp_set_object_terms( $p2, array( $bar_term_2 ), 'bar' );
  840. wp_set_object_terms( $p3, array( $foo_term_1 ), 'foo' );
  841. wp_set_object_terms( $p3, array( $bar_term_2 ), 'bar' );
  842. $q = new WP_Query(
  843. array(
  844. 'fields' => 'ids',
  845. 'update_post_meta_cache' => false,
  846. 'update_post_term_cache' => false,
  847. 'tax_query' => array(
  848. 'relation' => 'OR',
  849. array(
  850. 'taxonomy' => 'foo',
  851. 'terms' => array( $foo_term_2 ),
  852. 'field' => 'term_id',
  853. ),
  854. array(
  855. 'relation' => 'AND',
  856. array(
  857. 'taxonomy' => 'foo',
  858. 'terms' => array( $foo_term_1 ),
  859. 'field' => 'term_id',
  860. ),
  861. array(
  862. 'relation' => 'OR',
  863. array(
  864. 'taxonomy' => 'bar',
  865. 'terms' => array( $bar_term_1 ),
  866. 'field' => 'term_id',
  867. ),
  868. array(
  869. 'taxonomy' => 'bar',
  870. 'terms' => array( $bar_term_2 ),
  871. 'field' => 'term_id',
  872. ),
  873. ),
  874. ),
  875. ),
  876. )
  877. );
  878. _unregister_taxonomy( 'foo' );
  879. _unregister_taxonomy( 'bar' );
  880. $this->assertSameSets( array( $p1, $p2, $p3 ), $q->posts );
  881. }
  882. /**
  883. * An empty tax query should return an empty array, not all posts.
  884. *
  885. * @ticket 20604
  886. */
  887. public function test_tax_query_relation_or_both_clauses_empty_terms() {
  888. self::factory()->post->create_many( 2 );
  889. $query = new WP_Query(
  890. array(
  891. 'fields' => 'ids',
  892. 'update_post_term_cache' => false,
  893. 'update_post_meta_cache' => false,
  894. 'tax_query' => array(
  895. 'relation' => 'OR',
  896. array(
  897. 'taxonomy' => 'post_tag',
  898. 'field' => 'id',
  899. 'terms' => false,
  900. 'operator' => 'IN',
  901. ),
  902. array(
  903. 'taxonomy' => 'category',
  904. 'field' => 'id',
  905. 'terms' => false,
  906. 'operator' => 'IN',
  907. ),
  908. ),
  909. )
  910. );
  911. $posts = $query->get_posts();
  912. $this->assertSame( 0, count( $posts ) );
  913. }
  914. /**
  915. * An empty tax query should return an empty array, not all posts.
  916. *
  917. * @ticket 20604
  918. */
  919. public function test_tax_query_relation_or_one_clause_empty_terms() {
  920. self::factory()->post->create_many( 2 );
  921. $query = new WP_Query(
  922. array(
  923. 'fields' => 'ids',
  924. 'update_post_term_cache' => false,
  925. 'update_post_meta_cache' => false,
  926. 'tax_query' => array(
  927. 'relation' => 'OR',
  928. array(
  929. 'taxonomy' => 'post_tag',
  930. 'field' => 'id',
  931. 'terms' => array( 'foo' ),
  932. 'operator' => 'IN',
  933. ),
  934. array(
  935. 'taxonomy' => 'category',
  936. 'field' => 'id',
  937. 'terms' => false,
  938. 'operator' => 'IN',
  939. ),
  940. ),
  941. )
  942. );
  943. $posts = $query->get_posts();
  944. $this->assertSame( 0, count( $posts ) );
  945. }
  946. public function test_tax_query_include_children() {
  947. $cat_a = self::factory()->term->create(
  948. array(
  949. 'taxonomy' => 'category',
  950. 'name' => 'Australia',
  951. )
  952. );
  953. $cat_b = self::factory()->term->create(
  954. array(
  955. 'taxonomy' => 'category',
  956. 'name' => 'Sydney',
  957. 'parent' => $cat_a,
  958. )
  959. );
  960. $cat_c = self::factory()->term->create(
  961. array(
  962. 'taxonomy' => 'category',
  963. 'name' => 'East Syndney',
  964. 'parent' => $cat_b,
  965. )
  966. );
  967. $cat_d = self::factory()->term->create(
  968. array(
  969. 'taxonomy' => 'category',
  970. 'name' => 'West Syndney',
  971. 'parent' => $cat_b,
  972. )
  973. );
  974. $post_a = self::factory()->post->create( array( 'post_category' => array( $cat_a ) ) );
  975. $post_b = self::factory()->post->create( array( 'post_category' => array( $cat_b ) ) );
  976. $post_c = self::factory()->post->create( array( 'post_category' => array( $cat_c ) ) );
  977. $post_d = self::factory()->post->create( array( 'post_category' => array( $cat_d ) ) );
  978. $posts = get_posts(
  979. array(
  980. 'fields' => 'ids',
  981. 'update_post_meta_cache' => false,
  982. 'update_post_term_cache' => false,
  983. 'tax_query' => array(
  984. array(
  985. 'taxonomy' => 'category',
  986. 'field' => 'id',
  987. 'terms' => array( $cat_a ),
  988. ),
  989. ),
  990. )
  991. );
  992. $this->assertSame( 4, count( $posts ) );
  993. $posts = get_posts(
  994. array(
  995. 'fields' => 'ids',
  996. 'update_post_meta_cache' => false,
  997. 'update_post_term_cache' => false,
  998. 'tax_query' => array(
  999. array(
  1000. 'taxonomy' => 'category',
  1001. 'field' => 'id',
  1002. 'terms' => array( $cat_a ),
  1003. 'include_children' => false,
  1004. ),
  1005. ),
  1006. )
  1007. );
  1008. $this->assertSame( 1, count( $posts ) );
  1009. $posts = get_posts(
  1010. array(
  1011. 'fields' => 'ids',
  1012. 'update_post_meta_cache' => false,
  1013. 'update_post_term_cache' => false,
  1014. 'tax_query' => array(
  1015. array(
  1016. 'taxonomy' => 'category',
  1017. 'field' => 'id',
  1018. 'terms' => array( $cat_b ),
  1019. ),
  1020. ),
  1021. )
  1022. );
  1023. $this->assertSame( 3, count( $posts ) );
  1024. $posts = get_posts(
  1025. array(
  1026. 'fields' => 'ids',
  1027. 'update_post_meta_cache' => false,
  1028. 'update_post_term_cache' => false,
  1029. 'tax_query' => array(
  1030. array(
  1031. 'taxonomy' => 'category',
  1032. 'field' => 'id',
  1033. 'terms' => array( $cat_b ),
  1034. 'include_children' => false,
  1035. ),
  1036. ),
  1037. )
  1038. );
  1039. $this->assertSame( 1, count( $posts ) );
  1040. $posts = get_posts(
  1041. array(
  1042. 'fields' => 'ids',
  1043. 'update_post_meta_cache' => false,
  1044. 'update_post_term_cache' => false,
  1045. 'tax_query' => array(
  1046. array(
  1047. 'taxonomy' => 'category',
  1048. 'field' => 'id',
  1049. 'terms' => array( $cat_c ),
  1050. ),
  1051. ),
  1052. )
  1053. );
  1054. $this->assertSame( 1, count( $posts ) );
  1055. $posts = get_posts(
  1056. array(
  1057. 'fields' => 'ids',
  1058. 'update_post_meta_cache' => false,
  1059. 'update_post_term_cache' => false,
  1060. 'tax_query' => array(
  1061. array(
  1062. 'taxonomy' => 'category',
  1063. 'field' => 'id',
  1064. 'terms' => array( $cat_c ),
  1065. 'include_children' => false,
  1066. ),
  1067. ),
  1068. )
  1069. );
  1070. $this->assertSame( 1, count( $posts ) );
  1071. }
  1072. public function test_tax_query_taxonomy_with_attachments() {
  1073. $q = new WP_Query();
  1074. register_taxonomy_for_object_type( 'post_tag', 'attachment:image' );
  1075. $tag_id = self::factory()->term->create(
  1076. array(
  1077. 'slug' => rand_str(),
  1078. 'name' => rand_str(),
  1079. )
  1080. );
  1081. $image_id = self::factory()->attachment->create_object(
  1082. 'image.jpg',
  1083. 0,
  1084. array(
  1085. 'post_mime_type' => 'image/jpeg',
  1086. 'post_type' => 'attachment',
  1087. )
  1088. );
  1089. wp_set_object_terms( $image_id, $tag_id, 'post_tag' );
  1090. $posts = $q->query(
  1091. array(
  1092. 'fields' => 'ids',
  1093. 'update_post_meta_cache' => false,
  1094. 'update_post_term_cache' => false,
  1095. 'post_type' => 'attachment',
  1096. 'post_status' => 'inherit',
  1097. 'tax_query' => array(
  1098. array(
  1099. 'taxonomy' => 'post_tag',
  1100. 'field' => 'term_id',
  1101. 'terms' => array( $tag_id ),
  1102. ),
  1103. ),
  1104. )
  1105. );
  1106. $this->assertSame( array( $image_id ), $posts );
  1107. }
  1108. public function test_tax_query_no_taxonomy() {
  1109. $cat_id = self::factory()->category->create( array( 'name' => 'alpha' ) );
  1110. self::factory()->post->create(
  1111. array(
  1112. 'post_title' => 'alpha',
  1113. 'post_category' => array( $cat_id ),
  1114. )
  1115. );
  1116. $response1 = new WP_Query(
  1117. array(
  1118. 'tax_query' => array(
  1119. array( 'terms' => array( $cat_id ) ),
  1120. ),
  1121. )
  1122. );
  1123. $this->assertEmpty( $response1->posts );
  1124. $response2 = new WP_Query(
  1125. array(
  1126. 'fields' => 'ids',
  1127. 'update_post_meta_cache' => false,
  1128. 'update_post_term_cache' => false,
  1129. 'tax_query' => array(
  1130. array(
  1131. 'taxonomy' => 'category',
  1132. 'terms' => array( $cat_id ),
  1133. ),
  1134. ),
  1135. )
  1136. );
  1137. $this->assertNotEmpty( $response2->posts );
  1138. $term = get_category( $cat_id );
  1139. $response3 = new WP_Query(
  1140. array(
  1141. 'fields' => 'ids',
  1142. 'update_post_meta_cache' => false,
  1143. 'update_post_term_cache' => false,
  1144. 'tax_query' => array(
  1145. array(
  1146. 'field' => 'term_taxonomy_id',
  1147. 'terms' => array( $term->term_taxonomy_id ),
  1148. ),
  1149. ),
  1150. )
  1151. );
  1152. $this->assertNotEmpty( $response3->posts );
  1153. }
  1154. public function test_term_taxonomy_id_field_no_taxonomy() {
  1155. $q = new WP_Query();
  1156. $posts = self::factory()->post->create_many( 5 );
  1157. $cats = array();
  1158. $tags = array();
  1159. // Need term_taxonomy_ids in addition to term_ids, so no factory.
  1160. for ( $i = 0; $i < 5; $i++ ) {
  1161. $cats[ $i ] = wp_insert_term( 'category-' . $i, 'category' );
  1162. $tags[ $i ] = wp_insert_term( 'tag-' . $i, 'post_tag' );
  1163. // Post 0 gets all terms.
  1164. wp_set_object_terms( $posts[0], array( $cats[ $i ]['term_id'] ), 'category', true );
  1165. wp_set_object_terms( $posts[0], array( $tags[ $i ]['term_id'] ), 'post_tag', true );
  1166. }
  1167. wp_set_object_terms( $posts[1], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' );
  1168. wp_set_object_terms( $posts[1], array( $tags[0]['term_id'], $tags[2]['term_id'], $cats[4]['term_id'] ), 'post_tag' );
  1169. wp_set_object_terms( $posts[2], array( $cats[1]['term_id'], $cats[3]['term_id'] ), 'category' );
  1170. wp_set_object_terms( $posts[2], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' );
  1171. wp_set_object_terms( $posts[3], array( $cats[0]['term_id'], $cats[2]['term_id'], $cats[4]['term_id'] ), 'category' );
  1172. wp_set_object_terms( $posts[3], array( $tags[1]['term_id'], $tags[3]['term_id'] ), 'post_tag' );
  1173. $results1 = $q->query(
  1174. array(
  1175. 'fields' => 'ids',
  1176. 'update_post_meta_cache' => false,
  1177. 'update_post_term_cache' => false,
  1178. 'orderby' => 'ID',
  1179. 'order' => 'ASC',
  1180. 'tax_query' => array(
  1181. 'relation' => 'OR',
  1182. array(
  1183. 'field' => 'term_taxonomy_id',
  1184. 'terms' => array( $cats[0]['term_taxonomy_id'], $cats[2]['term_taxonomy_id'], $cats[4]['term_taxonomy_id'], $tags[0]['term_taxonomy_id'], $tags[2]['term_taxonomy_id'], $cats[4]['term_taxonomy_id'] ),
  1185. 'operator' => 'AND',
  1186. 'include_children' => false,
  1187. ),
  1188. array(
  1189. 'field' => 'term_taxonomy_id',
  1190. 'terms' => array( $cats[1]['term_taxonomy_id'], $cats[3]['term_taxonomy_id'], $tags[1]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ),
  1191. 'operator' => 'AND',
  1192. 'include_children' => false,
  1193. ),
  1194. ),
  1195. )
  1196. );
  1197. $this->assertSame( array( $posts[0], $posts[1], $posts[2] ), $results1, 'Relation: OR; Operator: AND' );
  1198. $results2 = $q->query(
  1199. array(
  1200. 'fields' => 'ids',
  1201. 'update_post_meta_cache' => false,
  1202. 'update_post_term_cache' => false,
  1203. 'orderby' => 'ID',
  1204. 'order' => 'ASC',
  1205. 'tax_query' => array(
  1206. 'relation' => 'AND',
  1207. array(
  1208. 'field' => 'term_taxonomy_id',
  1209. 'terms' => array( $cats[0]['term_taxonomy_id'], $tags[0]['term_taxonomy_id'] ),
  1210. 'operator' => 'IN',
  1211. 'include_children' => false,
  1212. ),
  1213. array(
  1214. 'field' => 'term_taxonomy_id',
  1215. 'terms' => array( $cats[3]['term_taxonomy_id'], $tags[3]['term_taxonomy_id'] ),
  1216. 'operator' => 'IN',
  1217. 'include_children' => false,
  1218. ),
  1219. ),
  1220. )
  1221. );
  1222. $this->assertSame( array( $posts[0], $posts[3] ), $results2, 'Relation: AND; Operator: IN' );
  1223. }
  1224. /**
  1225. * @ticket 29738
  1226. */
  1227. public function test_populate_taxonomy_query_var_from_tax_query() {
  1228. register_taxonomy( 'foo', 'post' );
  1229. $t = self::factory()->term->create(
  1230. array(
  1231. 'taxonomy' => 'foo',
  1232. )
  1233. );
  1234. $c = self::factory()->term->create(
  1235. array(
  1236. 'taxonomy' => 'category',
  1237. )
  1238. );
  1239. $q = new WP_Query(
  1240. array(
  1241. 'tax_query' => array(
  1242. // Empty terms mean that this one should be skipped.
  1243. array(
  1244. 'taxonomy' => 'bar',
  1245. 'terms' => array(),
  1246. ),
  1247. // Category and post tags should be skipped.
  1248. array(
  1249. 'taxonomy' => 'category',
  1250. 'terms' => array( $c ),
  1251. ),
  1252. array(
  1253. 'taxonomy' => 'foo',
  1254. 'terms' => array( $t ),
  1255. ),
  1256. ),
  1257. )
  1258. );
  1259. $this->assertSame( 'foo', $q->get( 'taxonomy' ) );
  1260. _unregister_taxonomy( 'foo' );
  1261. }
  1262. public function test_populate_taxonomy_query_var_from_tax_query_taxonomy_already_set() {
  1263. register_taxonomy( 'foo', 'post' );
  1264. register_taxonomy( 'foo1', 'post' );
  1265. $t = self::factory()->term->create(
  1266. array(
  1267. 'taxonomy' => 'foo',
  1268. )
  1269. );
  1270. $q = new WP_Query(
  1271. array(
  1272. 'taxonomy' => 'bar',
  1273. 'tax_query' => array(
  1274. array(
  1275. 'taxonomy' => 'foo',
  1276. 'terms' => array( $t ),
  1277. ),
  1278. ),
  1279. )
  1280. );
  1281. $this->assertSame( 'bar', $q->get( 'taxonomy' ) );
  1282. _unregister_taxonomy( 'foo' );
  1283. _unregister_taxonomy( 'foo1' );
  1284. }
  1285. public function test_populate_term_query_var_from_tax_query() {
  1286. register_taxonomy( 'foo', 'post' );
  1287. $t = self::factory()->term->create(
  1288. array(
  1289. 'taxonomy' => 'foo',
  1290. 'slug' => 'bar',
  1291. )
  1292. );
  1293. $q = new WP_Query(
  1294. array(
  1295. 'tax_query' => array(
  1296. array(
  1297. 'taxonomy' => 'foo',
  1298. 'terms' => array( 'bar' ),
  1299. 'field' => 'slug',
  1300. ),
  1301. ),
  1302. )
  1303. );
  1304. $this->assertSame( 'bar', $q->get( 'term' ) );
  1305. _unregister_taxonomy( 'foo' );
  1306. }
  1307. public function test_populate_term_id_query_var_from_tax_query() {
  1308. register_taxonomy( 'foo', 'post' );
  1309. $t = self::factory()->term->create(
  1310. array(
  1311. 'taxonomy' => 'foo',
  1312. 'slug' => 'bar',
  1313. )
  1314. );
  1315. $q = new WP_Query(
  1316. array(
  1317. 'tax_query' => array(
  1318. array(
  1319. 'taxonomy' => 'foo',
  1320. 'terms' => array( $t ),
  1321. 'field' => 'term_id',
  1322. ),
  1323. ),
  1324. )
  1325. );
  1326. $this->assertSame( $t, $q->get( 'term_id' ) );
  1327. _unregister_taxonomy( 'foo' );
  1328. }
  1329. /**
  1330. * @ticket 29738
  1331. */
  1332. public function test_populate_cat_category_name_query_var_from_tax_query() {
  1333. register_taxonomy( 'foo', 'post' );
  1334. $t = self::factory()->term->create(
  1335. array(
  1336. 'taxonomy' => 'foo',
  1337. )
  1338. );
  1339. $c = self::factory()->term->create(
  1340. array(
  1341. 'taxonomy' => 'category',
  1342. 'slug' => 'bar',
  1343. )
  1344. );
  1345. $q = new WP_Query(
  1346. array(
  1347. 'tax_query' => array(
  1348. // Non-category should be skipped.
  1349. array(
  1350. 'taxonomy' => 'foo',
  1351. 'terms' => array( $t ),
  1352. ),
  1353. // Empty terms mean that this one should be skipped.
  1354. array(
  1355. 'taxonomy' => 'category',
  1356. 'terms' => array(),
  1357. ),
  1358. // Category and post tags should be skipped.
  1359. array(
  1360. 'taxonomy' => 'category',
  1361. 'terms' => array( $c ),
  1362. ),
  1363. ),
  1364. )
  1365. );
  1366. $this->assertSame( $c, $q->get( 'cat' ) );
  1367. $this->assertSame( 'bar', $q->get( 'category_name' ) );
  1368. _unregister_taxonomy( 'foo' );
  1369. }
  1370. /**
  1371. * @ticket 29738
  1372. */
  1373. public function test_populate_tag_id_query_var_from_tax_query() {
  1374. register_taxonomy( 'foo', 'post' );
  1375. $t = self::factory()->term->create(
  1376. array(
  1377. 'taxonomy' => 'foo',
  1378. )
  1379. );
  1380. $tag = self::factory()->term->create(
  1381. array(
  1382. 'taxonomy' => 'post_tag',
  1383. 'slug' => 'bar',
  1384. )
  1385. );
  1386. $q = new WP_Query(
  1387. array(
  1388. 'tax_query' => array(
  1389. // Non-tag should be skipped.
  1390. array(
  1391. 'taxonomy' => 'foo',
  1392. 'terms' => array( $t ),
  1393. ),
  1394. // Empty terms mean that this one should be skipped.
  1395. array(
  1396. 'taxonomy' => 'post_tag',
  1397. 'terms' => array(),
  1398. ),
  1399. // Category and post tags should be skipped.
  1400. array(
  1401. 'taxonomy' => 'post_tag',
  1402. 'terms' => array( $tag ),
  1403. ),
  1404. ),
  1405. )
  1406. );
  1407. $this->assertSame( $tag, $q->get( 'tag_id' ) );
  1408. _unregister_taxonomy( 'foo' );
  1409. }
  1410. /**
  1411. * @ticket 39315
  1412. */
  1413. public function test_tax_terms_should_not_be_double_escaped() {
  1414. $name = "Don't worry be happy";
  1415. register_taxonomy( 'wptests_tax', 'post' );
  1416. $t = self::factory()->term->create(
  1417. array(
  1418. 'taxonomy' => 'wptests_tax',
  1419. 'name' => $name,
  1420. )
  1421. );
  1422. $p = self::factory()->post->create();
  1423. wp_set_object_terms( $p, array( $t ), 'wptests_tax' );
  1424. $q = new WP_Query(
  1425. array(
  1426. 'fields' => 'ids',
  1427. 'tax_query' => array(
  1428. array(
  1429. 'taxonomy' => 'wptests_tax',
  1430. 'field' => 'name',
  1431. 'terms' => $name,
  1432. ),
  1433. ),
  1434. )
  1435. );
  1436. $this->assertSameSets( array( $p ), $q->posts );
  1437. }
  1438. }