class-wp-query.php 121 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093
  1. <?php
  2. /**
  3. * Query API: WP_Query class
  4. *
  5. * @package WordPress
  6. * @subpackage Query
  7. * @since 4.7.0
  8. */
  9. /**
  10. * The WordPress Query class.
  11. *
  12. * @link https://codex.wordpress.org/Function_Reference/WP_Query Codex page.
  13. *
  14. * @since 1.5.0
  15. * @since 4.5.0 Removed the `$comments_popup` property.
  16. */
  17. class WP_Query {
  18. /**
  19. * Query vars set by the user
  20. *
  21. * @since 1.5.0
  22. * @var array
  23. */
  24. public $query;
  25. /**
  26. * Query vars, after parsing
  27. *
  28. * @since 1.5.0
  29. * @var array
  30. */
  31. public $query_vars = array();
  32. /**
  33. * Taxonomy query, as passed to get_tax_sql()
  34. *
  35. * @since 3.1.0
  36. * @var object WP_Tax_Query
  37. */
  38. public $tax_query;
  39. /**
  40. * Metadata query container
  41. *
  42. * @since 3.2.0
  43. * @var object WP_Meta_Query
  44. */
  45. public $meta_query = false;
  46. /**
  47. * Date query container
  48. *
  49. * @since 3.7.0
  50. * @var object WP_Date_Query
  51. */
  52. public $date_query = false;
  53. /**
  54. * Holds the data for a single object that is queried.
  55. *
  56. * Holds the contents of a post, page, category, attachment.
  57. *
  58. * @since 1.5.0
  59. * @var object|array
  60. */
  61. public $queried_object;
  62. /**
  63. * The ID of the queried object.
  64. *
  65. * @since 1.5.0
  66. * @var int
  67. */
  68. public $queried_object_id;
  69. /**
  70. * Get post database query.
  71. *
  72. * @since 2.0.1
  73. * @var string
  74. */
  75. public $request;
  76. /**
  77. * List of posts.
  78. *
  79. * @since 1.5.0
  80. * @var array
  81. */
  82. public $posts;
  83. /**
  84. * The amount of posts for the current query.
  85. *
  86. * @since 1.5.0
  87. * @var int
  88. */
  89. public $post_count = 0;
  90. /**
  91. * Index of the current item in the loop.
  92. *
  93. * @since 1.5.0
  94. * @var int
  95. */
  96. public $current_post = -1;
  97. /**
  98. * Whether the loop has started and the caller is in the loop.
  99. *
  100. * @since 2.0.0
  101. * @var bool
  102. */
  103. public $in_the_loop = false;
  104. /**
  105. * The current post.
  106. *
  107. * @since 1.5.0
  108. * @var WP_Post
  109. */
  110. public $post;
  111. /**
  112. * The list of comments for current post.
  113. *
  114. * @since 2.2.0
  115. * @var array
  116. */
  117. public $comments;
  118. /**
  119. * The amount of comments for the posts.
  120. *
  121. * @since 2.2.0
  122. * @var int
  123. */
  124. public $comment_count = 0;
  125. /**
  126. * The index of the comment in the comment loop.
  127. *
  128. * @since 2.2.0
  129. * @var int
  130. */
  131. public $current_comment = -1;
  132. /**
  133. * Current comment ID.
  134. *
  135. * @since 2.2.0
  136. * @var int
  137. */
  138. public $comment;
  139. /**
  140. * The amount of found posts for the current query.
  141. *
  142. * If limit clause was not used, equals $post_count.
  143. *
  144. * @since 2.1.0
  145. * @var int
  146. */
  147. public $found_posts = 0;
  148. /**
  149. * The amount of pages.
  150. *
  151. * @since 2.1.0
  152. * @var int
  153. */
  154. public $max_num_pages = 0;
  155. /**
  156. * The amount of comment pages.
  157. *
  158. * @since 2.7.0
  159. * @var int
  160. */
  161. public $max_num_comment_pages = 0;
  162. /**
  163. * Signifies whether the current query is for a single post.
  164. *
  165. * @since 1.5.0
  166. * @var bool
  167. */
  168. public $is_single = false;
  169. /**
  170. * Signifies whether the current query is for a preview.
  171. *
  172. * @since 2.0.0
  173. * @var bool
  174. */
  175. public $is_preview = false;
  176. /**
  177. * Signifies whether the current query is for a page.
  178. *
  179. * @since 1.5.0
  180. * @var bool
  181. */
  182. public $is_page = false;
  183. /**
  184. * Signifies whether the current query is for an archive.
  185. *
  186. * @since 1.5.0
  187. * @var bool
  188. */
  189. public $is_archive = false;
  190. /**
  191. * Signifies whether the current query is for a date archive.
  192. *
  193. * @since 1.5.0
  194. * @var bool
  195. */
  196. public $is_date = false;
  197. /**
  198. * Signifies whether the current query is for a year archive.
  199. *
  200. * @since 1.5.0
  201. * @var bool
  202. */
  203. public $is_year = false;
  204. /**
  205. * Signifies whether the current query is for a month archive.
  206. *
  207. * @since 1.5.0
  208. * @var bool
  209. */
  210. public $is_month = false;
  211. /**
  212. * Signifies whether the current query is for a day archive.
  213. *
  214. * @since 1.5.0
  215. * @var bool
  216. */
  217. public $is_day = false;
  218. /**
  219. * Signifies whether the current query is for a specific time.
  220. *
  221. * @since 1.5.0
  222. * @var bool
  223. */
  224. public $is_time = false;
  225. /**
  226. * Signifies whether the current query is for an author archive.
  227. *
  228. * @since 1.5.0
  229. * @var bool
  230. */
  231. public $is_author = false;
  232. /**
  233. * Signifies whether the current query is for a category archive.
  234. *
  235. * @since 1.5.0
  236. * @var bool
  237. */
  238. public $is_category = false;
  239. /**
  240. * Signifies whether the current query is for a tag archive.
  241. *
  242. * @since 2.3.0
  243. * @var bool
  244. */
  245. public $is_tag = false;
  246. /**
  247. * Signifies whether the current query is for a taxonomy archive.
  248. *
  249. * @since 2.5.0
  250. * @var bool
  251. */
  252. public $is_tax = false;
  253. /**
  254. * Signifies whether the current query is for a search.
  255. *
  256. * @since 1.5.0
  257. * @var bool
  258. */
  259. public $is_search = false;
  260. /**
  261. * Signifies whether the current query is for a feed.
  262. *
  263. * @since 1.5.0
  264. * @var bool
  265. */
  266. public $is_feed = false;
  267. /**
  268. * Signifies whether the current query is for a comment feed.
  269. *
  270. * @since 2.2.0
  271. * @var bool
  272. */
  273. public $is_comment_feed = false;
  274. /**
  275. * Signifies whether the current query is for trackback endpoint call.
  276. *
  277. * @since 1.5.0
  278. * @var bool
  279. */
  280. public $is_trackback = false;
  281. /**
  282. * Signifies whether the current query is for the site homepage.
  283. *
  284. * @since 1.5.0
  285. * @var bool
  286. */
  287. public $is_home = false;
  288. /**
  289. * Signifies whether the current query couldn't find anything.
  290. *
  291. * @since 1.5.0
  292. * @var bool
  293. */
  294. public $is_404 = false;
  295. /**
  296. * Signifies whether the current query is for an embed.
  297. *
  298. * @since 4.4.0
  299. * @var bool
  300. */
  301. public $is_embed = false;
  302. /**
  303. * Signifies whether the current query is for a paged result and not for the first page.
  304. *
  305. * @since 1.5.0
  306. * @var bool
  307. */
  308. public $is_paged = false;
  309. /**
  310. * Signifies whether the current query is for an administrative interface page.
  311. *
  312. * @since 1.5.0
  313. * @var bool
  314. */
  315. public $is_admin = false;
  316. /**
  317. * Signifies whether the current query is for an attachment page.
  318. *
  319. * @since 2.0.0
  320. * @var bool
  321. */
  322. public $is_attachment = false;
  323. /**
  324. * Signifies whether the current query is for an existing single post of any post type
  325. * (post, attachment, page, custom post types).
  326. *
  327. * @since 2.1.0
  328. * @var bool
  329. */
  330. public $is_singular = false;
  331. /**
  332. * Signifies whether the current query is for the robots.txt file.
  333. *
  334. * @since 2.1.0
  335. * @var bool
  336. */
  337. public $is_robots = false;
  338. /**
  339. * Signifies whether the current query is for the page_for_posts page.
  340. *
  341. * Basically, the homepage if the option isn't set for the static homepage.
  342. *
  343. * @since 2.1.0
  344. * @var bool
  345. */
  346. public $is_posts_page = false;
  347. /**
  348. * Signifies whether the current query is for a post type archive.
  349. *
  350. * @since 3.1.0
  351. * @var bool
  352. */
  353. public $is_post_type_archive = false;
  354. /**
  355. * Stores the ->query_vars state like md5(serialize( $this->query_vars ) ) so we know
  356. * whether we have to re-parse because something has changed
  357. *
  358. * @since 3.1.0
  359. * @var bool|string
  360. */
  361. private $query_vars_hash = false;
  362. /**
  363. * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made
  364. * via pre_get_posts hooks.
  365. *
  366. * @since 3.1.1
  367. */
  368. private $query_vars_changed = true;
  369. /**
  370. * Set if post thumbnails are cached
  371. *
  372. * @since 3.2.0
  373. * @var bool
  374. */
  375. public $thumbnails_cached = false;
  376. /**
  377. * Cached list of search stopwords.
  378. *
  379. * @since 3.7.0
  380. * @var array
  381. */
  382. private $stopwords;
  383. private $compat_fields = array( 'query_vars_hash', 'query_vars_changed' );
  384. private $compat_methods = array( 'init_query_flags', 'parse_tax_query' );
  385. /**
  386. * Resets query flags to false.
  387. *
  388. * The query flags are what page info WordPress was able to figure out.
  389. *
  390. * @since 2.0.0
  391. */
  392. private function init_query_flags() {
  393. $this->is_single = false;
  394. $this->is_preview = false;
  395. $this->is_page = false;
  396. $this->is_archive = false;
  397. $this->is_date = false;
  398. $this->is_year = false;
  399. $this->is_month = false;
  400. $this->is_day = false;
  401. $this->is_time = false;
  402. $this->is_author = false;
  403. $this->is_category = false;
  404. $this->is_tag = false;
  405. $this->is_tax = false;
  406. $this->is_search = false;
  407. $this->is_feed = false;
  408. $this->is_comment_feed = false;
  409. $this->is_trackback = false;
  410. $this->is_home = false;
  411. $this->is_404 = false;
  412. $this->is_paged = false;
  413. $this->is_admin = false;
  414. $this->is_attachment = false;
  415. $this->is_singular = false;
  416. $this->is_robots = false;
  417. $this->is_posts_page = false;
  418. $this->is_post_type_archive = false;
  419. }
  420. /**
  421. * Initiates object properties and sets default values.
  422. *
  423. * @since 1.5.0
  424. */
  425. public function init() {
  426. unset($this->posts);
  427. unset($this->query);
  428. $this->query_vars = array();
  429. unset($this->queried_object);
  430. unset($this->queried_object_id);
  431. $this->post_count = 0;
  432. $this->current_post = -1;
  433. $this->in_the_loop = false;
  434. unset( $this->request );
  435. unset( $this->post );
  436. unset( $this->comments );
  437. unset( $this->comment );
  438. $this->comment_count = 0;
  439. $this->current_comment = -1;
  440. $this->found_posts = 0;
  441. $this->max_num_pages = 0;
  442. $this->max_num_comment_pages = 0;
  443. $this->init_query_flags();
  444. }
  445. /**
  446. * Reparse the query vars.
  447. *
  448. * @since 1.5.0
  449. */
  450. public function parse_query_vars() {
  451. $this->parse_query();
  452. }
  453. /**
  454. * Fills in the query variables, which do not exist within the parameter.
  455. *
  456. * @since 2.1.0
  457. * @since 4.4.0 Removed the `comments_popup` public query variable.
  458. *
  459. * @param array $array Defined query variables.
  460. * @return array Complete query variables with undefined ones filled in empty.
  461. */
  462. public function fill_query_vars($array) {
  463. $keys = array(
  464. 'error'
  465. , 'm'
  466. , 'p'
  467. , 'post_parent'
  468. , 'subpost'
  469. , 'subpost_id'
  470. , 'attachment'
  471. , 'attachment_id'
  472. , 'name'
  473. , 'pagename'
  474. , 'page_id'
  475. , 'second'
  476. , 'minute'
  477. , 'hour'
  478. , 'day'
  479. , 'monthnum'
  480. , 'year'
  481. , 'w'
  482. , 'category_name'
  483. , 'tag'
  484. , 'cat'
  485. , 'tag_id'
  486. , 'author'
  487. , 'author_name'
  488. , 'feed'
  489. , 'tb'
  490. , 'paged'
  491. , 'meta_key'
  492. , 'meta_value'
  493. , 'preview'
  494. , 's'
  495. , 'sentence'
  496. , 'title'
  497. , 'fields'
  498. , 'menu_order'
  499. , 'embed'
  500. );
  501. foreach ( $keys as $key ) {
  502. if ( !isset($array[$key]) )
  503. $array[$key] = '';
  504. }
  505. $array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in', 'post_name__in',
  506. 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'post_parent__in', 'post_parent__not_in',
  507. 'author__in', 'author__not_in' );
  508. foreach ( $array_keys as $key ) {
  509. if ( !isset($array[$key]) )
  510. $array[$key] = array();
  511. }
  512. return $array;
  513. }
  514. /**
  515. * Parse a query string and set query type booleans.
  516. *
  517. * @since 1.5.0
  518. * @since 4.2.0 Introduced the ability to order by specific clauses of a `$meta_query`, by passing the clause's
  519. * array key to `$orderby`.
  520. * @since 4.4.0 Introduced `$post_name__in` and `$title` parameters. `$s` was updated to support excluded
  521. * search terms, by prepending a hyphen.
  522. * @since 4.5.0 Removed the `$comments_popup` parameter.
  523. * Introduced the `$comment_status` and `$ping_status` parameters.
  524. * Introduced `RAND(x)` syntax for `$orderby`, which allows an integer seed value to random sorts.
  525. * @since 4.6.0 Added 'post_name__in' support for `$orderby`. Introduced the `$lazy_load_term_meta` argument.
  526. * @since 4.9.0 Introduced the `$comment_count` parameter.
  527. *
  528. * @param string|array $query {
  529. * Optional. Array or string of Query parameters.
  530. *
  531. * @type int $attachment_id Attachment post ID. Used for 'attachment' post_type.
  532. * @type int|string $author Author ID, or comma-separated list of IDs.
  533. * @type string $author_name User 'user_nicename'.
  534. * @type array $author__in An array of author IDs to query from.
  535. * @type array $author__not_in An array of author IDs not to query from.
  536. * @type bool $cache_results Whether to cache post information. Default true.
  537. * @type int|string $cat Category ID or comma-separated list of IDs (this or any children).
  538. * @type array $category__and An array of category IDs (AND in).
  539. * @type array $category__in An array of category IDs (OR in, no children).
  540. * @type array $category__not_in An array of category IDs (NOT in).
  541. * @type string $category_name Use category slug (not name, this or any children).
  542. * @type array|int $comment_count Filter results by comment count. Provide an integer to match
  543. * comment count exactly. Provide an array with integer 'value'
  544. * and 'compare' operator ('=', '!=', '>', '>=', '<', '<=' ) to
  545. * compare against comment_count in a specific way.
  546. * @type string $comment_status Comment status.
  547. * @type int $comments_per_page The number of comments to return per page.
  548. * Default 'comments_per_page' option.
  549. * @type array $date_query An associative array of WP_Date_Query arguments.
  550. * See WP_Date_Query::__construct().
  551. * @type int $day Day of the month. Default empty. Accepts numbers 1-31.
  552. * @type bool $exact Whether to search by exact keyword. Default false.
  553. * @type string|array $fields Which fields to return. Single field or all fields (string),
  554. * or array of fields. 'id=>parent' uses 'id' and 'post_parent'.
  555. * Default all fields. Accepts 'ids', 'id=>parent'.
  556. * @type int $hour Hour of the day. Default empty. Accepts numbers 0-23.
  557. * @type int|bool $ignore_sticky_posts Whether to ignore sticky posts or not. Setting this to false
  558. * excludes stickies from 'post__in'. Accepts 1|true, 0|false.
  559. * Default 0|false.
  560. * @type int $m Combination YearMonth. Accepts any four-digit year and month
  561. * numbers 1-12. Default empty.
  562. * @type string $meta_compare Comparison operator to test the 'meta_value'.
  563. * @type string $meta_key Custom field key.
  564. * @type array $meta_query An associative array of WP_Meta_Query arguments. See WP_Meta_Query.
  565. * @type string $meta_value Custom field value.
  566. * @type int $meta_value_num Custom field value number.
  567. * @type int $menu_order The menu order of the posts.
  568. * @type int $monthnum The two-digit month. Default empty. Accepts numbers 1-12.
  569. * @type string $name Post slug.
  570. * @type bool $nopaging Show all posts (true) or paginate (false). Default false.
  571. * @type bool $no_found_rows Whether to skip counting the total rows found. Enabling can improve
  572. * performance. Default false.
  573. * @type int $offset The number of posts to offset before retrieval.
  574. * @type string $order Designates ascending or descending order of posts. Default 'DESC'.
  575. * Accepts 'ASC', 'DESC'.
  576. * @type string|array $orderby Sort retrieved posts by parameter. One or more options may be
  577. * passed. To use 'meta_value', or 'meta_value_num',
  578. * 'meta_key=keyname' must be also be defined. To sort by a
  579. * specific `$meta_query` clause, use that clause's array key.
  580. * Accepts 'none', 'name', 'author', 'date', 'title',
  581. * 'modified', 'menu_order', 'parent', 'ID', 'rand',
  582. * 'relevance', 'RAND(x)' (where 'x' is an integer seed value),
  583. * 'comment_count', 'meta_value', 'meta_value_num', 'post__in',
  584. * 'post_name__in', 'post_parent__in', and the array keys
  585. * of `$meta_query`. Default is 'date', except when a search
  586. * is being performed, when the default is 'relevance'.
  587. *
  588. * @type int $p Post ID.
  589. * @type int $page Show the number of posts that would show up on page X of a
  590. * static front page.
  591. * @type int $paged The number of the current page.
  592. * @type int $page_id Page ID.
  593. * @type string $pagename Page slug.
  594. * @type string $perm Show posts if user has the appropriate capability.
  595. * @type string $ping_status Ping status.
  596. * @type array $post__in An array of post IDs to retrieve, sticky posts will be included
  597. * @type string $post_mime_type The mime type of the post. Used for 'attachment' post_type.
  598. * @type array $post__not_in An array of post IDs not to retrieve. Note: a string of comma-
  599. * separated IDs will NOT work.
  600. * @type int $post_parent Page ID to retrieve child pages for. Use 0 to only retrieve
  601. * top-level pages.
  602. * @type array $post_parent__in An array containing parent page IDs to query child pages from.
  603. * @type array $post_parent__not_in An array containing parent page IDs not to query child pages from.
  604. * @type string|array $post_type A post type slug (string) or array of post type slugs.
  605. * Default 'any' if using 'tax_query'.
  606. * @type string|array $post_status A post status (string) or array of post statuses.
  607. * @type int $posts_per_page The number of posts to query for. Use -1 to request all posts.
  608. * @type int $posts_per_archive_page The number of posts to query for by archive page. Overrides
  609. * 'posts_per_page' when is_archive(), or is_search() are true.
  610. * @type array $post_name__in An array of post slugs that results must match.
  611. * @type string $s Search keyword(s). Prepending a term with a hyphen will
  612. * exclude posts matching that term. Eg, 'pillow -sofa' will
  613. * return posts containing 'pillow' but not 'sofa'. The
  614. * character used for exclusion can be modified using the
  615. * the 'wp_query_search_exclusion_prefix' filter.
  616. * @type int $second Second of the minute. Default empty. Accepts numbers 0-60.
  617. * @type bool $sentence Whether to search by phrase. Default false.
  618. * @type bool $suppress_filters Whether to suppress filters. Default false.
  619. * @type string $tag Tag slug. Comma-separated (either), Plus-separated (all).
  620. * @type array $tag__and An array of tag ids (AND in).
  621. * @type array $tag__in An array of tag ids (OR in).
  622. * @type array $tag__not_in An array of tag ids (NOT in).
  623. * @type int $tag_id Tag id or comma-separated list of IDs.
  624. * @type array $tag_slug__and An array of tag slugs (AND in).
  625. * @type array $tag_slug__in An array of tag slugs (OR in). unless 'ignore_sticky_posts' is
  626. * true. Note: a string of comma-separated IDs will NOT work.
  627. * @type array $tax_query An associative array of WP_Tax_Query arguments.
  628. * See WP_Tax_Query->queries.
  629. * @type string $title Post title.
  630. * @type bool $update_post_meta_cache Whether to update the post meta cache. Default true.
  631. * @type bool $update_post_term_cache Whether to update the post term cache. Default true.
  632. * @type bool $lazy_load_term_meta Whether to lazy-load term meta. Setting to false will
  633. * disable cache priming for term meta, so that each
  634. * get_term_meta() call will hit the database.
  635. * Defaults to the value of `$update_post_term_cache`.
  636. * @type int $w The week number of the year. Default empty. Accepts numbers 0-53.
  637. * @type int $year The four-digit year. Default empty. Accepts any four-digit year.
  638. * }
  639. */
  640. public function parse_query( $query = '' ) {
  641. if ( ! empty( $query ) ) {
  642. $this->init();
  643. $this->query = $this->query_vars = wp_parse_args( $query );
  644. } elseif ( ! isset( $this->query ) ) {
  645. $this->query = $this->query_vars;
  646. }
  647. $this->query_vars = $this->fill_query_vars($this->query_vars);
  648. $qv = &$this->query_vars;
  649. $this->query_vars_changed = true;
  650. if ( ! empty($qv['robots']) )
  651. $this->is_robots = true;
  652. if ( ! is_scalar( $qv['p'] ) || $qv['p'] < 0 ) {
  653. $qv['p'] = 0;
  654. $qv['error'] = '404';
  655. } else {
  656. $qv['p'] = intval( $qv['p'] );
  657. }
  658. $qv['page_id'] = absint($qv['page_id']);
  659. $qv['year'] = absint($qv['year']);
  660. $qv['monthnum'] = absint($qv['monthnum']);
  661. $qv['day'] = absint($qv['day']);
  662. $qv['w'] = absint($qv['w']);
  663. $qv['m'] = is_scalar( $qv['m'] ) ? preg_replace( '|[^0-9]|', '', $qv['m'] ) : '';
  664. $qv['paged'] = absint($qv['paged']);
  665. $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers
  666. $qv['author'] = preg_replace( '|[^0-9,-]|', '', $qv['author'] ); // comma separated list of positive or negative integers
  667. $qv['pagename'] = trim( $qv['pagename'] );
  668. $qv['name'] = trim( $qv['name'] );
  669. $qv['title'] = trim( $qv['title'] );
  670. if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']);
  671. if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
  672. if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
  673. if ( '' !== $qv['menu_order'] ) $qv['menu_order'] = absint($qv['menu_order']);
  674. // Fairly insane upper bound for search string lengths.
  675. if ( ! is_scalar( $qv['s'] ) || ( ! empty( $qv['s'] ) && strlen( $qv['s'] ) > 1600 ) ) {
  676. $qv['s'] = '';
  677. }
  678. // Compat. Map subpost to attachment.
  679. if ( '' != $qv['subpost'] )
  680. $qv['attachment'] = $qv['subpost'];
  681. if ( '' != $qv['subpost_id'] )
  682. $qv['attachment_id'] = $qv['subpost_id'];
  683. $qv['attachment_id'] = absint($qv['attachment_id']);
  684. if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) {
  685. $this->is_single = true;
  686. $this->is_attachment = true;
  687. } elseif ( '' != $qv['name'] ) {
  688. $this->is_single = true;
  689. } elseif ( $qv['p'] ) {
  690. $this->is_single = true;
  691. } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) {
  692. // If year, month, day, hour, minute, and second are set, a single
  693. // post is being queried.
  694. $this->is_single = true;
  695. } elseif ( '' != $qv['pagename'] || !empty($qv['page_id']) ) {
  696. $this->is_page = true;
  697. $this->is_single = false;
  698. } else {
  699. // Look for archive queries. Dates, categories, authors, search, post type archives.
  700. if ( isset( $this->query['s'] ) ) {
  701. $this->is_search = true;
  702. }
  703. if ( '' !== $qv['second'] ) {
  704. $this->is_time = true;
  705. $this->is_date = true;
  706. }
  707. if ( '' !== $qv['minute'] ) {
  708. $this->is_time = true;
  709. $this->is_date = true;
  710. }
  711. if ( '' !== $qv['hour'] ) {
  712. $this->is_time = true;
  713. $this->is_date = true;
  714. }
  715. if ( $qv['day'] ) {
  716. if ( ! $this->is_date ) {
  717. $date = sprintf( '%04d-%02d-%02d', $qv['year'], $qv['monthnum'], $qv['day'] );
  718. if ( $qv['monthnum'] && $qv['year'] && ! wp_checkdate( $qv['monthnum'], $qv['day'], $qv['year'], $date ) ) {
  719. $qv['error'] = '404';
  720. } else {
  721. $this->is_day = true;
  722. $this->is_date = true;
  723. }
  724. }
  725. }
  726. if ( $qv['monthnum'] ) {
  727. if ( ! $this->is_date ) {
  728. if ( 12 < $qv['monthnum'] ) {
  729. $qv['error'] = '404';
  730. } else {
  731. $this->is_month = true;
  732. $this->is_date = true;
  733. }
  734. }
  735. }
  736. if ( $qv['year'] ) {
  737. if ( ! $this->is_date ) {
  738. $this->is_year = true;
  739. $this->is_date = true;
  740. }
  741. }
  742. if ( $qv['m'] ) {
  743. $this->is_date = true;
  744. if ( strlen($qv['m']) > 9 ) {
  745. $this->is_time = true;
  746. } elseif ( strlen( $qv['m'] ) > 7 ) {
  747. $this->is_day = true;
  748. } elseif ( strlen( $qv['m'] ) > 5 ) {
  749. $this->is_month = true;
  750. } else {
  751. $this->is_year = true;
  752. }
  753. }
  754. if ( '' != $qv['w'] ) {
  755. $this->is_date = true;
  756. }
  757. $this->query_vars_hash = false;
  758. $this->parse_tax_query( $qv );
  759. foreach ( $this->tax_query->queries as $tax_query ) {
  760. if ( ! is_array( $tax_query ) ) {
  761. continue;
  762. }
  763. if ( isset( $tax_query['operator'] ) && 'NOT IN' != $tax_query['operator'] ) {
  764. switch ( $tax_query['taxonomy'] ) {
  765. case 'category':
  766. $this->is_category = true;
  767. break;
  768. case 'post_tag':
  769. $this->is_tag = true;
  770. break;
  771. default:
  772. $this->is_tax = true;
  773. }
  774. }
  775. }
  776. unset( $tax_query );
  777. if ( empty($qv['author']) || ($qv['author'] == '0') ) {
  778. $this->is_author = false;
  779. } else {
  780. $this->is_author = true;
  781. }
  782. if ( '' != $qv['author_name'] )
  783. $this->is_author = true;
  784. if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) {
  785. $post_type_obj = get_post_type_object( $qv['post_type'] );
  786. if ( ! empty( $post_type_obj->has_archive ) )
  787. $this->is_post_type_archive = true;
  788. }
  789. if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax )
  790. $this->is_archive = true;
  791. }
  792. if ( '' != $qv['feed'] )
  793. $this->is_feed = true;
  794. if ( '' != $qv['embed'] ) {
  795. $this->is_embed = true;
  796. }
  797. if ( '' != $qv['tb'] )
  798. $this->is_trackback = true;
  799. if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) )
  800. $this->is_paged = true;
  801. // if we're previewing inside the write screen
  802. if ( '' != $qv['preview'] )
  803. $this->is_preview = true;
  804. if ( is_admin() )
  805. $this->is_admin = true;
  806. if ( false !== strpos($qv['feed'], 'comments-') ) {
  807. $qv['feed'] = str_replace('comments-', '', $qv['feed']);
  808. $qv['withcomments'] = 1;
  809. }
  810. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  811. if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) )
  812. $this->is_comment_feed = true;
  813. if ( !( $this->is_singular || $this->is_archive || $this->is_search || $this->is_feed || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_robots ) )
  814. $this->is_home = true;
  815. // Correct is_* for page_on_front and page_for_posts
  816. if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) {
  817. $_query = wp_parse_args($this->query);
  818. // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename.
  819. if ( isset($_query['pagename']) && '' == $_query['pagename'] )
  820. unset($_query['pagename']);
  821. unset( $_query['embed'] );
  822. if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) {
  823. $this->is_page = true;
  824. $this->is_home = false;
  825. $qv['page_id'] = get_option('page_on_front');
  826. // Correct <!--nextpage--> for page_on_front
  827. if ( !empty($qv['paged']) ) {
  828. $qv['page'] = $qv['paged'];
  829. unset($qv['paged']);
  830. }
  831. }
  832. }
  833. if ( '' != $qv['pagename'] ) {
  834. $this->queried_object = get_page_by_path( $qv['pagename'] );
  835. if ( $this->queried_object && 'attachment' == $this->queried_object->post_type ) {
  836. if ( preg_match( "/^[^%]*%(?:postname)%/", get_option( 'permalink_structure' ) ) ) {
  837. // See if we also have a post with the same slug
  838. $post = get_page_by_path( $qv['pagename'], OBJECT, 'post' );
  839. if ( $post ) {
  840. $this->queried_object = $post;
  841. $this->is_page = false;
  842. $this->is_single = true;
  843. }
  844. }
  845. }
  846. if ( ! empty( $this->queried_object ) ) {
  847. $this->queried_object_id = (int) $this->queried_object->ID;
  848. } else {
  849. unset( $this->queried_object );
  850. }
  851. if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) {
  852. $this->is_page = false;
  853. $this->is_home = true;
  854. $this->is_posts_page = true;
  855. }
  856. }
  857. if ( $qv['page_id'] ) {
  858. if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) {
  859. $this->is_page = false;
  860. $this->is_home = true;
  861. $this->is_posts_page = true;
  862. }
  863. }
  864. if ( !empty($qv['post_type']) ) {
  865. if ( is_array($qv['post_type']) )
  866. $qv['post_type'] = array_map('sanitize_key', $qv['post_type']);
  867. else
  868. $qv['post_type'] = sanitize_key($qv['post_type']);
  869. }
  870. if ( ! empty( $qv['post_status'] ) ) {
  871. if ( is_array( $qv['post_status'] ) )
  872. $qv['post_status'] = array_map('sanitize_key', $qv['post_status']);
  873. else
  874. $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']);
  875. }
  876. if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) )
  877. $this->is_comment_feed = false;
  878. $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
  879. // Done correcting is_* for page_on_front and page_for_posts
  880. if ( '404' == $qv['error'] )
  881. $this->set_404();
  882. $this->is_embed = $this->is_embed && ( $this->is_singular || $this->is_404 );
  883. $this->query_vars_hash = md5( serialize( $this->query_vars ) );
  884. $this->query_vars_changed = false;
  885. /**
  886. * Fires after the main query vars have been parsed.
  887. *
  888. * @since 1.5.0
  889. *
  890. * @param WP_Query $this The WP_Query instance (passed by reference).
  891. */
  892. do_action_ref_array( 'parse_query', array( &$this ) );
  893. }
  894. /**
  895. * Parses various taxonomy related query vars.
  896. *
  897. * For BC, this method is not marked as protected. See [28987].
  898. *
  899. * @since 3.1.0
  900. *
  901. * @param array $q The query variables. Passed by reference.
  902. */
  903. public function parse_tax_query( &$q ) {
  904. if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
  905. $tax_query = $q['tax_query'];
  906. } else {
  907. $tax_query = array();
  908. }
  909. if ( !empty($q['taxonomy']) && !empty($q['term']) ) {
  910. $tax_query[] = array(
  911. 'taxonomy' => $q['taxonomy'],
  912. 'terms' => array( $q['term'] ),
  913. 'field' => 'slug',
  914. );
  915. }
  916. foreach ( get_taxonomies( array() , 'objects' ) as $taxonomy => $t ) {
  917. if ( 'post_tag' == $taxonomy )
  918. continue; // Handled further down in the $q['tag'] block
  919. if ( $t->query_var && !empty( $q[$t->query_var] ) ) {
  920. $tax_query_defaults = array(
  921. 'taxonomy' => $taxonomy,
  922. 'field' => 'slug',
  923. );
  924. if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) {
  925. $q[$t->query_var] = wp_basename( $q[$t->query_var] );
  926. }
  927. $term = $q[$t->query_var];
  928. if ( is_array( $term ) ) {
  929. $term = implode( ',', $term );
  930. }
  931. if ( strpos($term, '+') !== false ) {
  932. $terms = preg_split( '/[+]+/', $term );
  933. foreach ( $terms as $term ) {
  934. $tax_query[] = array_merge( $tax_query_defaults, array(
  935. 'terms' => array( $term )
  936. ) );
  937. }
  938. } else {
  939. $tax_query[] = array_merge( $tax_query_defaults, array(
  940. 'terms' => preg_split( '/[,]+/', $term )
  941. ) );
  942. }
  943. }
  944. }
  945. // If querystring 'cat' is an array, implode it.
  946. if ( is_array( $q['cat'] ) ) {
  947. $q['cat'] = implode( ',', $q['cat'] );
  948. }
  949. // Category stuff
  950. if ( ! empty( $q['cat'] ) && ! $this->is_singular ) {
  951. $cat_in = $cat_not_in = array();
  952. $cat_array = preg_split( '/[,\s]+/', urldecode( $q['cat'] ) );
  953. $cat_array = array_map( 'intval', $cat_array );
  954. $q['cat'] = implode( ',', $cat_array );
  955. foreach ( $cat_array as $cat ) {
  956. if ( $cat > 0 )
  957. $cat_in[] = $cat;
  958. elseif ( $cat < 0 )
  959. $cat_not_in[] = abs( $cat );
  960. }
  961. if ( ! empty( $cat_in ) ) {
  962. $tax_query[] = array(
  963. 'taxonomy' => 'category',
  964. 'terms' => $cat_in,
  965. 'field' => 'term_id',
  966. 'include_children' => true
  967. );
  968. }
  969. if ( ! empty( $cat_not_in ) ) {
  970. $tax_query[] = array(
  971. 'taxonomy' => 'category',
  972. 'terms' => $cat_not_in,
  973. 'field' => 'term_id',
  974. 'operator' => 'NOT IN',
  975. 'include_children' => true
  976. );
  977. }
  978. unset( $cat_array, $cat_in, $cat_not_in );
  979. }
  980. if ( ! empty( $q['category__and'] ) && 1 === count( (array) $q['category__and'] ) ) {
  981. $q['category__and'] = (array) $q['category__and'];
  982. if ( ! isset( $q['category__in'] ) )
  983. $q['category__in'] = array();
  984. $q['category__in'][] = absint( reset( $q['category__and'] ) );
  985. unset( $q['category__and'] );
  986. }
  987. if ( ! empty( $q['category__in'] ) ) {
  988. $q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) );
  989. $tax_query[] = array(
  990. 'taxonomy' => 'category',
  991. 'terms' => $q['category__in'],
  992. 'field' => 'term_id',
  993. 'include_children' => false
  994. );
  995. }
  996. if ( ! empty($q['category__not_in']) ) {
  997. $q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) );
  998. $tax_query[] = array(
  999. 'taxonomy' => 'category',
  1000. 'terms' => $q['category__not_in'],
  1001. 'operator' => 'NOT IN',
  1002. 'include_children' => false
  1003. );
  1004. }
  1005. if ( ! empty($q['category__and']) ) {
  1006. $q['category__and'] = array_map( 'absint', array_unique( (array) $q['category__and'] ) );
  1007. $tax_query[] = array(
  1008. 'taxonomy' => 'category',
  1009. 'terms' => $q['category__and'],
  1010. 'field' => 'term_id',
  1011. 'operator' => 'AND',
  1012. 'include_children' => false
  1013. );
  1014. }
  1015. // If querystring 'tag' is array, implode it.
  1016. if ( is_array( $q['tag'] ) ) {
  1017. $q['tag'] = implode( ',', $q['tag'] );
  1018. }
  1019. // Tag stuff
  1020. if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) {
  1021. if ( strpos($q['tag'], ',') !== false ) {
  1022. $tags = preg_split('/[,\r\n\t ]+/', $q['tag']);
  1023. foreach ( (array) $tags as $tag ) {
  1024. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1025. $q['tag_slug__in'][] = $tag;
  1026. }
  1027. } elseif ( preg_match('/[+\r\n\t ]+/', $q['tag'] ) || ! empty( $q['cat'] ) ) {
  1028. $tags = preg_split('/[+\r\n\t ]+/', $q['tag']);
  1029. foreach ( (array) $tags as $tag ) {
  1030. $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
  1031. $q['tag_slug__and'][] = $tag;
  1032. }
  1033. } else {
  1034. $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db');
  1035. $q['tag_slug__in'][] = $q['tag'];
  1036. }
  1037. }
  1038. if ( !empty($q['tag_id']) ) {
  1039. $q['tag_id'] = absint( $q['tag_id'] );
  1040. $tax_query[] = array(
  1041. 'taxonomy' => 'post_tag',
  1042. 'terms' => $q['tag_id']
  1043. );
  1044. }
  1045. if ( !empty($q['tag__in']) ) {
  1046. $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
  1047. $tax_query[] = array(
  1048. 'taxonomy' => 'post_tag',
  1049. 'terms' => $q['tag__in']
  1050. );
  1051. }
  1052. if ( !empty($q['tag__not_in']) ) {
  1053. $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
  1054. $tax_query[] = array(
  1055. 'taxonomy' => 'post_tag',
  1056. 'terms' => $q['tag__not_in'],
  1057. 'operator' => 'NOT IN'
  1058. );
  1059. }
  1060. if ( !empty($q['tag__and']) ) {
  1061. $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
  1062. $tax_query[] = array(
  1063. 'taxonomy' => 'post_tag',
  1064. 'terms' => $q['tag__and'],
  1065. 'operator' => 'AND'
  1066. );
  1067. }
  1068. if ( !empty($q['tag_slug__in']) ) {
  1069. $q['tag_slug__in'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) );
  1070. $tax_query[] = array(
  1071. 'taxonomy' => 'post_tag',
  1072. 'terms' => $q['tag_slug__in'],
  1073. 'field' => 'slug'
  1074. );
  1075. }
  1076. if ( !empty($q['tag_slug__and']) ) {
  1077. $q['tag_slug__and'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) );
  1078. $tax_query[] = array(
  1079. 'taxonomy' => 'post_tag',
  1080. 'terms' => $q['tag_slug__and'],
  1081. 'field' => 'slug',
  1082. 'operator' => 'AND'
  1083. );
  1084. }
  1085. $this->tax_query = new WP_Tax_Query( $tax_query );
  1086. /**
  1087. * Fires after taxonomy-related query vars have been parsed.
  1088. *
  1089. * @since 3.7.0
  1090. *
  1091. * @param WP_Query $this The WP_Query instance.
  1092. */
  1093. do_action( 'parse_tax_query', $this );
  1094. }
  1095. /**
  1096. * Generates SQL for the WHERE clause based on passed search terms.
  1097. *
  1098. * @since 3.7.0
  1099. *
  1100. * @global wpdb $wpdb WordPress database abstraction object.
  1101. *
  1102. * @param array $q Query variables.
  1103. * @return string WHERE clause.
  1104. */
  1105. protected function parse_search( &$q ) {
  1106. global $wpdb;
  1107. $search = '';
  1108. // added slashes screw with quote grouping when done early, so done later
  1109. $q['s'] = stripslashes( $q['s'] );
  1110. if ( empty( $_GET['s'] ) && $this->is_main_query() )
  1111. $q['s'] = urldecode( $q['s'] );
  1112. // there are no line breaks in <input /> fields
  1113. $q['s'] = str_replace( array( "\r", "\n" ), '', $q['s'] );
  1114. $q['search_terms_count'] = 1;
  1115. if ( ! empty( $q['sentence'] ) ) {
  1116. $q['search_terms'] = array( $q['s'] );
  1117. } else {
  1118. if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches ) ) {
  1119. $q['search_terms_count'] = count( $matches[0] );
  1120. $q['search_terms'] = $this->parse_search_terms( $matches[0] );
  1121. // if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence
  1122. if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 )
  1123. $q['search_terms'] = array( $q['s'] );
  1124. } else {
  1125. $q['search_terms'] = array( $q['s'] );
  1126. }
  1127. }
  1128. $n = ! empty( $q['exact'] ) ? '' : '%';
  1129. $searchand = '';
  1130. $q['search_orderby_title'] = array();
  1131. /**
  1132. * Filters the prefix that indicates that a search term should be excluded from results.
  1133. *
  1134. * @since 4.7.0
  1135. *
  1136. * @param string $exclusion_prefix The prefix. Default '-'. Returning
  1137. * an empty value disables exclusions.
  1138. */
  1139. $exclusion_prefix = apply_filters( 'wp_query_search_exclusion_prefix', '-' );
  1140. foreach ( $q['search_terms'] as $term ) {
  1141. // If there is an $exclusion_prefix, terms prefixed with it should be excluded.
  1142. $exclude = $exclusion_prefix && ( $exclusion_prefix === substr( $term, 0, 1 ) );
  1143. if ( $exclude ) {
  1144. $like_op = 'NOT LIKE';
  1145. $andor_op = 'AND';
  1146. $term = substr( $term, 1 );
  1147. } else {
  1148. $like_op = 'LIKE';
  1149. $andor_op = 'OR';
  1150. }
  1151. if ( $n && ! $exclude ) {
  1152. $like = '%' . $wpdb->esc_like( $term ) . '%';
  1153. $q['search_orderby_title'][] = $wpdb->prepare( "{$wpdb->posts}.post_title LIKE %s", $like );
  1154. }
  1155. $like = $n . $wpdb->esc_like( $term ) . $n;
  1156. $search .= $wpdb->prepare( "{$searchand}(({$wpdb->posts}.post_title $like_op %s) $andor_op ({$wpdb->posts}.post_excerpt $like_op %s) $andor_op ({$wpdb->posts}.post_content $like_op %s))", $like, $like, $like );
  1157. $searchand = ' AND ';
  1158. }
  1159. if ( ! empty( $search ) ) {
  1160. $search = " AND ({$search}) ";
  1161. if ( ! is_user_logged_in() ) {
  1162. $search .= " AND ({$wpdb->posts}.post_password = '') ";
  1163. }
  1164. }
  1165. return $search;
  1166. }
  1167. /**
  1168. * Check if the terms are suitable for searching.
  1169. *
  1170. * Uses an array of stopwords (terms) that are excluded from the separate
  1171. * term matching when searching for posts. The list of English stopwords is
  1172. * the approximate search engines list, and is translatable.
  1173. *
  1174. * @since 3.7.0
  1175. *
  1176. * @param array $terms Terms to check.
  1177. * @return array Terms that are not stopwords.
  1178. */
  1179. protected function parse_search_terms( $terms ) {
  1180. $strtolower = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower';
  1181. $checked = array();
  1182. $stopwords = $this->get_search_stopwords();
  1183. foreach ( $terms as $term ) {
  1184. // keep before/after spaces when term is for exact match
  1185. if ( preg_match( '/^".+"$/', $term ) )
  1186. $term = trim( $term, "\"'" );
  1187. else
  1188. $term = trim( $term, "\"' " );
  1189. // Avoid single A-Z and single dashes.
  1190. if ( ! $term || ( 1 === strlen( $term ) && preg_match( '/^[a-z\-]$/i', $term ) ) )
  1191. continue;
  1192. if ( in_array( call_user_func( $strtolower, $term ), $stopwords, true ) )
  1193. continue;
  1194. $checked[] = $term;
  1195. }
  1196. return $checked;
  1197. }
  1198. /**
  1199. * Retrieve stopwords used when parsing search terms.
  1200. *
  1201. * @since 3.7.0
  1202. *
  1203. * @return array Stopwords.
  1204. */
  1205. protected function get_search_stopwords() {
  1206. if ( isset( $this->stopwords ) )
  1207. return $this->stopwords;
  1208. /* translators: This is a comma-separated list of very common words that should be excluded from a search,
  1209. * like a, an, and the. These are usually called "stopwords". You should not simply translate these individual
  1210. * words into your language. Instead, look for and provide commonly accepted stopwords in your language.
  1211. */
  1212. $words = explode( ',', _x( 'about,an,are,as,at,be,by,com,for,from,how,in,is,it,of,on,or,that,the,this,to,was,what,when,where,who,will,with,www',
  1213. 'Comma-separated list of search stopwords in your language' ) );
  1214. $stopwords = array();
  1215. foreach ( $words as $word ) {
  1216. $word = trim( $word, "\r\n\t " );
  1217. if ( $word )
  1218. $stopwords[] = $word;
  1219. }
  1220. /**
  1221. * Filters stopwords used when parsing search terms.
  1222. *
  1223. * @since 3.7.0
  1224. *
  1225. * @param array $stopwords Stopwords.
  1226. */
  1227. $this->stopwords = apply_filters( 'wp_search_stopwords', $stopwords );
  1228. return $this->stopwords;
  1229. }
  1230. /**
  1231. * Generates SQL for the ORDER BY condition based on passed search terms.
  1232. *
  1233. * @since 3.7.0
  1234. *
  1235. * @global wpdb $wpdb WordPress database abstraction object.
  1236. *
  1237. * @param array $q Query variables.
  1238. * @return string ORDER BY clause.
  1239. */
  1240. protected function parse_search_order( &$q ) {
  1241. global $wpdb;
  1242. if ( $q['search_terms_count'] > 1 ) {
  1243. $num_terms = count( $q['search_orderby_title'] );
  1244. // If the search terms contain negative queries, don't bother ordering by sentence matches.
  1245. $like = '';
  1246. if ( ! preg_match( '/(?:\s|^)\-/', $q['s'] ) ) {
  1247. $like = '%' . $wpdb->esc_like( $q['s'] ) . '%';
  1248. }
  1249. $search_orderby = '';
  1250. // sentence match in 'post_title'
  1251. if ( $like ) {
  1252. $search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_title LIKE %s THEN 1 ", $like );
  1253. }
  1254. // sanity limit, sort as sentence when more than 6 terms
  1255. // (few searches are longer than 6 terms and most titles are not)
  1256. if ( $num_terms < 7 ) {
  1257. // all words in title
  1258. $search_orderby .= 'WHEN ' . implode( ' AND ', $q['search_orderby_title'] ) . ' THEN 2 ';
  1259. // any word in title, not needed when $num_terms == 1
  1260. if ( $num_terms > 1 )
  1261. $search_orderby .= 'WHEN ' . implode( ' OR ', $q['search_orderby_title'] ) . ' THEN 3 ';
  1262. }
  1263. // Sentence match in 'post_content' and 'post_excerpt'.
  1264. if ( $like ) {
  1265. $search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_excerpt LIKE %s THEN 4 ", $like );
  1266. $search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_content LIKE %s THEN 5 ", $like );
  1267. }
  1268. if ( $search_orderby ) {
  1269. $search_orderby = '(CASE ' . $search_orderby . 'ELSE 6 END)';
  1270. }
  1271. } else {
  1272. // single word or sentence search
  1273. $search_orderby = reset( $q['search_orderby_title'] ) . ' DESC';
  1274. }
  1275. return $search_orderby;
  1276. }
  1277. /**
  1278. * Converts the given orderby alias (if allowed) to a properly-prefixed value.
  1279. *
  1280. * @since 4.0.0
  1281. *
  1282. * @global wpdb $wpdb WordPress database abstraction object.
  1283. *
  1284. * @param string $orderby Alias for the field to order by.
  1285. * @return string|false Table-prefixed value to used in the ORDER clause. False otherwise.
  1286. */
  1287. protected function parse_orderby( $orderby ) {
  1288. global $wpdb;
  1289. // Used to filter values.
  1290. $allowed_keys = array(
  1291. 'post_name', 'post_author', 'post_date', 'post_title', 'post_modified',
  1292. 'post_parent', 'post_type', 'name', 'author', 'date', 'title', 'modified',
  1293. 'parent', 'type', 'ID', 'menu_order', 'comment_count', 'rand',
  1294. );
  1295. $primary_meta_key = '';
  1296. $primary_meta_query = false;
  1297. $meta_clauses = $this->meta_query->get_clauses();
  1298. if ( ! empty( $meta_clauses ) ) {
  1299. $primary_meta_query = reset( $meta_clauses );
  1300. if ( ! empty( $primary_meta_query['key'] ) ) {
  1301. $primary_meta_key = $primary_meta_query['key'];
  1302. $allowed_keys[] = $primary_meta_key;
  1303. }
  1304. $allowed_keys[] = 'meta_value';
  1305. $allowed_keys[] = 'meta_value_num';
  1306. $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_clauses ) );
  1307. }
  1308. // If RAND() contains a seed value, sanitize and add to allowed keys.
  1309. $rand_with_seed = false;
  1310. if ( preg_match( '/RAND\(([0-9]+)\)/i', $orderby, $matches ) ) {
  1311. $orderby = sprintf( 'RAND(%s)', intval( $matches[1] ) );
  1312. $allowed_keys[] = $orderby;
  1313. $rand_with_seed = true;
  1314. }
  1315. if ( ! in_array( $orderby, $allowed_keys, true ) ) {
  1316. return false;
  1317. }
  1318. switch ( $orderby ) {
  1319. case 'post_name':
  1320. case 'post_author':
  1321. case 'post_date':
  1322. case 'post_title':
  1323. case 'post_modified':
  1324. case 'post_parent':
  1325. case 'post_type':
  1326. case 'ID':
  1327. case 'menu_order':
  1328. case 'comment_count':
  1329. $orderby_clause = "{$wpdb->posts}.{$orderby}";
  1330. break;
  1331. case 'rand':
  1332. $orderby_clause = 'RAND()';
  1333. break;
  1334. case $primary_meta_key:
  1335. case 'meta_value':
  1336. if ( ! empty( $primary_meta_query['type'] ) ) {
  1337. $orderby_clause = "CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})";
  1338. } else {
  1339. $orderby_clause = "{$primary_meta_query['alias']}.meta_value";
  1340. }
  1341. break;
  1342. case 'meta_value_num':
  1343. $orderby_clause = "{$primary_meta_query['alias']}.meta_value+0";
  1344. break;
  1345. default:
  1346. if ( array_key_exists( $orderby, $meta_clauses ) ) {
  1347. // $orderby corresponds to a meta_query clause.
  1348. $meta_clause = $meta_clauses[ $orderby ];
  1349. $orderby_clause = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})";
  1350. } elseif ( $rand_with_seed ) {
  1351. $orderby_clause = $orderby;
  1352. } else {
  1353. // Default: order by post field.
  1354. $orderby_clause = "{$wpdb->posts}.post_" . sanitize_key( $orderby );
  1355. }
  1356. break;
  1357. }
  1358. return $orderby_clause;
  1359. }
  1360. /**
  1361. * Parse an 'order' query variable and cast it to ASC or DESC as necessary.
  1362. *
  1363. * @since 4.0.0
  1364. *
  1365. * @param string $order The 'order' query variable.
  1366. * @return string The sanitized 'order' query variable.
  1367. */
  1368. protected function parse_order( $order ) {
  1369. if ( ! is_string( $order ) || empty( $order ) ) {
  1370. return 'DESC';
  1371. }
  1372. if ( 'ASC' === strtoupper( $order ) ) {
  1373. return 'ASC';
  1374. } else {
  1375. return 'DESC';
  1376. }
  1377. }
  1378. /**
  1379. * Sets the 404 property and saves whether query is feed.
  1380. *
  1381. * @since 2.0.0
  1382. */
  1383. public function set_404() {
  1384. $is_feed = $this->is_feed;
  1385. $this->init_query_flags();
  1386. $this->is_404 = true;
  1387. $this->is_feed = $is_feed;
  1388. }
  1389. /**
  1390. * Retrieve query variable.
  1391. *
  1392. * @since 1.5.0
  1393. * @since 3.9.0 The `$default` argument was introduced.
  1394. *
  1395. *
  1396. * @param string $query_var Query variable key.
  1397. * @param mixed $default Optional. Value to return if the query variable is not set. Default empty.
  1398. * @return mixed Contents of the query variable.
  1399. */
  1400. public function get( $query_var, $default = '' ) {
  1401. if ( isset( $this->query_vars[ $query_var ] ) ) {
  1402. return $this->query_vars[ $query_var ];
  1403. }
  1404. return $default;
  1405. }
  1406. /**
  1407. * Set query variable.
  1408. *
  1409. * @since 1.5.0
  1410. *
  1411. * @param string $query_var Query variable key.
  1412. * @param mixed $value Query variable value.
  1413. */
  1414. public function set($query_var, $value) {
  1415. $this->query_vars[$query_var] = $value;
  1416. }
  1417. /**
  1418. * Retrieve the posts based on query variables.
  1419. *
  1420. * There are a few filters and actions that can be used to modify the post
  1421. * database query.
  1422. *
  1423. * @since 1.5.0
  1424. *
  1425. * @return array List of posts.
  1426. */
  1427. public function get_posts() {
  1428. global $wpdb;
  1429. $this->parse_query();
  1430. /**
  1431. * Fires after the query variable object is created, but before the actual query is run.
  1432. *
  1433. * Note: If using conditional tags, use the method versions within the passed instance
  1434. * (e.g. $this->is_main_query() instead of is_main_query()). This is because the functions
  1435. * like is_main_query() test against the global $wp_query instance, not the passed one.
  1436. *
  1437. * @since 2.0.0
  1438. *
  1439. * @param WP_Query $this The WP_Query instance (passed by reference).
  1440. */
  1441. do_action_ref_array( 'pre_get_posts', array( &$this ) );
  1442. // Shorthand.
  1443. $q = &$this->query_vars;
  1444. // Fill again in case pre_get_posts unset some vars.
  1445. $q = $this->fill_query_vars($q);
  1446. // Parse meta query
  1447. $this->meta_query = new WP_Meta_Query();
  1448. $this->meta_query->parse_query_vars( $q );
  1449. // Set a flag if a pre_get_posts hook changed the query vars.
  1450. $hash = md5( serialize( $this->query_vars ) );
  1451. if ( $hash != $this->query_vars_hash ) {
  1452. $this->query_vars_changed = true;
  1453. $this->query_vars_hash = $hash;
  1454. }
  1455. unset($hash);
  1456. // First let's clear some variables
  1457. $distinct = '';
  1458. $whichauthor = '';
  1459. $whichmimetype = '';
  1460. $where = '';
  1461. $limits = '';
  1462. $join = '';
  1463. $search = '';
  1464. $groupby = '';
  1465. $post_status_join = false;
  1466. $page = 1;
  1467. if ( isset( $q['caller_get_posts'] ) ) {
  1468. _deprecated_argument( 'WP_Query', '3.1.0',
  1469. /* translators: 1: caller_get_posts, 2: ignore_sticky_posts */
  1470. sprintf( __( '%1$s is deprecated. Use %2$s instead.' ),
  1471. '<code>caller_get_posts</code>',
  1472. '<code>ignore_sticky_posts</code>'
  1473. )
  1474. );
  1475. if ( ! isset( $q['ignore_sticky_posts'] ) ) {
  1476. $q['ignore_sticky_posts'] = $q['caller_get_posts'];
  1477. }
  1478. }
  1479. if ( !isset( $q['ignore_sticky_posts'] ) )
  1480. $q['ignore_sticky_posts'] = false;
  1481. if ( !isset($q['suppress_filters']) )
  1482. $q['suppress_filters'] = false;
  1483. if ( !isset($q['cache_results']) ) {
  1484. if ( wp_using_ext_object_cache() )
  1485. $q['cache_results'] = false;
  1486. else
  1487. $q['cache_results'] = true;
  1488. }
  1489. if ( !isset($q['update_post_term_cache']) )
  1490. $q['update_post_term_cache'] = true;
  1491. if ( ! isset( $q['lazy_load_term_meta'] ) ) {
  1492. $q['lazy_load_term_meta'] = $q['update_post_term_cache'];
  1493. }
  1494. if ( !isset($q['update_post_meta_cache']) )
  1495. $q['update_post_meta_cache'] = true;
  1496. if ( !isset($q['post_type']) ) {
  1497. if ( $this->is_search )
  1498. $q['post_type'] = 'any';
  1499. else
  1500. $q['post_type'] = '';
  1501. }
  1502. $post_type = $q['post_type'];
  1503. if ( empty( $q['posts_per_page'] ) ) {
  1504. $q['posts_per_page'] = get_option( 'posts_per_page' );
  1505. }
  1506. if ( isset($q['showposts']) && $q['showposts'] ) {
  1507. $q['showposts'] = (int) $q['showposts'];
  1508. $q['posts_per_page'] = $q['showposts'];
  1509. }
  1510. if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
  1511. $q['posts_per_page'] = $q['posts_per_archive_page'];
  1512. if ( !isset($q['nopaging']) ) {
  1513. if ( $q['posts_per_page'] == -1 ) {
  1514. $q['nopaging'] = true;
  1515. } else {
  1516. $q['nopaging'] = false;
  1517. }
  1518. }
  1519. if ( $this->is_feed ) {
  1520. // This overrides posts_per_page.
  1521. if ( ! empty( $q['posts_per_rss'] ) ) {
  1522. $q['posts_per_page'] = $q['posts_per_rss'];
  1523. } else {
  1524. $q['posts_per_page'] = get_option( 'posts_per_rss' );
  1525. }
  1526. $q['nopaging'] = false;
  1527. }
  1528. $q['posts_per_page'] = (int) $q['posts_per_page'];
  1529. if ( $q['posts_per_page'] < -1 )
  1530. $q['posts_per_page'] = abs($q['posts_per_page']);
  1531. elseif ( $q['posts_per_page'] == 0 )
  1532. $q['posts_per_page'] = 1;
  1533. if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
  1534. $q['comments_per_page'] = get_option('comments_per_page');
  1535. if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
  1536. $this->is_page = true;
  1537. $this->is_home = false;
  1538. $q['page_id'] = get_option('page_on_front');
  1539. }
  1540. if ( isset($q['page']) ) {
  1541. $q['page'] = trim($q['page'], '/');
  1542. $q['page'] = absint($q['page']);
  1543. }
  1544. // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
  1545. if ( isset($q['no_found_rows']) )
  1546. $q['no_found_rows'] = (bool) $q['no_found_rows'];
  1547. else
  1548. $q['no_found_rows'] = false;
  1549. switch ( $q['fields'] ) {
  1550. case 'ids':
  1551. $fields = "{$wpdb->posts}.ID";
  1552. break;
  1553. case 'id=>parent':
  1554. $fields = "{$wpdb->posts}.ID, {$wpdb->posts}.post_parent";
  1555. break;
  1556. default:
  1557. $fields = "{$wpdb->posts}.*";
  1558. }
  1559. if ( '' !== $q['menu_order'] ) {
  1560. $where .= " AND {$wpdb->posts}.menu_order = " . $q['menu_order'];
  1561. }
  1562. // The "m" parameter is meant for months but accepts datetimes of varying specificity
  1563. if ( $q['m'] ) {
  1564. $where .= " AND YEAR({$wpdb->posts}.post_date)=" . substr($q['m'], 0, 4);
  1565. if ( strlen($q['m']) > 5 ) {
  1566. $where .= " AND MONTH({$wpdb->posts}.post_date)=" . substr($q['m'], 4, 2);
  1567. }
  1568. if ( strlen($q['m']) > 7 ) {
  1569. $where .= " AND DAYOFMONTH({$wpdb->posts}.post_date)=" . substr($q['m'], 6, 2);
  1570. }
  1571. if ( strlen($q['m']) > 9 ) {
  1572. $where .= " AND HOUR({$wpdb->posts}.post_date)=" . substr($q['m'], 8, 2);
  1573. }
  1574. if ( strlen($q['m']) > 11 ) {
  1575. $where .= " AND MINUTE({$wpdb->posts}.post_date)=" . substr($q['m'], 10, 2);
  1576. }
  1577. if ( strlen($q['m']) > 13 ) {
  1578. $where .= " AND SECOND({$wpdb->posts}.post_date)=" . substr($q['m'], 12, 2);
  1579. }
  1580. }
  1581. // Handle the other individual date parameters
  1582. $date_parameters = array();
  1583. if ( '' !== $q['hour'] )
  1584. $date_parameters['hour'] = $q['hour'];
  1585. if ( '' !== $q['minute'] )
  1586. $date_parameters['minute'] = $q['minute'];
  1587. if ( '' !== $q['second'] )
  1588. $date_parameters['second'] = $q['second'];
  1589. if ( $q['year'] )
  1590. $date_parameters['year'] = $q['year'];
  1591. if ( $q['monthnum'] )
  1592. $date_parameters['monthnum'] = $q['monthnum'];
  1593. if ( $q['w'] )
  1594. $date_parameters['week'] = $q['w'];
  1595. if ( $q['day'] )
  1596. $date_parameters['day'] = $q['day'];
  1597. if ( $date_parameters ) {
  1598. $date_query = new WP_Date_Query( array( $date_parameters ) );
  1599. $where .= $date_query->get_sql();
  1600. }
  1601. unset( $date_parameters, $date_query );
  1602. // Handle complex date queries
  1603. if ( ! empty( $q['date_query'] ) ) {
  1604. $this->date_query = new WP_Date_Query( $q['date_query'] );
  1605. $where .= $this->date_query->get_sql();
  1606. }
  1607. // If we've got a post_type AND it's not "any" post_type.
  1608. if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
  1609. foreach ( (array)$q['post_type'] as $_post_type ) {
  1610. $ptype_obj = get_post_type_object($_post_type);
  1611. if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
  1612. continue;
  1613. if ( ! $ptype_obj->hierarchical ) {
  1614. // Non-hierarchical post types can directly use 'name'.
  1615. $q['name'] = $q[ $ptype_obj->query_var ];
  1616. } else {
  1617. // Hierarchical post types will operate through 'pagename'.
  1618. $q['pagename'] = $q[ $ptype_obj->query_var ];
  1619. $q['name'] = '';
  1620. }
  1621. // Only one request for a slug is possible, this is why name & pagename are overwritten above.
  1622. break;
  1623. } //end foreach
  1624. unset($ptype_obj);
  1625. }
  1626. if ( '' !== $q['title'] ) {
  1627. $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_title = %s", stripslashes( $q['title'] ) );
  1628. }
  1629. // Parameters related to 'post_name'.
  1630. if ( '' != $q['name'] ) {
  1631. $q['name'] = sanitize_title_for_query( $q['name'] );
  1632. $where .= " AND {$wpdb->posts}.post_name = '" . $q['name'] . "'";
  1633. } elseif ( '' != $q['pagename'] ) {
  1634. if ( isset($this->queried_object_id) ) {
  1635. $reqpage = $this->queried_object_id;
  1636. } else {
  1637. if ( 'page' != $q['post_type'] ) {
  1638. foreach ( (array)$q['post_type'] as $_post_type ) {
  1639. $ptype_obj = get_post_type_object($_post_type);
  1640. if ( !$ptype_obj || !$ptype_obj->hierarchical )
  1641. continue;
  1642. $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
  1643. if ( $reqpage )
  1644. break;
  1645. }
  1646. unset($ptype_obj);
  1647. } else {
  1648. $reqpage = get_page_by_path($q['pagename']);
  1649. }
  1650. if ( !empty($reqpage) )
  1651. $reqpage = $reqpage->ID;
  1652. else
  1653. $reqpage = 0;
  1654. }
  1655. $page_for_posts = get_option('page_for_posts');
  1656. if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
  1657. $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
  1658. $q['name'] = $q['pagename'];
  1659. $where .= " AND ({$wpdb->posts}.ID = '$reqpage')";
  1660. $reqpage_obj = get_post( $reqpage );
  1661. if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
  1662. $this->is_attachment = true;
  1663. $post_type = $q['post_type'] = 'attachment';
  1664. $this->is_page = true;
  1665. $q['attachment_id'] = $reqpage;
  1666. }
  1667. }
  1668. } elseif ( '' != $q['attachment'] ) {
  1669. $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
  1670. $q['name'] = $q['attachment'];
  1671. $where .= " AND {$wpdb->posts}.post_name = '" . $q['attachment'] . "'";
  1672. } elseif ( is_array( $q['post_name__in'] ) && ! empty( $q['post_name__in'] ) ) {
  1673. $q['post_name__in'] = array_map( 'sanitize_title_for_query', $q['post_name__in'] );
  1674. $post_name__in = "'" . implode( "','", $q['post_name__in'] ) . "'";
  1675. $where .= " AND {$wpdb->posts}.post_name IN ($post_name__in)";
  1676. }
  1677. // If an attachment is requested by number, let it supersede any post number.
  1678. if ( $q['attachment_id'] )
  1679. $q['p'] = absint($q['attachment_id']);
  1680. // If a post number is specified, load that post
  1681. if ( $q['p'] ) {
  1682. $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
  1683. } elseif ( $q['post__in'] ) {
  1684. $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
  1685. $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
  1686. } elseif ( $q['post__not_in'] ) {
  1687. $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
  1688. $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
  1689. }
  1690. if ( is_numeric( $q['post_parent'] ) ) {
  1691. $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_parent = %d ", $q['post_parent'] );
  1692. } elseif ( $q['post_parent__in'] ) {
  1693. $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) );
  1694. $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)";
  1695. } elseif ( $q['post_parent__not_in'] ) {
  1696. $post_parent__not_in = implode( ',', array_map( 'absint', $q['post_parent__not_in'] ) );
  1697. $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)";
  1698. }
  1699. if ( $q['page_id'] ) {
  1700. if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
  1701. $q['p'] = $q['page_id'];
  1702. $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
  1703. }
  1704. }
  1705. // If a search pattern is specified, load the posts that match.
  1706. if ( strlen( $q['s'] ) ) {
  1707. $search = $this->parse_search( $q );
  1708. }
  1709. if ( ! $q['suppress_filters'] ) {
  1710. /**
  1711. * Filters the search SQL that is used in the WHERE clause of WP_Query.
  1712. *
  1713. * @since 3.0.0
  1714. *
  1715. * @param string $search Search SQL for WHERE clause.
  1716. * @param WP_Query $this The current WP_Query object.
  1717. */
  1718. $search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) );
  1719. }
  1720. // Taxonomies
  1721. if ( !$this->is_singular ) {
  1722. $this->parse_tax_query( $q );
  1723. $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
  1724. $join .= $clauses['join'];
  1725. $where .= $clauses['where'];
  1726. }
  1727. if ( $this->is_tax ) {
  1728. if ( empty($post_type) ) {
  1729. // Do a fully inclusive search for currently registered post types of queried taxonomies
  1730. $post_type = array();
  1731. $taxonomies = array_keys( $this->tax_query->queried_terms );
  1732. foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) {
  1733. $object_taxonomies = $pt === 'attachment' ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt );
  1734. if ( array_intersect( $taxonomies, $object_taxonomies ) )
  1735. $post_type[] = $pt;
  1736. }
  1737. if ( ! $post_type )
  1738. $post_type = 'any';
  1739. elseif ( count( $post_type ) == 1 )
  1740. $post_type = $post_type[0];
  1741. $post_status_join = true;
  1742. } elseif ( in_array('attachment', (array) $post_type) ) {
  1743. $post_status_join = true;
  1744. }
  1745. }
  1746. /*
  1747. * Ensure that 'taxonomy', 'term', 'term_id', 'cat', and
  1748. * 'category_name' vars are set for backward compatibility.
  1749. */
  1750. if ( ! empty( $this->tax_query->queried_terms ) ) {
  1751. /*
  1752. * Set 'taxonomy', 'term', and 'term_id' to the
  1753. * first taxonomy other than 'post_tag' or 'category'.
  1754. */
  1755. if ( ! isset( $q['taxonomy'] ) ) {
  1756. foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) {
  1757. if ( empty( $queried_items['terms'][0] ) ) {
  1758. continue;
  1759. }
  1760. if ( ! in_array( $queried_taxonomy, array( 'category', 'post_tag' ) ) ) {
  1761. $q['taxonomy'] = $queried_taxonomy;
  1762. if ( 'slug' === $queried_items['field'] ) {
  1763. $q['term'] = $queried_items['terms'][0];
  1764. } else {
  1765. $q['term_id'] = $queried_items['terms'][0];
  1766. }
  1767. // Take the first one we find.
  1768. break;
  1769. }
  1770. }
  1771. }
  1772. // 'cat', 'category_name', 'tag_id'
  1773. foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) {
  1774. if ( empty( $queried_items['terms'][0] ) ) {
  1775. continue;
  1776. }
  1777. if ( 'category' === $queried_taxonomy ) {
  1778. $the_cat = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'category' );
  1779. if ( $the_cat ) {
  1780. $this->set( 'cat', $the_cat->term_id );
  1781. $this->set( 'category_name', $the_cat->slug );
  1782. }
  1783. unset( $the_cat );
  1784. }
  1785. if ( 'post_tag' === $queried_taxonomy ) {
  1786. $the_tag = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'post_tag' );
  1787. if ( $the_tag ) {
  1788. $this->set( 'tag_id', $the_tag->term_id );
  1789. }
  1790. unset( $the_tag );
  1791. }
  1792. }
  1793. }
  1794. if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
  1795. $groupby = "{$wpdb->posts}.ID";
  1796. }
  1797. // Author/user stuff
  1798. if ( ! empty( $q['author'] ) && $q['author'] != '0' ) {
  1799. $q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) );
  1800. $authors = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $q['author'] ) ) );
  1801. foreach ( $authors as $author ) {
  1802. $key = $author > 0 ? 'author__in' : 'author__not_in';
  1803. $q[$key][] = abs( $author );
  1804. }
  1805. $q['author'] = implode( ',', $authors );
  1806. }
  1807. if ( ! empty( $q['author__not_in'] ) ) {
  1808. $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) );
  1809. $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) ";
  1810. } elseif ( ! empty( $q['author__in'] ) ) {
  1811. $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) );
  1812. $where .= " AND {$wpdb->posts}.post_author IN ($author__in) ";
  1813. }
  1814. // Author stuff for nice URLs
  1815. if ( '' != $q['author_name'] ) {
  1816. if ( strpos($q['author_name'], '/') !== false ) {
  1817. $q['author_name'] = explode('/', $q['author_name']);
  1818. if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
  1819. $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
  1820. } else {
  1821. $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailing slash
  1822. }
  1823. }
  1824. $q['author_name'] = sanitize_title_for_query( $q['author_name'] );
  1825. $q['author'] = get_user_by('slug', $q['author_name']);
  1826. if ( $q['author'] )
  1827. $q['author'] = $q['author']->ID;
  1828. $whichauthor .= " AND ({$wpdb->posts}.post_author = " . absint($q['author']) . ')';
  1829. }
  1830. // Matching by comment count.
  1831. if ( isset( $q['comment_count'] ) ) {
  1832. // Numeric comment count is converted to array format.
  1833. if ( is_numeric( $q['comment_count'] ) ) {
  1834. $q['comment_count'] = array(
  1835. 'value' => intval( $q['comment_count'] ),
  1836. );
  1837. }
  1838. if ( isset( $q['comment_count']['value'] ) ) {
  1839. $q['comment_count'] = array_merge( array(
  1840. 'compare' => '=',
  1841. ), $q['comment_count'] );
  1842. // Fallback for invalid compare operators is '='.
  1843. $compare_operators = array( '=', '!=', '>', '>=', '<', '<=' );
  1844. if ( ! in_array( $q['comment_count']['compare'], $compare_operators, true ) ) {
  1845. $q['comment_count']['compare'] = '=';
  1846. }
  1847. $where .= $wpdb->prepare( " AND {$wpdb->posts}.comment_count {$q['comment_count']['compare']} %d", $q['comment_count']['value'] );
  1848. }
  1849. }
  1850. // MIME-Type stuff for attachment browsing
  1851. if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] ) {
  1852. $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts );
  1853. }
  1854. $where .= $search . $whichauthor . $whichmimetype;
  1855. if ( ! empty( $this->meta_query->queries ) ) {
  1856. $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
  1857. $join .= $clauses['join'];
  1858. $where .= $clauses['where'];
  1859. }
  1860. $rand = ( isset( $q['orderby'] ) && 'rand' === $q['orderby'] );
  1861. if ( ! isset( $q['order'] ) ) {
  1862. $q['order'] = $rand ? '' : 'DESC';
  1863. } else {
  1864. $q['order'] = $rand ? '' : $this->parse_order( $q['order'] );
  1865. }
  1866. // Order by.
  1867. if ( empty( $q['orderby'] ) ) {
  1868. /*
  1869. * Boolean false or empty array blanks out ORDER BY,
  1870. * while leaving the value unset or otherwise empty sets the default.
  1871. */
  1872. if ( isset( $q['orderby'] ) && ( is_array( $q['orderby'] ) || false === $q['orderby'] ) ) {
  1873. $orderby = '';
  1874. } else {
  1875. $orderby = "{$wpdb->posts}.post_date " . $q['order'];
  1876. }
  1877. } elseif ( 'none' == $q['orderby'] ) {
  1878. $orderby = '';
  1879. } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) {
  1880. $orderby = "FIELD( {$wpdb->posts}.ID, $post__in )";
  1881. } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) {
  1882. $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";
  1883. } elseif ( $q['orderby'] == 'post_name__in' && ! empty( $post_name__in ) ) {
  1884. $orderby = "FIELD( {$wpdb->posts}.post_name, $post_name__in )";
  1885. } else {
  1886. $orderby_array = array();
  1887. if ( is_array( $q['orderby'] ) ) {
  1888. foreach ( $q['orderby'] as $_orderby => $order ) {
  1889. $orderby = addslashes_gpc( urldecode( $_orderby ) );
  1890. $parsed = $this->parse_orderby( $orderby );
  1891. if ( ! $parsed ) {
  1892. continue;
  1893. }
  1894. $orderby_array[] = $parsed . ' ' . $this->parse_order( $order );
  1895. }
  1896. $orderby = implode( ', ', $orderby_array );
  1897. } else {
  1898. $q['orderby'] = urldecode( $q['orderby'] );
  1899. $q['orderby'] = addslashes_gpc( $q['orderby'] );
  1900. foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) {
  1901. $parsed = $this->parse_orderby( $orderby );
  1902. // Only allow certain values for safety.
  1903. if ( ! $parsed ) {
  1904. continue;
  1905. }
  1906. $orderby_array[] = $parsed;
  1907. }
  1908. $orderby = implode( ' ' . $q['order'] . ', ', $orderby_array );
  1909. if ( empty( $orderby ) ) {
  1910. $orderby = "{$wpdb->posts}.post_date " . $q['order'];
  1911. } elseif ( ! empty( $q['order'] ) ) {
  1912. $orderby .= " {$q['order']}";
  1913. }
  1914. }
  1915. }
  1916. // Order search results by relevance only when another "orderby" is not specified in the query.
  1917. if ( ! empty( $q['s'] ) ) {
  1918. $search_orderby = '';
  1919. if ( ! empty( $q['search_orderby_title'] ) && ( empty( $q['orderby'] ) && ! $this->is_feed ) || ( isset( $q['orderby'] ) && 'relevance' === $q['orderby'] ) )
  1920. $search_orderby = $this->parse_search_order( $q );
  1921. if ( ! $q['suppress_filters'] ) {
  1922. /**
  1923. * Filters the ORDER BY used when ordering search results.
  1924. *
  1925. * @since 3.7.0
  1926. *
  1927. * @param string $search_orderby The ORDER BY clause.
  1928. * @param WP_Query $this The current WP_Query instance.
  1929. */
  1930. $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this );
  1931. }
  1932. if ( $search_orderby )
  1933. $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby;
  1934. }
  1935. if ( is_array( $post_type ) && count( $post_type ) > 1 ) {
  1936. $post_type_cap = 'multiple_post_type';
  1937. } else {
  1938. if ( is_array( $post_type ) )
  1939. $post_type = reset( $post_type );
  1940. $post_type_object = get_post_type_object( $post_type );
  1941. if ( empty( $post_type_object ) )
  1942. $post_type_cap = $post_type;
  1943. }
  1944. if ( isset( $q['post_password'] ) ) {
  1945. $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_password = %s", $q['post_password'] );
  1946. if ( empty( $q['perm'] ) ) {
  1947. $q['perm'] = 'readable';
  1948. }
  1949. } elseif ( isset( $q['has_password'] ) ) {
  1950. $where .= sprintf( " AND {$wpdb->posts}.post_password %s ''", $q['has_password'] ? '!=' : '=' );
  1951. }
  1952. if ( ! empty( $q['comment_status'] ) ) {
  1953. $where .= $wpdb->prepare( " AND {$wpdb->posts}.comment_status = %s ", $q['comment_status'] );
  1954. }
  1955. if ( ! empty( $q['ping_status'] ) ) {
  1956. $where .= $wpdb->prepare( " AND {$wpdb->posts}.ping_status = %s ", $q['ping_status'] );
  1957. }
  1958. if ( 'any' == $post_type ) {
  1959. $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
  1960. if ( empty( $in_search_post_types ) ) {
  1961. $where .= ' AND 1=0 ';
  1962. } else {
  1963. $where .= " AND {$wpdb->posts}.post_type IN ('" . join( "', '", array_map( 'esc_sql', $in_search_post_types ) ) . "')";
  1964. }
  1965. } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
  1966. $where .= " AND {$wpdb->posts}.post_type IN ('" . join("', '", esc_sql( $post_type ) ) . "')";
  1967. } elseif ( ! empty( $post_type ) ) {
  1968. $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_type = %s", $post_type );
  1969. $post_type_object = get_post_type_object ( $post_type );
  1970. } elseif ( $this->is_attachment ) {
  1971. $where .= " AND {$wpdb->posts}.post_type = 'attachment'";
  1972. $post_type_object = get_post_type_object ( 'attachment' );
  1973. } elseif ( $this->is_page ) {
  1974. $where .= " AND {$wpdb->posts}.post_type = 'page'";
  1975. $post_type_object = get_post_type_object ( 'page' );
  1976. } else {
  1977. $where .= " AND {$wpdb->posts}.post_type = 'post'";
  1978. $post_type_object = get_post_type_object ( 'post' );
  1979. }
  1980. $edit_cap = 'edit_post';
  1981. $read_cap = 'read_post';
  1982. if ( ! empty( $post_type_object ) ) {
  1983. $edit_others_cap = $post_type_object->cap->edit_others_posts;
  1984. $read_private_cap = $post_type_object->cap->read_private_posts;
  1985. } else {
  1986. $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
  1987. $read_private_cap = 'read_private_' . $post_type_cap . 's';
  1988. }
  1989. $user_id = get_current_user_id();
  1990. $q_status = array();
  1991. if ( ! empty( $q['post_status'] ) ) {
  1992. $statuswheres = array();
  1993. $q_status = $q['post_status'];
  1994. if ( ! is_array( $q_status ) )
  1995. $q_status = explode(',', $q_status);
  1996. $r_status = array();
  1997. $p_status = array();
  1998. $e_status = array();
  1999. if ( in_array( 'any', $q_status ) ) {
  2000. foreach ( get_post_stati( array( 'exclude_from_search' => true ) ) as $status ) {
  2001. if ( ! in_array( $status, $q_status ) ) {
  2002. $e_status[] = "{$wpdb->posts}.post_status <> '$status'";
  2003. }
  2004. }
  2005. } else {
  2006. foreach ( get_post_stati() as $status ) {
  2007. if ( in_array( $status, $q_status ) ) {
  2008. if ( 'private' == $status ) {
  2009. $p_status[] = "{$wpdb->posts}.post_status = '$status'";
  2010. } else {
  2011. $r_status[] = "{$wpdb->posts}.post_status = '$status'";
  2012. }
  2013. }
  2014. }
  2015. }
  2016. if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
  2017. $r_status = array_merge($r_status, $p_status);
  2018. unset($p_status);
  2019. }
  2020. if ( !empty($e_status) ) {
  2021. $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
  2022. }
  2023. if ( !empty($r_status) ) {
  2024. if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) ) {
  2025. $statuswheres[] = "({$wpdb->posts}.post_author = $user_id " . "AND (" . join( ' OR ', $r_status ) . "))";
  2026. } else {
  2027. $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
  2028. }
  2029. }
  2030. if ( !empty($p_status) ) {
  2031. if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) ) {
  2032. $statuswheres[] = "({$wpdb->posts}.post_author = $user_id " . "AND (" . join( ' OR ', $p_status ) . "))";
  2033. } else {
  2034. $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
  2035. }
  2036. }
  2037. if ( $post_status_join ) {
  2038. $join .= " LEFT JOIN {$wpdb->posts} AS p2 ON ({$wpdb->posts}.post_parent = p2.ID) ";
  2039. foreach ( $statuswheres as $index => $statuswhere ) {
  2040. $statuswheres[$index] = "($statuswhere OR ({$wpdb->posts}.post_status = 'inherit' AND " . str_replace( $wpdb->posts, 'p2', $statuswhere ) . "))";
  2041. }
  2042. }
  2043. $where_status = implode( ' OR ', $statuswheres );
  2044. if ( ! empty( $where_status ) ) {
  2045. $where .= " AND ($where_status)";
  2046. }
  2047. } elseif ( !$this->is_singular ) {
  2048. $where .= " AND ({$wpdb->posts}.post_status = 'publish'";
  2049. // Add public states.
  2050. $public_states = get_post_stati( array('public' => true) );
  2051. foreach ( (array) $public_states as $state ) {
  2052. if ( 'publish' == $state ) // Publish is hard-coded above.
  2053. continue;
  2054. $where .= " OR {$wpdb->posts}.post_status = '$state'";
  2055. }
  2056. if ( $this->is_admin ) {
  2057. // Add protected states that should show in the admin all list.
  2058. $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
  2059. foreach ( (array) $admin_all_states as $state ) {
  2060. $where .= " OR {$wpdb->posts}.post_status = '$state'";
  2061. }
  2062. }
  2063. if ( is_user_logged_in() ) {
  2064. // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
  2065. $private_states = get_post_stati( array('private' => true) );
  2066. foreach ( (array) $private_states as $state ) {
  2067. $where .= current_user_can( $read_private_cap ) ? " OR {$wpdb->posts}.post_status = '$state'" : " OR {$wpdb->posts}.post_author = $user_id AND {$wpdb->posts}.post_status = '$state'";
  2068. }
  2069. }
  2070. $where .= ')';
  2071. }
  2072. /*
  2073. * Apply filters on where and join prior to paging so that any
  2074. * manipulations to them are reflected in the paging by day queries.
  2075. */
  2076. if ( !$q['suppress_filters'] ) {
  2077. /**
  2078. * Filters the WHERE clause of the query.
  2079. *
  2080. * @since 1.5.0
  2081. *
  2082. * @param string $where The WHERE clause of the query.
  2083. * @param WP_Query $this The WP_Query instance (passed by reference).
  2084. */
  2085. $where = apply_filters_ref_array( 'posts_where', array( $where, &$this ) );
  2086. /**
  2087. * Filters the JOIN clause of the query.
  2088. *
  2089. * @since 1.5.0
  2090. *
  2091. * @param string $join The JOIN clause of the query.
  2092. * @param WP_Query $this The WP_Query instance (passed by reference).
  2093. */
  2094. $join = apply_filters_ref_array( 'posts_join', array( $join, &$this ) );
  2095. }
  2096. // Paging
  2097. if ( empty($q['nopaging']) && !$this->is_singular ) {
  2098. $page = absint($q['paged']);
  2099. if ( !$page )
  2100. $page = 1;
  2101. // If 'offset' is provided, it takes precedence over 'paged'.
  2102. if ( isset( $q['offset'] ) && is_numeric( $q['offset'] ) ) {
  2103. $q['offset'] = absint( $q['offset'] );
  2104. $pgstrt = $q['offset'] . ', ';
  2105. } else {
  2106. $pgstrt = absint( ( $page - 1 ) * $q['posts_per_page'] ) . ', ';
  2107. }
  2108. $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
  2109. }
  2110. // Comments feeds
  2111. if ( $this->is_comment_feed && ! $this->is_singular ) {
  2112. if ( $this->is_archive || $this->is_search ) {
  2113. $cjoin = "JOIN {$wpdb->posts} ON ({$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID) $join ";
  2114. $cwhere = "WHERE comment_approved = '1' $where";
  2115. $cgroupby = "{$wpdb->comments}.comment_id";
  2116. } else { // Other non singular e.g. front
  2117. $cjoin = "JOIN {$wpdb->posts} ON ( {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID )";
  2118. $cwhere = "WHERE ( post_status = 'publish' OR ( post_status = 'inherit' AND post_type = 'attachment' ) ) AND comment_approved = '1'";
  2119. $cgroupby = '';
  2120. }
  2121. if ( !$q['suppress_filters'] ) {
  2122. /**
  2123. * Filters the JOIN clause of the comments feed query before sending.
  2124. *
  2125. * @since 2.2.0
  2126. *
  2127. * @param string $cjoin The JOIN clause of the query.
  2128. * @param WP_Query $this The WP_Query instance (passed by reference).
  2129. */
  2130. $cjoin = apply_filters_ref_array( 'comment_feed_join', array( $cjoin, &$this ) );
  2131. /**
  2132. * Filters the WHERE clause of the comments feed query before sending.
  2133. *
  2134. * @since 2.2.0
  2135. *
  2136. * @param string $cwhere The WHERE clause of the query.
  2137. * @param WP_Query $this The WP_Query instance (passed by reference).
  2138. */
  2139. $cwhere = apply_filters_ref_array( 'comment_feed_where', array( $cwhere, &$this ) );
  2140. /**
  2141. * Filters the GROUP BY clause of the comments feed query before sending.
  2142. *
  2143. * @since 2.2.0
  2144. *
  2145. * @param string $cgroupby The GROUP BY clause of the query.
  2146. * @param WP_Query $this The WP_Query instance (passed by reference).
  2147. */
  2148. $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( $cgroupby, &$this ) );
  2149. /**
  2150. * Filters the ORDER BY clause of the comments feed query before sending.
  2151. *
  2152. * @since 2.8.0
  2153. *
  2154. * @param string $corderby The ORDER BY clause of the query.
  2155. * @param WP_Query $this The WP_Query instance (passed by reference).
  2156. */
  2157. $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
  2158. /**
  2159. * Filters the LIMIT clause of the comments feed query before sending.
  2160. *
  2161. * @since 2.8.0
  2162. *
  2163. * @param string $climits The JOIN clause of the query.
  2164. * @param WP_Query $this The WP_Query instance (passed by reference).
  2165. */
  2166. $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
  2167. }
  2168. $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  2169. $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  2170. $comments = (array) $wpdb->get_results("SELECT $distinct {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits");
  2171. // Convert to WP_Comment
  2172. $this->comments = array_map( 'get_comment', $comments );
  2173. $this->comment_count = count($this->comments);
  2174. $post_ids = array();
  2175. foreach ( $this->comments as $comment )
  2176. $post_ids[] = (int) $comment->comment_post_ID;
  2177. $post_ids = join(',', $post_ids);
  2178. $join = '';
  2179. if ( $post_ids ) {
  2180. $where = "AND {$wpdb->posts}.ID IN ($post_ids) ";
  2181. } else {
  2182. $where = "AND 0";
  2183. }
  2184. }
  2185. $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
  2186. /*
  2187. * Apply post-paging filters on where and join. Only plugins that
  2188. * manipulate paging queries should use these hooks.
  2189. */
  2190. if ( !$q['suppress_filters'] ) {
  2191. /**
  2192. * Filters the WHERE clause of the query.
  2193. *
  2194. * Specifically for manipulating paging queries.
  2195. *
  2196. * @since 1.5.0
  2197. *
  2198. * @param string $where The WHERE clause of the query.
  2199. * @param WP_Query $this The WP_Query instance (passed by reference).
  2200. */
  2201. $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
  2202. /**
  2203. * Filters the GROUP BY clause of the query.
  2204. *
  2205. * @since 2.0.0
  2206. *
  2207. * @param string $groupby The GROUP BY clause of the query.
  2208. * @param WP_Query $this The WP_Query instance (passed by reference).
  2209. */
  2210. $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) );
  2211. /**
  2212. * Filters the JOIN clause of the query.
  2213. *
  2214. * Specifically for manipulating paging queries.
  2215. *
  2216. * @since 1.5.0
  2217. *
  2218. * @param string $join The JOIN clause of the query.
  2219. * @param WP_Query $this The WP_Query instance (passed by reference).
  2220. */
  2221. $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) );
  2222. /**
  2223. * Filters the ORDER BY clause of the query.
  2224. *
  2225. * @since 1.5.1
  2226. *
  2227. * @param string $orderby The ORDER BY clause of the query.
  2228. * @param WP_Query $this The WP_Query instance (passed by reference).
  2229. */
  2230. $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
  2231. /**
  2232. * Filters the DISTINCT clause of the query.
  2233. *
  2234. * @since 2.1.0
  2235. *
  2236. * @param string $distinct The DISTINCT clause of the query.
  2237. * @param WP_Query $this The WP_Query instance (passed by reference).
  2238. */
  2239. $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) );
  2240. /**
  2241. * Filters the LIMIT clause of the query.
  2242. *
  2243. * @since 2.1.0
  2244. *
  2245. * @param string $limits The LIMIT clause of the query.
  2246. * @param WP_Query $this The WP_Query instance (passed by reference).
  2247. */
  2248. $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) );
  2249. /**
  2250. * Filters the SELECT clause of the query.
  2251. *
  2252. * @since 2.1.0
  2253. *
  2254. * @param string $fields The SELECT clause of the query.
  2255. * @param WP_Query $this The WP_Query instance (passed by reference).
  2256. */
  2257. $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) );
  2258. /**
  2259. * Filters all query clauses at once, for convenience.
  2260. *
  2261. * Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT,
  2262. * fields (SELECT), and LIMITS clauses.
  2263. *
  2264. * @since 3.1.0
  2265. *
  2266. * @param array $clauses The list of clauses for the query.
  2267. * @param WP_Query $this The WP_Query instance (passed by reference).
  2268. */
  2269. $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
  2270. $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';
  2271. $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : '';
  2272. $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';
  2273. $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';
  2274. $distinct = isset( $clauses[ 'distinct' ] ) ? $clauses[ 'distinct' ] : '';
  2275. $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';
  2276. $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';
  2277. }
  2278. /**
  2279. * Fires to announce the query's current selection parameters.
  2280. *
  2281. * For use by caching plugins.
  2282. *
  2283. * @since 2.3.0
  2284. *
  2285. * @param string $selection The assembled selection query.
  2286. */
  2287. do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
  2288. /*
  2289. * Filters again for the benefit of caching plugins.
  2290. * Regular plugins should use the hooks above.
  2291. */
  2292. if ( !$q['suppress_filters'] ) {
  2293. /**
  2294. * Filters the WHERE clause of the query.
  2295. *
  2296. * For use by caching plugins.
  2297. *
  2298. * @since 2.5.0
  2299. *
  2300. * @param string $where The WHERE clause of the query.
  2301. * @param WP_Query $this The WP_Query instance (passed by reference).
  2302. */
  2303. $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) );
  2304. /**
  2305. * Filters the GROUP BY clause of the query.
  2306. *
  2307. * For use by caching plugins.
  2308. *
  2309. * @since 2.5.0
  2310. *
  2311. * @param string $groupby The GROUP BY clause of the query.
  2312. * @param WP_Query $this The WP_Query instance (passed by reference).
  2313. */
  2314. $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) );
  2315. /**
  2316. * Filters the JOIN clause of the query.
  2317. *
  2318. * For use by caching plugins.
  2319. *
  2320. * @since 2.5.0
  2321. *
  2322. * @param string $join The JOIN clause of the query.
  2323. * @param WP_Query $this The WP_Query instance (passed by reference).
  2324. */
  2325. $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) );
  2326. /**
  2327. * Filters the ORDER BY clause of the query.
  2328. *
  2329. * For use by caching plugins.
  2330. *
  2331. * @since 2.5.0
  2332. *
  2333. * @param string $orderby The ORDER BY clause of the query.
  2334. * @param WP_Query $this The WP_Query instance (passed by reference).
  2335. */
  2336. $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) );
  2337. /**
  2338. * Filters the DISTINCT clause of the query.
  2339. *
  2340. * For use by caching plugins.
  2341. *
  2342. * @since 2.5.0
  2343. *
  2344. * @param string $distinct The DISTINCT clause of the query.
  2345. * @param WP_Query $this The WP_Query instance (passed by reference).
  2346. */
  2347. $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) );
  2348. /**
  2349. * Filters the SELECT clause of the query.
  2350. *
  2351. * For use by caching plugins.
  2352. *
  2353. * @since 2.5.0
  2354. *
  2355. * @param string $fields The SELECT clause of the query.
  2356. * @param WP_Query $this The WP_Query instance (passed by reference).
  2357. */
  2358. $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) );
  2359. /**
  2360. * Filters the LIMIT clause of the query.
  2361. *
  2362. * For use by caching plugins.
  2363. *
  2364. * @since 2.5.0
  2365. *
  2366. * @param string $limits The LIMIT clause of the query.
  2367. * @param WP_Query $this The WP_Query instance (passed by reference).
  2368. */
  2369. $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) );
  2370. /**
  2371. * Filters all query clauses at once, for convenience.
  2372. *
  2373. * For use by caching plugins.
  2374. *
  2375. * Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT,
  2376. * fields (SELECT), and LIMITS clauses.
  2377. *
  2378. * @since 3.1.0
  2379. *
  2380. * @param array $pieces The pieces of the query.
  2381. * @param WP_Query $this The WP_Query instance (passed by reference).
  2382. */
  2383. $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
  2384. $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : '';
  2385. $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : '';
  2386. $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : '';
  2387. $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : '';
  2388. $distinct = isset( $clauses[ 'distinct' ] ) ? $clauses[ 'distinct' ] : '';
  2389. $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : '';
  2390. $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : '';
  2391. }
  2392. if ( ! empty($groupby) )
  2393. $groupby = 'GROUP BY ' . $groupby;
  2394. if ( !empty( $orderby ) )
  2395. $orderby = 'ORDER BY ' . $orderby;
  2396. $found_rows = '';
  2397. if ( !$q['no_found_rows'] && !empty($limits) )
  2398. $found_rows = 'SQL_CALC_FOUND_ROWS';
  2399. $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM {$wpdb->posts} $join WHERE 1=1 $where $groupby $orderby $limits";
  2400. if ( !$q['suppress_filters'] ) {
  2401. /**
  2402. * Filters the completed SQL query before sending.
  2403. *
  2404. * @since 2.0.0
  2405. *
  2406. * @param string $request The complete SQL query.
  2407. * @param WP_Query $this The WP_Query instance (passed by reference).
  2408. */
  2409. $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) );
  2410. }
  2411. /**
  2412. * Filters the posts array before the query takes place.
  2413. *
  2414. * Return a non-null value to bypass WordPress's default post queries.
  2415. *
  2416. * Filtering functions that require pagination information are encouraged to set
  2417. * the `found_posts` and `max_num_pages` properties of the WP_Query object,
  2418. * passed to the filter by reference. If WP_Query does not perform a database
  2419. * query, it will not have enough information to generate these values itself.
  2420. *
  2421. * @since 4.6.0
  2422. *
  2423. * @param array|null $posts Return an array of post data to short-circuit WP's query,
  2424. * or null to allow WP to run its normal queries.
  2425. * @param WP_Query $this The WP_Query instance (passed by reference).
  2426. */
  2427. $this->posts = apply_filters_ref_array( 'posts_pre_query', array( null, &$this ) );
  2428. if ( 'ids' == $q['fields'] ) {
  2429. if ( null === $this->posts ) {
  2430. $this->posts = $wpdb->get_col( $this->request );
  2431. }
  2432. $this->posts = array_map( 'intval', $this->posts );
  2433. $this->post_count = count( $this->posts );
  2434. $this->set_found_posts( $q, $limits );
  2435. return $this->posts;
  2436. }
  2437. if ( 'id=>parent' == $q['fields'] ) {
  2438. if ( null === $this->posts ) {
  2439. $this->posts = $wpdb->get_results( $this->request );
  2440. }
  2441. $this->post_count = count( $this->posts );
  2442. $this->set_found_posts( $q, $limits );
  2443. $r = array();
  2444. foreach ( $this->posts as $key => $post ) {
  2445. $this->posts[ $key ]->ID = (int) $post->ID;
  2446. $this->posts[ $key ]->post_parent = (int) $post->post_parent;
  2447. $r[ (int) $post->ID ] = (int) $post->post_parent;
  2448. }
  2449. return $r;
  2450. }
  2451. if ( null === $this->posts ) {
  2452. $split_the_query = ( $old_request == $this->request && "{$wpdb->posts}.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
  2453. /**
  2454. * Filters whether to split the query.
  2455. *
  2456. * Splitting the query will cause it to fetch just the IDs of the found posts
  2457. * (and then individually fetch each post by ID), rather than fetching every
  2458. * complete row at once. One massive result vs. many small results.
  2459. *
  2460. * @since 3.4.0
  2461. *
  2462. * @param bool $split_the_query Whether or not to split the query.
  2463. * @param WP_Query $this The WP_Query instance.
  2464. */
  2465. $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this );
  2466. if ( $split_the_query ) {
  2467. // First get the IDs and then fill in the objects
  2468. $this->request = "SELECT $found_rows $distinct {$wpdb->posts}.ID FROM {$wpdb->posts} $join WHERE 1=1 $where $groupby $orderby $limits";
  2469. /**
  2470. * Filters the Post IDs SQL request before sending.
  2471. *
  2472. * @since 3.4.0
  2473. *
  2474. * @param string $request The post ID request.
  2475. * @param WP_Query $this The WP_Query instance.
  2476. */
  2477. $this->request = apply_filters( 'posts_request_ids', $this->request, $this );
  2478. $ids = $wpdb->get_col( $this->request );
  2479. if ( $ids ) {
  2480. $this->posts = $ids;
  2481. $this->set_found_posts( $q, $limits );
  2482. _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
  2483. } else {
  2484. $this->posts = array();
  2485. }
  2486. } else {
  2487. $this->posts = $wpdb->get_results( $this->request );
  2488. $this->set_found_posts( $q, $limits );
  2489. }
  2490. }
  2491. // Convert to WP_Post objects.
  2492. if ( $this->posts ) {
  2493. $this->posts = array_map( 'get_post', $this->posts );
  2494. }
  2495. if ( ! $q['suppress_filters'] ) {
  2496. /**
  2497. * Filters the raw post results array, prior to status checks.
  2498. *
  2499. * @since 2.3.0
  2500. *
  2501. * @param array $posts The post results array.
  2502. * @param WP_Query $this The WP_Query instance (passed by reference).
  2503. */
  2504. $this->posts = apply_filters_ref_array( 'posts_results', array( $this->posts, &$this ) );
  2505. }
  2506. if ( !empty($this->posts) && $this->is_comment_feed && $this->is_singular ) {
  2507. /** This filter is documented in wp-includes/query.php */
  2508. $cjoin = apply_filters_ref_array( 'comment_feed_join', array( '', &$this ) );
  2509. /** This filter is documented in wp-includes/query.php */
  2510. $cwhere = apply_filters_ref_array( 'comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
  2511. /** This filter is documented in wp-includes/query.php */
  2512. $cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( '', &$this ) );
  2513. $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
  2514. /** This filter is documented in wp-includes/query.php */
  2515. $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) );
  2516. $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
  2517. /** This filter is documented in wp-includes/query.php */
  2518. $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
  2519. $comments_request = "SELECT {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits";
  2520. $comments = $wpdb->get_results($comments_request);
  2521. // Convert to WP_Comment
  2522. $this->comments = array_map( 'get_comment', $comments );
  2523. $this->comment_count = count($this->comments);
  2524. }
  2525. // Check post status to determine if post should be displayed.
  2526. if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
  2527. $status = get_post_status($this->posts[0]);
  2528. if ( 'attachment' === $this->posts[0]->post_type && 0 === (int) $this->posts[0]->post_parent ) {
  2529. $this->is_page = false;
  2530. $this->is_single = true;
  2531. $this->is_attachment = true;
  2532. }
  2533. $post_status_obj = get_post_status_object($status);
  2534. // If the post_status was specifically requested, let it pass through.
  2535. if ( !$post_status_obj->public && ! in_array( $status, $q_status ) ) {
  2536. if ( ! is_user_logged_in() ) {
  2537. // User must be logged in to view unpublished posts.
  2538. $this->posts = array();
  2539. } else {
  2540. if ( $post_status_obj->protected ) {
  2541. // User must have edit permissions on the draft to preview.
  2542. if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) {
  2543. $this->posts = array();
  2544. } else {
  2545. $this->is_preview = true;
  2546. if ( 'future' != $status )
  2547. $this->posts[0]->post_date = current_time('mysql');
  2548. }
  2549. } elseif ( $post_status_obj->private ) {
  2550. if ( ! current_user_can($read_cap, $this->posts[0]->ID) )
  2551. $this->posts = array();
  2552. } else {
  2553. $this->posts = array();
  2554. }
  2555. }
  2556. }
  2557. if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) ) {
  2558. /**
  2559. * Filters the single post for preview mode.
  2560. *
  2561. * @since 2.7.0
  2562. *
  2563. * @param WP_Post $post_preview The Post object.
  2564. * @param WP_Query $this The WP_Query instance (passed by reference).
  2565. */
  2566. $this->posts[0] = get_post( apply_filters_ref_array( 'the_preview', array( $this->posts[0], &$this ) ) );
  2567. }
  2568. }
  2569. // Put sticky posts at the top of the posts array
  2570. $sticky_posts = get_option('sticky_posts');
  2571. if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) {
  2572. $num_posts = count($this->posts);
  2573. $sticky_offset = 0;
  2574. // Loop over posts and relocate stickies to the front.
  2575. for ( $i = 0; $i < $num_posts; $i++ ) {
  2576. if ( in_array($this->posts[$i]->ID, $sticky_posts) ) {
  2577. $sticky_post = $this->posts[$i];
  2578. // Remove sticky from current position
  2579. array_splice($this->posts, $i, 1);
  2580. // Move to front, after other stickies
  2581. array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
  2582. // Increment the sticky offset. The next sticky will be placed at this offset.
  2583. $sticky_offset++;
  2584. // Remove post from sticky posts array
  2585. $offset = array_search($sticky_post->ID, $sticky_posts);
  2586. unset( $sticky_posts[$offset] );
  2587. }
  2588. }
  2589. // If any posts have been excluded specifically, Ignore those that are sticky.
  2590. if ( !empty($sticky_posts) && !empty($q['post__not_in']) )
  2591. $sticky_posts = array_diff($sticky_posts, $q['post__not_in']);
  2592. // Fetch sticky posts that weren't in the query results
  2593. if ( !empty($sticky_posts) ) {
  2594. $stickies = get_posts( array(
  2595. 'post__in' => $sticky_posts,
  2596. 'post_type' => $post_type,
  2597. 'post_status' => 'publish',
  2598. 'nopaging' => true
  2599. ) );
  2600. foreach ( $stickies as $sticky_post ) {
  2601. array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) );
  2602. $sticky_offset++;
  2603. }
  2604. }
  2605. }
  2606. // If comments have been fetched as part of the query, make sure comment meta lazy-loading is set up.
  2607. if ( ! empty( $this->comments ) ) {
  2608. wp_queue_comments_for_comment_meta_lazyload( $this->comments );
  2609. }
  2610. if ( ! $q['suppress_filters'] ) {
  2611. /**
  2612. * Filters the array of retrieved posts after they've been fetched and
  2613. * internally processed.
  2614. *
  2615. * @since 1.5.0
  2616. *
  2617. * @param array $posts The array of retrieved posts.
  2618. * @param WP_Query $this The WP_Query instance (passed by reference).
  2619. */
  2620. $this->posts = apply_filters_ref_array( 'the_posts', array( $this->posts, &$this ) );
  2621. }
  2622. // Ensure that any posts added/modified via one of the filters above are
  2623. // of the type WP_Post and are filtered.
  2624. if ( $this->posts ) {
  2625. $this->post_count = count( $this->posts );
  2626. $this->posts = array_map( 'get_post', $this->posts );
  2627. if ( $q['cache_results'] )
  2628. update_post_caches($this->posts, $post_type, $q['update_post_term_cache'], $q['update_post_meta_cache']);
  2629. $this->post = reset( $this->posts );
  2630. } else {
  2631. $this->post_count = 0;
  2632. $this->posts = array();
  2633. }
  2634. if ( $q['lazy_load_term_meta'] ) {
  2635. wp_queue_posts_for_term_meta_lazyload( $this->posts );
  2636. }
  2637. return $this->posts;
  2638. }
  2639. /**
  2640. * Set up the amount of found posts and the number of pages (if limit clause was used)
  2641. * for the current query.
  2642. *
  2643. * @since 3.5.0
  2644. *
  2645. * @param array $q Query variables.
  2646. * @param string $limits LIMIT clauses of the query.
  2647. */
  2648. private function set_found_posts( $q, $limits ) {
  2649. global $wpdb;
  2650. // Bail if posts is an empty array. Continue if posts is an empty string,
  2651. // null, or false to accommodate caching plugins that fill posts later.
  2652. if ( $q['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) )
  2653. return;
  2654. if ( ! empty( $limits ) ) {
  2655. /**
  2656. * Filters the query to run for retrieving the found posts.
  2657. *
  2658. * @since 2.1.0
  2659. *
  2660. * @param string $found_posts The query to run to find the found posts.
  2661. * @param WP_Query $this The WP_Query instance (passed by reference).
  2662. */
  2663. $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
  2664. } else {
  2665. if ( is_array( $this->posts ) ) {
  2666. $this->found_posts = count( $this->posts );
  2667. } else {
  2668. if ( null === $this->posts ) {
  2669. $this->found_posts = 0;
  2670. } else {
  2671. $this->found_posts = 1;
  2672. }
  2673. }
  2674. }
  2675. /**
  2676. * Filters the number of found posts for the query.
  2677. *
  2678. * @since 2.1.0
  2679. *
  2680. * @param int $found_posts The number of posts found.
  2681. * @param WP_Query $this The WP_Query instance (passed by reference).
  2682. */
  2683. $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
  2684. if ( ! empty( $limits ) )
  2685. $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
  2686. }
  2687. /**
  2688. * Set up the next post and iterate current post index.
  2689. *
  2690. * @since 1.5.0
  2691. *
  2692. * @return WP_Post Next post.
  2693. */
  2694. public function next_post() {
  2695. $this->current_post++;
  2696. $this->post = $this->posts[$this->current_post];
  2697. return $this->post;
  2698. }
  2699. /**
  2700. * Sets up the current post.
  2701. *
  2702. * Retrieves the next post, sets up the post, sets the 'in the loop'
  2703. * property to true.
  2704. *
  2705. * @since 1.5.0
  2706. *
  2707. * @global WP_Post $post
  2708. */
  2709. public function the_post() {
  2710. global $post;
  2711. $this->in_the_loop = true;
  2712. if ( $this->current_post == -1 ) // loop has just started
  2713. /**
  2714. * Fires once the loop is started.
  2715. *
  2716. * @since 2.0.0
  2717. *
  2718. * @param WP_Query $this The WP_Query instance (passed by reference).
  2719. */
  2720. do_action_ref_array( 'loop_start', array( &$this ) );
  2721. $post = $this->next_post();
  2722. $this->setup_postdata( $post );
  2723. }
  2724. /**
  2725. * Determines whether there are more posts available in the loop.
  2726. *
  2727. * Calls the {@see 'loop_end'} action when the loop is complete.
  2728. *
  2729. * @since 1.5.0
  2730. *
  2731. * @return bool True if posts are available, false if end of loop.
  2732. */
  2733. public function have_posts() {
  2734. if ( $this->current_post + 1 < $this->post_count ) {
  2735. return true;
  2736. } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) {
  2737. /**
  2738. * Fires once the loop has ended.
  2739. *
  2740. * @since 2.0.0
  2741. *
  2742. * @param WP_Query $this The WP_Query instance (passed by reference).
  2743. */
  2744. do_action_ref_array( 'loop_end', array( &$this ) );
  2745. // Do some cleaning up after the loop
  2746. $this->rewind_posts();
  2747. } elseif ( 0 === $this->post_count ) {
  2748. /**
  2749. * Fires if no results are found in a post query.
  2750. *
  2751. * @since 4.9.0
  2752. *
  2753. * @param WP_Query $this The WP_Query instance.
  2754. */
  2755. do_action( 'loop_no_results', $this );
  2756. }
  2757. $this->in_the_loop = false;
  2758. return false;
  2759. }
  2760. /**
  2761. * Rewind the posts and reset post index.
  2762. *
  2763. * @since 1.5.0
  2764. */
  2765. public function rewind_posts() {
  2766. $this->current_post = -1;
  2767. if ( $this->post_count > 0 ) {
  2768. $this->post = $this->posts[0];
  2769. }
  2770. }
  2771. /**
  2772. * Iterate current comment index and return WP_Comment object.
  2773. *
  2774. * @since 2.2.0
  2775. *
  2776. * @return WP_Comment Comment object.
  2777. */
  2778. public function next_comment() {
  2779. $this->current_comment++;
  2780. $this->comment = $this->comments[$this->current_comment];
  2781. return $this->comment;
  2782. }
  2783. /**
  2784. * Sets up the current comment.
  2785. *
  2786. * @since 2.2.0
  2787. * @global WP_Comment $comment Current comment.
  2788. */
  2789. public function the_comment() {
  2790. global $comment;
  2791. $comment = $this->next_comment();
  2792. if ( $this->current_comment == 0 ) {
  2793. /**
  2794. * Fires once the comment loop is started.
  2795. *
  2796. * @since 2.2.0
  2797. */
  2798. do_action( 'comment_loop_start' );
  2799. }
  2800. }
  2801. /**
  2802. * Whether there are more comments available.
  2803. *
  2804. * Automatically rewinds comments when finished.
  2805. *
  2806. * @since 2.2.0
  2807. *
  2808. * @return bool True, if more comments. False, if no more posts.
  2809. */
  2810. public function have_comments() {
  2811. if ( $this->current_comment + 1 < $this->comment_count ) {
  2812. return true;
  2813. } elseif ( $this->current_comment + 1 == $this->comment_count ) {
  2814. $this->rewind_comments();
  2815. }
  2816. return false;
  2817. }
  2818. /**
  2819. * Rewind the comments, resets the comment index and comment to first.
  2820. *
  2821. * @since 2.2.0
  2822. */
  2823. public function rewind_comments() {
  2824. $this->current_comment = -1;
  2825. if ( $this->comment_count > 0 ) {
  2826. $this->comment = $this->comments[0];
  2827. }
  2828. }
  2829. /**
  2830. * Sets up the WordPress query by parsing query string.
  2831. *
  2832. * @since 1.5.0
  2833. *
  2834. * @param string|array $query URL query string or array of query arguments.
  2835. * @return array List of posts.
  2836. */
  2837. public function query( $query ) {
  2838. $this->init();
  2839. $this->query = $this->query_vars = wp_parse_args( $query );
  2840. return $this->get_posts();
  2841. }
  2842. /**
  2843. * Retrieve queried object.
  2844. *
  2845. * If queried object is not set, then the queried object will be set from
  2846. * the category, tag, taxonomy, posts page, single post, page, or author
  2847. * query variable. After it is set up, it will be returned.
  2848. *
  2849. * @since 1.5.0
  2850. *
  2851. * @return object
  2852. */
  2853. public function get_queried_object() {
  2854. if ( isset($this->queried_object) )
  2855. return $this->queried_object;
  2856. $this->queried_object = null;
  2857. $this->queried_object_id = null;
  2858. if ( $this->is_category || $this->is_tag || $this->is_tax ) {
  2859. if ( $this->is_category ) {
  2860. if ( $this->get( 'cat' ) ) {
  2861. $term = get_term( $this->get( 'cat' ), 'category' );
  2862. } elseif ( $this->get( 'category_name' ) ) {
  2863. $term = get_term_by( 'slug', $this->get( 'category_name' ), 'category' );
  2864. }
  2865. } elseif ( $this->is_tag ) {
  2866. if ( $this->get( 'tag_id' ) ) {
  2867. $term = get_term( $this->get( 'tag_id' ), 'post_tag' );
  2868. } elseif ( $this->get( 'tag' ) ) {
  2869. $term = get_term_by( 'slug', $this->get( 'tag' ), 'post_tag' );
  2870. }
  2871. } else {
  2872. // For other tax queries, grab the first term from the first clause.
  2873. if ( ! empty( $this->tax_query->queried_terms ) ) {
  2874. $queried_taxonomies = array_keys( $this->tax_query->queried_terms );
  2875. $matched_taxonomy = reset( $queried_taxonomies );
  2876. $query = $this->tax_query->queried_terms[ $matched_taxonomy ];
  2877. if ( ! empty( $query['terms'] ) ) {
  2878. if ( 'term_id' == $query['field'] ) {
  2879. $term = get_term( reset( $query['terms'] ), $matched_taxonomy );
  2880. } else {
  2881. $term = get_term_by( $query['field'], reset( $query['terms'] ), $matched_taxonomy );
  2882. }
  2883. }
  2884. }
  2885. }
  2886. if ( ! empty( $term ) && ! is_wp_error( $term ) ) {
  2887. $this->queried_object = $term;
  2888. $this->queried_object_id = (int) $term->term_id;
  2889. if ( $this->is_category && 'category' === $this->queried_object->taxonomy )
  2890. _make_cat_compat( $this->queried_object );
  2891. }
  2892. } elseif ( $this->is_post_type_archive ) {
  2893. $post_type = $this->get( 'post_type' );
  2894. if ( is_array( $post_type ) )
  2895. $post_type = reset( $post_type );
  2896. $this->queried_object = get_post_type_object( $post_type );
  2897. } elseif ( $this->is_posts_page ) {
  2898. $page_for_posts = get_option('page_for_posts');
  2899. $this->queried_object = get_post( $page_for_posts );
  2900. $this->queried_object_id = (int) $this->queried_object->ID;
  2901. } elseif ( $this->is_singular && ! empty( $this->post ) ) {
  2902. $this->queried_object = $this->post;
  2903. $this->queried_object_id = (int) $this->post->ID;
  2904. } elseif ( $this->is_author ) {
  2905. $this->queried_object_id = (int) $this->get('author');
  2906. $this->queried_object = get_userdata( $this->queried_object_id );
  2907. }
  2908. return $this->queried_object;
  2909. }
  2910. /**
  2911. * Retrieve ID of the current queried object.
  2912. *
  2913. * @since 1.5.0
  2914. *
  2915. * @return int
  2916. */
  2917. public function get_queried_object_id() {
  2918. $this->get_queried_object();
  2919. if ( isset($this->queried_object_id) ) {
  2920. return $this->queried_object_id;
  2921. }
  2922. return 0;
  2923. }
  2924. /**
  2925. * Constructor.
  2926. *
  2927. * Sets up the WordPress query, if parameter is not empty.
  2928. *
  2929. * @since 1.5.0
  2930. *
  2931. * @param string|array $query URL query string or array of vars.
  2932. */
  2933. public function __construct( $query = '' ) {
  2934. if ( ! empty( $query ) ) {
  2935. $this->query( $query );
  2936. }
  2937. }
  2938. /**
  2939. * Make private properties readable for backward compatibility.
  2940. *
  2941. * @since 4.0.0
  2942. *
  2943. * @param string $name Property to get.
  2944. * @return mixed Property.
  2945. */
  2946. public function __get( $name ) {
  2947. if ( in_array( $name, $this->compat_fields ) ) {
  2948. return $this->$name;
  2949. }
  2950. }
  2951. /**
  2952. * Make private properties checkable for backward compatibility.
  2953. *
  2954. * @since 4.0.0
  2955. *
  2956. * @param string $name Property to check if set.
  2957. * @return bool Whether the property is set.
  2958. */
  2959. public function __isset( $name ) {
  2960. if ( in_array( $name, $this->compat_fields ) ) {
  2961. return isset( $this->$name );
  2962. }
  2963. }
  2964. /**
  2965. * Make private/protected methods readable for backward compatibility.
  2966. *
  2967. * @since 4.0.0
  2968. *
  2969. * @param callable $name Method to call.
  2970. * @param array $arguments Arguments to pass when calling.
  2971. * @return mixed|false Return value of the callback, false otherwise.
  2972. */
  2973. public function __call( $name, $arguments ) {
  2974. if ( in_array( $name, $this->compat_methods ) ) {
  2975. return call_user_func_array( array( $this, $name ), $arguments );
  2976. }
  2977. return false;
  2978. }
  2979. /**
  2980. * Is the query for an existing archive page?
  2981. *
  2982. * Month, Year, Category, Author, Post Type archive...
  2983. *
  2984. * @since 3.1.0
  2985. *
  2986. * @return bool
  2987. */
  2988. public function is_archive() {
  2989. return (bool) $this->is_archive;
  2990. }
  2991. /**
  2992. * Is the query for an existing post type archive page?
  2993. *
  2994. * @since 3.1.0
  2995. *
  2996. * @param mixed $post_types Optional. Post type or array of posts types to check against.
  2997. * @return bool
  2998. */
  2999. public function is_post_type_archive( $post_types = '' ) {
  3000. if ( empty( $post_types ) || ! $this->is_post_type_archive )
  3001. return (bool) $this->is_post_type_archive;
  3002. $post_type = $this->get( 'post_type' );
  3003. if ( is_array( $post_type ) )
  3004. $post_type = reset( $post_type );
  3005. $post_type_object = get_post_type_object( $post_type );
  3006. return in_array( $post_type_object->name, (array) $post_types );
  3007. }
  3008. /**
  3009. * Is the query for an existing attachment page?
  3010. *
  3011. * @since 3.1.0
  3012. *
  3013. * @param mixed $attachment Attachment ID, title, slug, or array of such.
  3014. * @return bool
  3015. */
  3016. public function is_attachment( $attachment = '' ) {
  3017. if ( ! $this->is_attachment ) {
  3018. return false;
  3019. }
  3020. if ( empty( $attachment ) ) {
  3021. return true;
  3022. }
  3023. $attachment = array_map( 'strval', (array) $attachment );
  3024. $post_obj = $this->get_queried_object();
  3025. if ( in_array( (string) $post_obj->ID, $attachment ) ) {
  3026. return true;
  3027. } elseif ( in_array( $post_obj->post_title, $attachment ) ) {
  3028. return true;
  3029. } elseif ( in_array( $post_obj->post_name, $attachment ) ) {
  3030. return true;
  3031. }
  3032. return false;
  3033. }
  3034. /**
  3035. * Is the query for an existing author archive page?
  3036. *
  3037. * If the $author parameter is specified, this function will additionally
  3038. * check if the query is for one of the authors specified.
  3039. *
  3040. * @since 3.1.0
  3041. *
  3042. * @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
  3043. * @return bool
  3044. */
  3045. public function is_author( $author = '' ) {
  3046. if ( !$this->is_author )
  3047. return false;
  3048. if ( empty($author) )
  3049. return true;
  3050. $author_obj = $this->get_queried_object();
  3051. $author = array_map( 'strval', (array) $author );
  3052. if ( in_array( (string) $author_obj->ID, $author ) )
  3053. return true;
  3054. elseif ( in_array( $author_obj->nickname, $author ) )
  3055. return true;
  3056. elseif ( in_array( $author_obj->user_nicename, $author ) )
  3057. return true;
  3058. return false;
  3059. }
  3060. /**
  3061. * Is the query for an existing category archive page?
  3062. *
  3063. * If the $category parameter is specified, this function will additionally
  3064. * check if the query is for one of the categories specified.
  3065. *
  3066. * @since 3.1.0
  3067. *
  3068. * @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
  3069. * @return bool
  3070. */
  3071. public function is_category( $category = '' ) {
  3072. if ( !$this->is_category )
  3073. return false;
  3074. if ( empty($category) )
  3075. return true;
  3076. $cat_obj = $this->get_queried_object();
  3077. $category = array_map( 'strval', (array) $category );
  3078. if ( in_array( (string) $cat_obj->term_id, $category ) )
  3079. return true;
  3080. elseif ( in_array( $cat_obj->name, $category ) )
  3081. return true;
  3082. elseif ( in_array( $cat_obj->slug, $category ) )
  3083. return true;
  3084. return false;
  3085. }
  3086. /**
  3087. * Is the query for an existing tag archive page?
  3088. *
  3089. * If the $tag parameter is specified, this function will additionally
  3090. * check if the query is for one of the tags specified.
  3091. *
  3092. * @since 3.1.0
  3093. *
  3094. * @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
  3095. * @return bool
  3096. */
  3097. public function is_tag( $tag = '' ) {
  3098. if ( ! $this->is_tag )
  3099. return false;
  3100. if ( empty( $tag ) )
  3101. return true;
  3102. $tag_obj = $this->get_queried_object();
  3103. $tag = array_map( 'strval', (array) $tag );
  3104. if ( in_array( (string) $tag_obj->term_id, $tag ) )
  3105. return true;
  3106. elseif ( in_array( $tag_obj->name, $tag ) )
  3107. return true;
  3108. elseif ( in_array( $tag_obj->slug, $tag ) )
  3109. return true;
  3110. return false;
  3111. }
  3112. /**
  3113. * Is the query for an existing custom taxonomy archive page?
  3114. *
  3115. * If the $taxonomy parameter is specified, this function will additionally
  3116. * check if the query is for that specific $taxonomy.
  3117. *
  3118. * If the $term parameter is specified in addition to the $taxonomy parameter,
  3119. * this function will additionally check if the query is for one of the terms
  3120. * specified.
  3121. *
  3122. * @since 3.1.0
  3123. *
  3124. * @global array $wp_taxonomies
  3125. *
  3126. * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
  3127. * @param mixed $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
  3128. * @return bool True for custom taxonomy archive pages, false for built-in taxonomies (category and tag archives).
  3129. */
  3130. public function is_tax( $taxonomy = '', $term = '' ) {
  3131. global $wp_taxonomies;
  3132. if ( !$this->is_tax )
  3133. return false;
  3134. if ( empty( $taxonomy ) )
  3135. return true;
  3136. $queried_object = $this->get_queried_object();
  3137. $tax_array = array_intersect( array_keys( $wp_taxonomies ), (array) $taxonomy );
  3138. $term_array = (array) $term;
  3139. // Check that the taxonomy matches.
  3140. if ( ! ( isset( $queried_object->taxonomy ) && count( $tax_array ) && in_array( $queried_object->taxonomy, $tax_array ) ) )
  3141. return false;
  3142. // Only a Taxonomy provided.
  3143. if ( empty( $term ) )
  3144. return true;
  3145. return isset( $queried_object->term_id ) &&
  3146. count( array_intersect(
  3147. array( $queried_object->term_id, $queried_object->name, $queried_object->slug ),
  3148. $term_array
  3149. ) );
  3150. }
  3151. /**
  3152. * Whether the current URL is within the comments popup window.
  3153. *
  3154. * @since 3.1.0
  3155. * @deprecated 4.5.0
  3156. *
  3157. * @return bool
  3158. */
  3159. public function is_comments_popup() {
  3160. _deprecated_function( __FUNCTION__, '4.5.0' );
  3161. return false;
  3162. }
  3163. /**
  3164. * Is the query for an existing date archive?
  3165. *
  3166. * @since 3.1.0
  3167. *
  3168. * @return bool
  3169. */
  3170. public function is_date() {
  3171. return (bool) $this->is_date;
  3172. }
  3173. /**
  3174. * Is the query for an existing day archive?
  3175. *
  3176. * @since 3.1.0
  3177. *
  3178. * @return bool
  3179. */
  3180. public function is_day() {
  3181. return (bool) $this->is_day;
  3182. }
  3183. /**
  3184. * Is the query for a feed?
  3185. *
  3186. * @since 3.1.0
  3187. *
  3188. * @param string|array $feeds Optional feed types to check.
  3189. * @return bool
  3190. */
  3191. public function is_feed( $feeds = '' ) {
  3192. if ( empty( $feeds ) || ! $this->is_feed )
  3193. return (bool) $this->is_feed;
  3194. $qv = $this->get( 'feed' );
  3195. if ( 'feed' == $qv )
  3196. $qv = get_default_feed();
  3197. return in_array( $qv, (array) $feeds );
  3198. }
  3199. /**
  3200. * Is the query for a comments feed?
  3201. *
  3202. * @since 3.1.0
  3203. *
  3204. * @return bool
  3205. */
  3206. public function is_comment_feed() {
  3207. return (bool) $this->is_comment_feed;
  3208. }
  3209. /**
  3210. * Is the query for the front page of the site?
  3211. *
  3212. * This is for what is displayed at your site's main URL.
  3213. *
  3214. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_on_front'.
  3215. *
  3216. * If you set a static page for the front page of your site, this function will return
  3217. * true when viewing that page.
  3218. *
  3219. * Otherwise the same as @see WP_Query::is_home()
  3220. *
  3221. * @since 3.1.0
  3222. *
  3223. * @return bool True, if front of site.
  3224. */
  3225. public function is_front_page() {
  3226. // most likely case
  3227. if ( 'posts' == get_option( 'show_on_front') && $this->is_home() )
  3228. return true;
  3229. elseif ( 'page' == get_option( 'show_on_front') && get_option( 'page_on_front' ) && $this->is_page( get_option( 'page_on_front' ) ) )
  3230. return true;
  3231. else
  3232. return false;
  3233. }
  3234. /**
  3235. * Is the query for the blog homepage?
  3236. *
  3237. * This is the page which shows the time based blog content of your site.
  3238. *
  3239. * Depends on the site's "Front page displays" Reading Settings 'show_on_front' and 'page_for_posts'.
  3240. *
  3241. * If you set a static page for the front page of your site, this function will return
  3242. * true only on the page you set as the "Posts page".
  3243. *
  3244. * @see WP_Query::is_front_page()
  3245. *
  3246. * @since 3.1.0
  3247. *
  3248. * @return bool True if blog view homepage.
  3249. */
  3250. public function is_home() {
  3251. return (bool) $this->is_home;
  3252. }
  3253. /**
  3254. * Is the query for an existing month archive?
  3255. *
  3256. * @since 3.1.0
  3257. *
  3258. * @return bool
  3259. */
  3260. public function is_month() {
  3261. return (bool) $this->is_month;
  3262. }
  3263. /**
  3264. * Is the query for an existing single page?
  3265. *
  3266. * If the $page parameter is specified, this function will additionally
  3267. * check if the query is for one of the pages specified.
  3268. *
  3269. * @see WP_Query::is_single()
  3270. * @see WP_Query::is_singular()
  3271. *
  3272. * @since 3.1.0
  3273. *
  3274. * @param int|string|array $page Optional. Page ID, title, slug, path, or array of such. Default empty.
  3275. * @return bool Whether the query is for an existing single page.
  3276. */
  3277. public function is_page( $page = '' ) {
  3278. if ( !$this->is_page )
  3279. return false;
  3280. if ( empty( $page ) )
  3281. return true;
  3282. $page_obj = $this->get_queried_object();
  3283. $page = array_map( 'strval', (array) $page );
  3284. if ( in_array( (string) $page_obj->ID, $page ) ) {
  3285. return true;
  3286. } elseif ( in_array( $page_obj->post_title, $page ) ) {
  3287. return true;
  3288. } elseif ( in_array( $page_obj->post_name, $page ) ) {
  3289. return true;
  3290. } else {
  3291. foreach ( $page as $pagepath ) {
  3292. if ( ! strpos( $pagepath, '/' ) ) {
  3293. continue;
  3294. }
  3295. $pagepath_obj = get_page_by_path( $pagepath );
  3296. if ( $pagepath_obj && ( $pagepath_obj->ID == $page_obj->ID ) ) {
  3297. return true;
  3298. }
  3299. }
  3300. }
  3301. return false;
  3302. }
  3303. /**
  3304. * Is the query for paged result and not for the first page?
  3305. *
  3306. * @since 3.1.0
  3307. *
  3308. * @return bool
  3309. */
  3310. public function is_paged() {
  3311. return (bool) $this->is_paged;
  3312. }
  3313. /**
  3314. * Is the query for a post or page preview?
  3315. *
  3316. * @since 3.1.0
  3317. *
  3318. * @return bool
  3319. */
  3320. public function is_preview() {
  3321. return (bool) $this->is_preview;
  3322. }
  3323. /**
  3324. * Is the query for the robots file?
  3325. *
  3326. * @since 3.1.0
  3327. *
  3328. * @return bool
  3329. */
  3330. public function is_robots() {
  3331. return (bool) $this->is_robots;
  3332. }
  3333. /**
  3334. * Is the query for a search?
  3335. *
  3336. * @since 3.1.0
  3337. *
  3338. * @return bool
  3339. */
  3340. public function is_search() {
  3341. return (bool) $this->is_search;
  3342. }
  3343. /**
  3344. * Is the query for an existing single post?
  3345. *
  3346. * Works for any post type excluding pages.
  3347. *
  3348. * If the $post parameter is specified, this function will additionally
  3349. * check if the query is for one of the Posts specified.
  3350. *
  3351. * @see WP_Query::is_page()
  3352. * @see WP_Query::is_singular()
  3353. *
  3354. * @since 3.1.0
  3355. *
  3356. * @param int|string|array $post Optional. Post ID, title, slug, path, or array of such. Default empty.
  3357. * @return bool Whether the query is for an existing single post.
  3358. */
  3359. public function is_single( $post = '' ) {
  3360. if ( !$this->is_single )
  3361. return false;
  3362. if ( empty($post) )
  3363. return true;
  3364. $post_obj = $this->get_queried_object();
  3365. $post = array_map( 'strval', (array) $post );
  3366. if ( in_array( (string) $post_obj->ID, $post ) ) {
  3367. return true;
  3368. } elseif ( in_array( $post_obj->post_title, $post ) ) {
  3369. return true;
  3370. } elseif ( in_array( $post_obj->post_name, $post ) ) {
  3371. return true;
  3372. } else {
  3373. foreach ( $post as $postpath ) {
  3374. if ( ! strpos( $postpath, '/' ) ) {
  3375. continue;
  3376. }
  3377. $postpath_obj = get_page_by_path( $postpath, OBJECT, $post_obj->post_type );
  3378. if ( $postpath_obj && ( $postpath_obj->ID == $post_obj->ID ) ) {
  3379. return true;
  3380. }
  3381. }
  3382. }
  3383. return false;
  3384. }
  3385. /**
  3386. * Is the query for an existing single post of any post type (post, attachment, page,
  3387. * custom post types)?
  3388. *
  3389. * If the $post_types parameter is specified, this function will additionally
  3390. * check if the query is for one of the Posts Types specified.
  3391. *
  3392. * @see WP_Query::is_page()
  3393. * @see WP_Query::is_single()
  3394. *
  3395. * @since 3.1.0
  3396. *
  3397. * @param string|array $post_types Optional. Post type or array of post types. Default empty.
  3398. * @return bool Whether the query is for an existing single post of any of the given post types.
  3399. */
  3400. public function is_singular( $post_types = '' ) {
  3401. if ( empty( $post_types ) || !$this->is_singular )
  3402. return (bool) $this->is_singular;
  3403. $post_obj = $this->get_queried_object();
  3404. return in_array( $post_obj->post_type, (array) $post_types );
  3405. }
  3406. /**
  3407. * Is the query for a specific time?
  3408. *
  3409. * @since 3.1.0
  3410. *
  3411. * @return bool
  3412. */
  3413. public function is_time() {
  3414. return (bool) $this->is_time;
  3415. }
  3416. /**
  3417. * Is the query for a trackback endpoint call?
  3418. *
  3419. * @since 3.1.0
  3420. *
  3421. * @return bool
  3422. */
  3423. public function is_trackback() {
  3424. return (bool) $this->is_trackback;
  3425. }
  3426. /**
  3427. * Is the query for an existing year archive?
  3428. *
  3429. * @since 3.1.0
  3430. *
  3431. * @return bool
  3432. */
  3433. public function is_year() {
  3434. return (bool) $this->is_year;
  3435. }
  3436. /**
  3437. * Is the query a 404 (returns no results)?
  3438. *
  3439. * @since 3.1.0
  3440. *
  3441. * @return bool
  3442. */
  3443. public function is_404() {
  3444. return (bool) $this->is_404;
  3445. }
  3446. /**
  3447. * Is the query for an embedded post?
  3448. *
  3449. * @since 4.4.0
  3450. *
  3451. * @return bool
  3452. */
  3453. public function is_embed() {
  3454. return (bool) $this->is_embed;
  3455. }
  3456. /**
  3457. * Is the query the main query?
  3458. *
  3459. * @since 3.3.0
  3460. *
  3461. * @global WP_Query $wp_query Global WP_Query instance.
  3462. *
  3463. * @return bool
  3464. */
  3465. public function is_main_query() {
  3466. global $wp_the_query;
  3467. return $wp_the_query === $this;
  3468. }
  3469. /**
  3470. * Set up global post data.
  3471. *
  3472. * @since 4.1.0
  3473. * @since 4.4.0 Added the ability to pass a post ID to `$post`.
  3474. *
  3475. * @global int $id
  3476. * @global WP_User $authordata
  3477. * @global string|int|bool $currentday
  3478. * @global string|int|bool $currentmonth
  3479. * @global int $page
  3480. * @global array $pages
  3481. * @global int $multipage
  3482. * @global int $more
  3483. * @global int $numpages
  3484. *
  3485. * @param WP_Post|object|int $post WP_Post instance or Post ID/object.
  3486. * @return true True when finished.
  3487. */
  3488. public function setup_postdata( $post ) {
  3489. global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
  3490. if ( ! ( $post instanceof WP_Post ) ) {
  3491. $post = get_post( $post );
  3492. }
  3493. if ( ! $post ) {
  3494. return;
  3495. }
  3496. $id = (int) $post->ID;
  3497. $authordata = get_userdata($post->post_author);
  3498. $currentday = mysql2date('d.m.y', $post->post_date, false);
  3499. $currentmonth = mysql2date('m', $post->post_date, false);
  3500. $numpages = 1;
  3501. $multipage = 0;
  3502. $page = $this->get( 'page' );
  3503. if ( ! $page )
  3504. $page = 1;
  3505. /*
  3506. * Force full post content when viewing the permalink for the $post,
  3507. * or when on an RSS feed. Otherwise respect the 'more' tag.
  3508. */
  3509. if ( $post->ID === get_queried_object_id() && ( $this->is_page() || $this->is_single() ) ) {
  3510. $more = 1;
  3511. } elseif ( $this->is_feed() ) {
  3512. $more = 1;
  3513. } else {
  3514. $more = 0;
  3515. }
  3516. $content = $post->post_content;
  3517. if ( false !== strpos( $content, '<!--nextpage-->' ) ) {
  3518. $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content );
  3519. $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content );
  3520. $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content );
  3521. // Ignore nextpage at the beginning of the content.
  3522. if ( 0 === strpos( $content, '<!--nextpage-->' ) )
  3523. $content = substr( $content, 15 );
  3524. $pages = explode('<!--nextpage-->', $content);
  3525. } else {
  3526. $pages = array( $post->post_content );
  3527. }
  3528. /**
  3529. * Filters the "pages" derived from splitting the post content.
  3530. *
  3531. * "Pages" are determined by splitting the post content based on the presence
  3532. * of `<!-- nextpage -->` tags.
  3533. *
  3534. * @since 4.4.0
  3535. *
  3536. * @param array $pages Array of "pages" derived from the post content.
  3537. * of `<!-- nextpage -->` tags..
  3538. * @param WP_Post $post Current post object.
  3539. */
  3540. $pages = apply_filters( 'content_pagination', $pages, $post );
  3541. $numpages = count( $pages );
  3542. if ( $numpages > 1 ) {
  3543. if ( $page > 1 ) {
  3544. $more = 1;
  3545. }
  3546. $multipage = 1;
  3547. } else {
  3548. $multipage = 0;
  3549. }
  3550. /**
  3551. * Fires once the post data has been setup.
  3552. *
  3553. * @since 2.8.0
  3554. * @since 4.1.0 Introduced `$this` parameter.
  3555. *
  3556. * @param WP_Post $post The Post object (passed by reference).
  3557. * @param WP_Query $this The current Query object (passed by reference).
  3558. */
  3559. do_action_ref_array( 'the_post', array( &$post, &$this ) );
  3560. return true;
  3561. }
  3562. /**
  3563. * After looping through a nested query, this function
  3564. * restores the $post global to the current post in this query.
  3565. *
  3566. * @since 3.7.0
  3567. *
  3568. * @global WP_Post $post
  3569. */
  3570. public function reset_postdata() {
  3571. if ( ! empty( $this->post ) ) {
  3572. $GLOBALS['post'] = $this->post;
  3573. $this->setup_postdata( $this->post );
  3574. }
  3575. }
  3576. /**
  3577. * Lazyload term meta for posts in the loop.
  3578. *
  3579. * @since 4.4.0
  3580. * @deprecated 4.5.0 See wp_queue_posts_for_term_meta_lazyload().
  3581. *
  3582. * @param mixed $check
  3583. * @param int $term_id
  3584. * @return mixed
  3585. */
  3586. public function lazyload_term_meta( $check, $term_id ) {
  3587. _deprecated_function( __METHOD__, '4.5.0' );
  3588. return $check;
  3589. }
  3590. /**
  3591. * Lazyload comment meta for comments in the loop.
  3592. *
  3593. * @since 4.4.0
  3594. * @deprecated 4.5.0 See wp_queue_comments_for_comment_meta_lazyload().
  3595. *
  3596. * @param mixed $check
  3597. * @param int $comment_id
  3598. * @return mixed
  3599. */
  3600. public function lazyload_comment_meta( $check, $comment_id ) {
  3601. _deprecated_function( __METHOD__, '4.5.0' );
  3602. return $check;
  3603. }
  3604. }