class-wp-query.php 145 KB

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