Нема описа

class-wp-query.php 137KB

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