Nessuna descrizione

class-wp-query.php 132KB

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