/
query.php
2701 lines (2356 loc) · 69.4 KB
/
query.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* WordPress Query API
*
* The query API attempts to get which part of WordPress to the user is on. It
* also provides functionality to getting URL query information.
*
* @link http://codex.wordpress.org/The_Loop More information on The Loop.
*
* @package WordPress
* @subpackage Query
*/
/**
* Retrieve variable in the WP_Query class.
*
* @see WP_Query::get()
* @since 1.5.0
* @uses $wp_query
*
* @param string $var The variable key to retrieve.
* @return mixed
*/
function get_query_var($var) {
global $wp_query;
return $wp_query->get($var);
}
/**
* Set query variable.
*
* @see WP_Query::set()
* @since 2.2.0
* @uses $wp_query
*
* @param string $var Query variable key.
* @param mixed $value
* @return null
*/
function set_query_var($var, $value) {
global $wp_query;
return $wp_query->set($var, $value);
}
/**
* Setup The Loop with query parameters.
*
* This will override the current WordPress Loop and shouldn't be used more than
* once. This must not be used within the WordPress Loop.
*
* @since 1.5.0
* @uses $wp_query
*
* @param string $query
* @return array List of posts
*/
function &query_posts($query) {
unset($GLOBALS['wp_query']);
$GLOBALS['wp_query'] =& new WP_Query();
return $GLOBALS['wp_query']->query($query);
}
/**
* Destroy the previous query and setup a new query.
*
* This should be used after {@link query_posts()} and before another {@link
* query_posts()}. This will remove obscure bugs that occur when the previous
* wp_query object is not destroyed properly before another is setup.
*
* @since 2.3.0
* @uses $wp_query
*/
function wp_reset_query() {
unset($GLOBALS['wp_query']);
$GLOBALS['wp_query'] =& $GLOBALS['wp_the_query'];
global $wp_query;
if ( !empty($wp_query->post) ) {
$GLOBALS['post'] = $wp_query->post;
setup_postdata($wp_query->post);
}
}
/*
* Query type checks.
*/
/**
* Whether the current request is in WordPress admin Panel
*
* Does not inform on whether the user is an admin! Use capability checks to
* tell if the user should be accessing a section or not.
*
* @since 1.5.1
*
* @return bool True if inside WordPress administration pages.
*/
function is_admin () {
if ( defined('WP_ADMIN') )
return WP_ADMIN;
return false;
}
/**
* Is query requesting an archive page.
*
* @since 1.5.0
* @uses $wp_query
*
* @return bool True if page is archive.
*/
function is_archive () {
global $wp_query;
return $wp_query->is_archive;
}
/**
* Is query requesting an attachment page.
*
* @since 2.0.0
* @uses $wp_query
*
* @return bool True if page is attachment.
*/
function is_attachment () {
global $wp_query;
return $wp_query->is_attachment;
}
/**
* Is query requesting an author page.
*
* If the $author parameter is specified then the check will be expanded to
* include whether the queried author matches the one given in the parameter.
* You can match against integers and against strings.
*
* If matching against an integer, the ID should be used of the author for the
* test. If the $author is an ID and matches the author page user ID, then
* 'true' will be returned.
*
* If matching against strings, then the test will be matched against both the
* nickname and user nicename and will return true on success.
*
* @since 1.5.0
* @uses $wp_query
*
* @param string|int $author Optional. Is current page this author.
* @return bool True if page is author or $author (if set).
*/
function is_author ($author = '') {
global $wp_query;
if ( !$wp_query->is_author )
return false;
if ( empty($author) )
return true;
$author_obj = $wp_query->get_queried_object();
$author = (array) $author;
if ( in_array( $author_obj->ID, $author ) )
return true;
elseif ( in_array( $author_obj->nickname, $author ) )
return true;
elseif ( in_array( $author_obj->user_nicename, $author ) )
return true;
return false;
}
/**
* Whether current page query contains a category name or given category name.
*
* The category list can contain category IDs, names, or category slugs. If any
* of them are part of the query, then it will return true.
*
* @since 1.5.0
* @uses $wp_query
*
* @param string|array $category Optional.
* @return bool
*/
function is_category ($category = '') {
global $wp_query;
if ( !$wp_query->is_category )
return false;
if ( empty($category) )
return true;
$cat_obj = $wp_query->get_queried_object();
$category = (array) $category;
if ( in_array( $cat_obj->term_id, $category ) )
return true;
elseif ( in_array( $cat_obj->name, $category ) )
return true;
elseif ( in_array( $cat_obj->slug, $category ) )
return true;
return false;
}
/**
* Whether the current page query has the given tag slug or contains tag.
*
* @since 2.3.0
* @uses $wp_query
*
* @param string|array $slug Optional. Single tag or list of tags to check for.
* @return bool
*/
function is_tag( $slug = '' ) {
global $wp_query;
if ( !$wp_query->is_tag )
return false;
if ( empty( $slug ) )
return true;
$tag_obj = $wp_query->get_queried_object();
$slug = (array) $slug;
if ( in_array( $tag_obj->slug, $slug ) )
return true;
return false;
}
/**
* Whether the current page query has the given taxonomy slug or contains taxonomy.
*
* @since 2.5.0
* @uses $wp_query
*
* @param string|array $slug Optional. Slug or slugs to check in current query.
* @return bool
*/
function is_tax( $slug = '' ) {
global $wp_query;
if ( !$wp_query->is_tax )
return false;
if ( empty($slug) )
return true;
$term = $wp_query->get_queried_object();
$slug = (array) $slug;
if ( in_array( $term->slug, $slug ) )
return true;
return false;
}
/**
* Whether the current URL is within the comments popup window.
*
* @since 1.5.0
* @uses $wp_query
*
* @return bool
*/
function is_comments_popup () {
global $wp_query;
return $wp_query->is_comments_popup;
}
/**
* Whether current URL is based on a date.
*
* @since 1.5.0
* @uses $wp_query
*
* @return bool
*/
function is_date () {
global $wp_query;
return $wp_query->is_date;
}
/**
* Whether current blog URL contains a day.
*
* @since 1.5.0
* @uses $wp_query
*
* @return bool
*/
function is_day () {
global $wp_query;
return $wp_query->is_day;
}
/**
* Whether current page query is feed URL.
*
* @since 1.5.0
* @uses $wp_query
*
* @return bool
*/
function is_feed () {
global $wp_query;
return $wp_query->is_feed;
}
/**
* Whether current page query is the front of the site.
*
* @since 2.5.0
* @uses is_home()
* @uses get_option()
*
* @return bool True, if front of site.
*/
function is_front_page () {
// most likely case
if ( 'posts' == get_option('show_on_front') && is_home() )
return true;
elseif ( 'page' == get_option('show_on_front') && get_option('page_on_front') && is_page(get_option('page_on_front')) )
return true;
else
return false;
}
/**
* Whether current page view is the blog homepage.
*
* @since 1.5.0
* @uses $wp_query
*
* @return bool True if blog view homepage.
*/
function is_home () {
global $wp_query;
return $wp_query->is_home;
}
/**
* Whether current page query contains a month.
*
* @since 1.5.0
* @uses $wp_query
*
* @return bool
*/
function is_month () {
global $wp_query;
return $wp_query->is_month;
}
/**
* Whether query is page or contains given page(s).
*
* Calls the function without any parameters will only test whether the current
* query is of the page type. Either a list or a single item can be tested
* against for whether the query is a page and also is the value or one of the
* values in the page parameter.
*
* The parameter can contain the page ID, page title, or page name. The
* parameter can also be an array of those three values.
*
* @since 1.5.0
* @uses $wp_query
*
* @param mixed $page Either page or list of pages to test against.
* @return bool
*/
function is_page ($page = '') {
global $wp_query;
if ( !$wp_query->is_page )
return false;
if ( empty($page) )
return true;
$page_obj = $wp_query->get_queried_object();
$page = (array) $page;
if ( in_array( $page_obj->ID, $page ) )
return true;
elseif ( in_array( $page_obj->post_title, $page ) )
return true;
else if ( in_array( $page_obj->post_name, $page ) )
return true;
return false;
}
/**
* Whether query contains multiple pages for the results.
*
* @since 1.5.0
* @uses $wp_query
*
* @return bool
*/
function is_paged () {
global $wp_query;
return $wp_query->is_paged;
}
/**
* Whether the current page was created by a plugin.
*
* The plugin can set this by using the global $plugin_page and setting it to
* true.
*
* @since 1.5.0
* @global bool $plugin_page Used by plugins to tell the query that current is a plugin page.
*
* @return bool
*/
function is_plugin_page() {
global $plugin_page;
if ( isset($plugin_page) )
return true;
return false;
}
/**
* Whether the current query is preview of post or page.
*
* @since 2.0.0
* @uses $wp_query
*
* @return bool
*/
function is_preview() {
global $wp_query;
return $wp_query->is_preview;
}
/**
* Whether the current query post is robots.
*
* @since 2.1.0
* @uses $wp_query
*
* @return bool
*/
function is_robots() {
global $wp_query;
return $wp_query->is_robots;
}
/**
* Whether current query is the result of a user search.
*
* @since 1.5.0
* @uses $wp_query
*
* @return bool
*/
function is_search () {
global $wp_query;
return $wp_query->is_search;
}
/**
* Whether the current page query is single page.
*
* The parameter can contain the post ID, post title, or post name. The
* parameter can also be an array of those three values.
*
* This applies to other post types, attachments, pages, posts. Just means that
* the current query has only a single object.
*
* @since 1.5.0
* @uses $wp_query
*
* @param mixed $post Either post or list of posts to test against.
* @return bool
*/
function is_single ($post = '') {
global $wp_query;
if ( !$wp_query->is_single )
return false;
if ( empty( $post) )
return true;
$post_obj = $wp_query->get_queried_object();
$post = (array) $post;
if ( in_array( $post_obj->ID, $post ) )
return true;
elseif ( in_array( $post_obj->post_title, $post ) )
return true;
elseif ( in_array( $post_obj->post_name, $post ) )
return true;
return false;
}
/**
* Whether is single post, is a page, or is an attachment.
*
* @since 1.5.0
* @uses $wp_query
*
* @return bool
*/
function is_singular() {
global $wp_query;
return $wp_query->is_singular;
}
/**
* Whether the query contains a time.
*
* @since 1.5.0
* @uses $wp_query
*
* @return bool
*/
function is_time () {
global $wp_query;
return $wp_query->is_time;
}
/**
* Whether the query is a trackback.
*
* @since 1.5.0
* @uses $wp_query
*
* @return bool
*/
function is_trackback () {
global $wp_query;
return $wp_query->is_trackback;
}
/**
* Whether the query contains a year.
*
* @since 1.5.0
* @uses $wp_query
*
* @return bool
*/
function is_year () {
global $wp_query;
return $wp_query->is_year;
}
/**
* Whether current page query is a 404 and no results for WordPress query.
*
* @since 1.5.0
* @uses $wp_query
*
* @return bool True, if nothing is found matching WordPress Query.
*/
function is_404 () {
global $wp_query;
return $wp_query->is_404;
}
/*
* The Loop. Post loop control.
*/
/**
* Whether current WordPress query has results to loop over.
*
* @see WP_Query::have_posts()
* @since 1.5.0
* @uses $wp_query
*
* @return bool
*/
function have_posts() {
global $wp_query;
return $wp_query->have_posts();
}
/**
* Whether the caller is in the Loop.
*
* @since 2.0.0
* @uses $wp_query
*
* @return bool True if caller is within loop, false if loop hasn't started or ended.
*/
function in_the_loop() {
global $wp_query;
return $wp_query->in_the_loop;
}
/**
* Rewind the loop posts.
*
* @see WP_Query::rewind_posts()
* @since 1.5.0
* @uses $wp_query
*
* @return null
*/
function rewind_posts() {
global $wp_query;
return $wp_query->rewind_posts();
}
/**
* Iterate the post index in the loop.
*
* @see WP_Query::the_post()
* @since 1.5.0
* @uses $wp_query
*/
function the_post() {
global $wp_query;
$wp_query->the_post();
}
/*
* Comments loop.
*/
/**
* Whether there are comments to loop over.
*
* @see WP_Query::have_comments()
* @since 2.2.0
* @uses $wp_query
*
* @return bool
*/
function have_comments() {
global $wp_query;
return $wp_query->have_comments();
}
/**
* Iterate comment index in the comment loop.
*
* @see WP_Query::the_comment()
* @since 2.2.0
* @uses $wp_query
*
* @return object
*/
function the_comment() {
global $wp_query;
return $wp_query->the_comment();
}
/*
* WP_Query
*/
/**
* The WordPress Query class.
*
* @link http://codex.wordpress.org/Function_Reference/WP_Query Codex page.
*
* @since 1.5.0
*/
class WP_Query {
/**
* Query string
*
* @since 1.5.0
* @access public
* @var string
*/
var $query;
/**
* Query search variables set by the user.
*
* @since 1.5.0
* @access public
* @var array
*/
var $query_vars = array();
/**
* Holds the data for a single object that is queried.
*
* Holds the contents of a post, page, category, attachment.
*
* @since 1.5.0
* @access public
* @var object|array
*/
var $queried_object;
/**
* The ID of the queried object.
*
* @since 1.5.0
* @access public
* @var int
*/
var $queried_object_id;
/**
* Get post database query.
*
* @since 2.0.1
* @access public
* @var string
*/
var $request;
/**
* List of posts.
*
* @since 1.5.0
* @access public
* @var array
*/
var $posts;
/**
* The amount of posts for the current query.
*
* @since 1.5.0
* @access public
* @var int
*/
var $post_count = 0;
/**
* Index of the current item in the loop.
*
* @since 1.5.0
* @access public
* @var int
*/
var $current_post = -1;
/**
* Whether the loop has started and the caller is in the loop.
*
* @since 2.0.0
* @access public
* @var bool
*/
var $in_the_loop = false;
/**
* The current post ID.
*
* @since 1.5.0
* @access public
* @var int
*/
var $post;
/**
* The list of comments for current post.
*
* @since 2.2.0
* @access public
* @var array
*/
var $comments;
/**
* The amount of comments for the posts.
*
* @since 2.2.0
* @access public
* @var int
*/
var $comment_count = 0;
/**
* The index of the comment in the comment loop.
*
* @since 2.2.0
* @access public
* @var int
*/
var $current_comment = -1;
/**
* Current comment ID.
*
* @since 2.2.0
* @access public
* @var int
*/
var $comment;
/**
* Amount of posts if limit clause was not used.
*
* @since 2.1.0
* @access public
* @var int
*/
var $found_posts = 0;
/**
* The amount of pages.
*
* @since 2.1.0
* @access public
* @var int
*/
var $max_num_pages = 0;
/**
* The amount of comment pages.
*
* @since 2.7.0
* @access public
* @var int
*/
var $max_num_comment_pages = 0;
/**
* Set if query is single post.
*
* @since 1.5.0
* @access public
* @var bool
*/
var $is_single = false;
/**
* Set if query is preview of blog.
*
* @since 2.0.0
* @access public
* @var bool
*/
var $is_preview = false;
/**
* Set if query returns a page.
*
* @since 1.5.0
* @access public
* @var bool
*/
var $is_page = false;
/**
* Set if query is an archive list.
*
* @since 1.5.0
* @access public
* @var bool
*/
var $is_archive = false;
/**
* Set if query is part of a date.
*
* @since 1.5.0
* @access public
* @var bool
*/
var $is_date = false;
/**
* Set if query contains a year.
*
* @since 1.5.0
* @access public
* @var bool
*/
var $is_year = false;
/**
* Set if query contains a month.
*
* @since 1.5.0
* @access public
* @var bool
*/
var $is_month = false;
/**
* Set if query contains a day.
*
* @since 1.5.0
* @access public
* @var bool
*/
var $is_day = false;
/**
* Set if query contains time.
*
* @since 1.5.0
* @access public
* @var bool
*/
var $is_time = false;
/**
* Set if query contains an author.
*
* @since 1.5.0
* @access public
* @var bool
*/
var $is_author = false;
/**
* Set if query contains category.
*
* @since 1.5.0
* @access public
* @var bool
*/
var $is_category = false;
/**
* Set if query contains tag.
*
* @since 2.3.0
* @access public
* @var bool
*/
var $is_tag = false;
/**
* Set if query contains taxonomy.
*
* @since 2.5.0
* @access public
* @var bool
*/
var $is_tax = false;
/**
* Set if query was part of a search result.
*
* @since 1.5.0
* @access public
* @var bool
*/
var $is_search = false;
/**
* Set if query is feed display.
*
* @since 1.5.0
* @access public
* @var bool
*/
var $is_feed = false;
/**
* Set if query is comment feed display.
*
* @since 2.2.0
* @access public
* @var bool
*/
var $is_comment_feed = false;
/**
* Set if query is trackback.