class-wp-query.php 127 KB

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