class-wp-query.php 121 KB

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