443 * The query flags are what page info WordPress was able to figure out. |
451 * The query flags are what page info WordPress was able to figure out. |
444 * |
452 * |
445 * @since 2.0.0 |
453 * @since 2.0.0 |
446 */ |
454 */ |
447 private function init_query_flags() { |
455 private function init_query_flags() { |
448 $this->is_single = false; |
456 $this->is_single = false; |
449 $this->is_preview = false; |
457 $this->is_preview = false; |
450 $this->is_page = false; |
458 $this->is_page = false; |
451 $this->is_archive = false; |
459 $this->is_archive = false; |
452 $this->is_date = false; |
460 $this->is_date = false; |
453 $this->is_year = false; |
461 $this->is_year = false; |
454 $this->is_month = false; |
462 $this->is_month = false; |
455 $this->is_day = false; |
463 $this->is_day = false; |
456 $this->is_time = false; |
464 $this->is_time = false; |
457 $this->is_author = false; |
465 $this->is_author = false; |
458 $this->is_category = false; |
466 $this->is_category = false; |
459 $this->is_tag = false; |
467 $this->is_tag = false; |
460 $this->is_tax = false; |
468 $this->is_tax = false; |
461 $this->is_search = false; |
469 $this->is_search = false; |
462 $this->is_feed = false; |
470 $this->is_feed = false; |
463 $this->is_comment_feed = false; |
471 $this->is_comment_feed = false; |
464 $this->is_trackback = false; |
472 $this->is_trackback = false; |
465 $this->is_home = false; |
473 $this->is_home = false; |
466 $this->is_404 = false; |
474 $this->is_privacy_policy = false; |
467 $this->is_paged = false; |
475 $this->is_404 = false; |
468 $this->is_admin = false; |
476 $this->is_paged = false; |
469 $this->is_attachment = false; |
477 $this->is_admin = false; |
470 $this->is_singular = false; |
478 $this->is_attachment = false; |
471 $this->is_robots = false; |
479 $this->is_singular = false; |
472 $this->is_posts_page = false; |
480 $this->is_robots = false; |
|
481 $this->is_posts_page = false; |
473 $this->is_post_type_archive = false; |
482 $this->is_post_type_archive = false; |
474 } |
483 } |
475 |
484 |
476 /** |
485 /** |
477 * Initiates object properties and sets default values. |
486 * Initiates object properties and sets default values. |
478 * |
487 * |
479 * @since 1.5.0 |
488 * @since 1.5.0 |
480 */ |
489 */ |
481 public function init() { |
490 public function init() { |
482 unset($this->posts); |
491 unset( $this->posts ); |
483 unset($this->query); |
492 unset( $this->query ); |
484 $this->query_vars = array(); |
493 $this->query_vars = array(); |
485 unset($this->queried_object); |
494 unset( $this->queried_object ); |
486 unset($this->queried_object_id); |
495 unset( $this->queried_object_id ); |
487 $this->post_count = 0; |
496 $this->post_count = 0; |
488 $this->current_post = -1; |
497 $this->current_post = -1; |
489 $this->in_the_loop = false; |
498 $this->in_the_loop = false; |
490 unset( $this->request ); |
499 unset( $this->request ); |
491 unset( $this->post ); |
500 unset( $this->post ); |
492 unset( $this->comments ); |
501 unset( $this->comments ); |
493 unset( $this->comment ); |
502 unset( $this->comment ); |
494 $this->comment_count = 0; |
503 $this->comment_count = 0; |
495 $this->current_comment = -1; |
504 $this->current_comment = -1; |
496 $this->found_posts = 0; |
505 $this->found_posts = 0; |
497 $this->max_num_pages = 0; |
506 $this->max_num_pages = 0; |
498 $this->max_num_comment_pages = 0; |
507 $this->max_num_comment_pages = 0; |
499 |
508 |
500 $this->init_query_flags(); |
509 $this->init_query_flags(); |
501 } |
510 } |
502 |
511 |
699 * Defaults to the value of `$update_post_term_cache`. |
726 * Defaults to the value of `$update_post_term_cache`. |
700 * @type int $w The week number of the year. Default empty. Accepts numbers 0-53. |
727 * @type int $w The week number of the year. Default empty. Accepts numbers 0-53. |
701 * @type int $year The four-digit year. Default empty. Accepts any four-digit year. |
728 * @type int $year The four-digit year. Default empty. Accepts any four-digit year. |
702 * } |
729 * } |
703 */ |
730 */ |
704 public function parse_query( $query = '' ) { |
731 public function parse_query( $query = '' ) { |
705 if ( ! empty( $query ) ) { |
732 if ( ! empty( $query ) ) { |
706 $this->init(); |
733 $this->init(); |
707 $this->query = $this->query_vars = wp_parse_args( $query ); |
734 $this->query = $this->query_vars = wp_parse_args( $query ); |
708 } elseif ( ! isset( $this->query ) ) { |
735 } elseif ( ! isset( $this->query ) ) { |
709 $this->query = $this->query_vars; |
736 $this->query = $this->query_vars; |
710 } |
737 } |
711 |
738 |
712 $this->query_vars = $this->fill_query_vars($this->query_vars); |
739 $this->query_vars = $this->fill_query_vars( $this->query_vars ); |
713 $qv = &$this->query_vars; |
740 $qv = &$this->query_vars; |
714 $this->query_vars_changed = true; |
741 $this->query_vars_changed = true; |
715 |
742 |
716 if ( ! empty($qv['robots']) ) |
743 if ( ! empty( $qv['robots'] ) ) { |
717 $this->is_robots = true; |
744 $this->is_robots = true; |
|
745 } |
718 |
746 |
719 if ( ! is_scalar( $qv['p'] ) || $qv['p'] < 0 ) { |
747 if ( ! is_scalar( $qv['p'] ) || $qv['p'] < 0 ) { |
720 $qv['p'] = 0; |
748 $qv['p'] = 0; |
721 $qv['error'] = '404'; |
749 $qv['error'] = '404'; |
722 } else { |
750 } else { |
723 $qv['p'] = intval( $qv['p'] ); |
751 $qv['p'] = intval( $qv['p'] ); |
724 } |
752 } |
725 |
753 |
726 $qv['page_id'] = absint($qv['page_id']); |
754 $qv['page_id'] = absint( $qv['page_id'] ); |
727 $qv['year'] = absint($qv['year']); |
755 $qv['year'] = absint( $qv['year'] ); |
728 $qv['monthnum'] = absint($qv['monthnum']); |
756 $qv['monthnum'] = absint( $qv['monthnum'] ); |
729 $qv['day'] = absint($qv['day']); |
757 $qv['day'] = absint( $qv['day'] ); |
730 $qv['w'] = absint($qv['w']); |
758 $qv['w'] = absint( $qv['w'] ); |
731 $qv['m'] = is_scalar( $qv['m'] ) ? preg_replace( '|[^0-9]|', '', $qv['m'] ) : ''; |
759 $qv['m'] = is_scalar( $qv['m'] ) ? preg_replace( '|[^0-9]|', '', $qv['m'] ) : ''; |
732 $qv['paged'] = absint($qv['paged']); |
760 $qv['paged'] = absint( $qv['paged'] ); |
733 $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers |
761 $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers |
734 $qv['author'] = preg_replace( '|[^0-9,-]|', '', $qv['author'] ); // comma separated list of positive or negative integers |
762 $qv['author'] = preg_replace( '|[^0-9,-]|', '', $qv['author'] ); // comma separated list of positive or negative integers |
735 $qv['pagename'] = trim( $qv['pagename'] ); |
763 $qv['pagename'] = trim( $qv['pagename'] ); |
736 $qv['name'] = trim( $qv['name'] ); |
764 $qv['name'] = trim( $qv['name'] ); |
737 $qv['title'] = trim( $qv['title'] ); |
765 $qv['title'] = trim( $qv['title'] ); |
738 if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']); |
766 if ( '' !== $qv['hour'] ) { |
739 if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']); |
767 $qv['hour'] = absint( $qv['hour'] ); |
740 if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']); |
768 } |
741 if ( '' !== $qv['menu_order'] ) $qv['menu_order'] = absint($qv['menu_order']); |
769 if ( '' !== $qv['minute'] ) { |
|
770 $qv['minute'] = absint( $qv['minute'] ); |
|
771 } |
|
772 if ( '' !== $qv['second'] ) { |
|
773 $qv['second'] = absint( $qv['second'] ); |
|
774 } |
|
775 if ( '' !== $qv['menu_order'] ) { |
|
776 $qv['menu_order'] = absint( $qv['menu_order'] ); |
|
777 } |
742 |
778 |
743 // Fairly insane upper bound for search string lengths. |
779 // Fairly insane upper bound for search string lengths. |
744 if ( ! is_scalar( $qv['s'] ) || ( ! empty( $qv['s'] ) && strlen( $qv['s'] ) > 1600 ) ) { |
780 if ( ! is_scalar( $qv['s'] ) || ( ! empty( $qv['s'] ) && strlen( $qv['s'] ) > 1600 ) ) { |
745 $qv['s'] = ''; |
781 $qv['s'] = ''; |
746 } |
782 } |
747 |
783 |
748 // Compat. Map subpost to attachment. |
784 // Compat. Map subpost to attachment. |
749 if ( '' != $qv['subpost'] ) |
785 if ( '' != $qv['subpost'] ) { |
750 $qv['attachment'] = $qv['subpost']; |
786 $qv['attachment'] = $qv['subpost']; |
751 if ( '' != $qv['subpost_id'] ) |
787 } |
|
788 if ( '' != $qv['subpost_id'] ) { |
752 $qv['attachment_id'] = $qv['subpost_id']; |
789 $qv['attachment_id'] = $qv['subpost_id']; |
753 |
790 } |
754 $qv['attachment_id'] = absint($qv['attachment_id']); |
791 |
755 |
792 $qv['attachment_id'] = absint( $qv['attachment_id'] ); |
756 if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) { |
793 |
757 $this->is_single = true; |
794 if ( ( '' != $qv['attachment'] ) || ! empty( $qv['attachment_id'] ) ) { |
|
795 $this->is_single = true; |
758 $this->is_attachment = true; |
796 $this->is_attachment = true; |
759 } elseif ( '' != $qv['name'] ) { |
797 } elseif ( '' != $qv['name'] ) { |
760 $this->is_single = true; |
798 $this->is_single = true; |
761 } elseif ( $qv['p'] ) { |
799 } elseif ( $qv['p'] ) { |
762 $this->is_single = true; |
800 $this->is_single = true; |
763 } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) { |
801 } elseif ( ( '' !== $qv['hour'] ) && ( '' !== $qv['minute'] ) && ( '' !== $qv['second'] ) && ( '' != $qv['year'] ) && ( '' != $qv['monthnum'] ) && ( '' != $qv['day'] ) ) { |
764 // If year, month, day, hour, minute, and second are set, a single |
802 // If year, month, day, hour, minute, and second are set, a single |
765 // post is being queried. |
803 // post is being queried. |
766 $this->is_single = true; |
804 $this->is_single = true; |
767 } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) { |
805 } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || ! empty( $qv['page_id'] ) ) { |
768 $this->is_page = true; |
806 $this->is_page = true; |
769 $this->is_single = false; |
807 $this->is_single = false; |
770 } else { |
808 } else { |
771 // Look for archive queries. Dates, categories, authors, search, post type archives. |
809 // Look for archive queries. Dates, categories, authors, search, post type archives. |
772 |
810 |
773 if ( isset( $this->query['s'] ) ) { |
811 if ( isset( $this->query['s'] ) ) { |
857 } |
895 } |
858 } |
896 } |
859 } |
897 } |
860 unset( $tax_query ); |
898 unset( $tax_query ); |
861 |
899 |
862 if ( empty($qv['author']) || ($qv['author'] == '0') ) { |
900 if ( empty( $qv['author'] ) || ( $qv['author'] == '0' ) ) { |
863 $this->is_author = false; |
901 $this->is_author = false; |
864 } else { |
902 } else { |
865 $this->is_author = true; |
903 $this->is_author = true; |
866 } |
904 } |
867 |
905 |
868 if ( '' != $qv['author_name'] ) |
906 if ( '' != $qv['author_name'] ) { |
869 $this->is_author = true; |
907 $this->is_author = true; |
870 |
908 } |
871 if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) { |
909 |
|
910 if ( ! empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) { |
872 $post_type_obj = get_post_type_object( $qv['post_type'] ); |
911 $post_type_obj = get_post_type_object( $qv['post_type'] ); |
873 if ( ! empty( $post_type_obj->has_archive ) ) |
912 if ( ! empty( $post_type_obj->has_archive ) ) { |
874 $this->is_post_type_archive = true; |
913 $this->is_post_type_archive = true; |
875 } |
914 } |
876 |
915 } |
877 if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax ) |
916 |
|
917 if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax ) { |
878 $this->is_archive = true; |
918 $this->is_archive = true; |
879 } |
919 } |
880 |
920 } |
881 if ( '' != $qv['feed'] ) |
921 |
|
922 if ( '' != $qv['feed'] ) { |
882 $this->is_feed = true; |
923 $this->is_feed = true; |
|
924 } |
883 |
925 |
884 if ( '' != $qv['embed'] ) { |
926 if ( '' != $qv['embed'] ) { |
885 $this->is_embed = true; |
927 $this->is_embed = true; |
886 } |
928 } |
887 |
929 |
888 if ( '' != $qv['tb'] ) |
930 if ( '' != $qv['tb'] ) { |
889 $this->is_trackback = true; |
931 $this->is_trackback = true; |
890 |
932 } |
891 if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) ) |
933 |
|
934 if ( '' != $qv['paged'] && ( intval( $qv['paged'] ) > 1 ) ) { |
892 $this->is_paged = true; |
935 $this->is_paged = true; |
|
936 } |
893 |
937 |
894 // if we're previewing inside the write screen |
938 // if we're previewing inside the write screen |
895 if ( '' != $qv['preview'] ) |
939 if ( '' != $qv['preview'] ) { |
896 $this->is_preview = true; |
940 $this->is_preview = true; |
897 |
941 } |
898 if ( is_admin() ) |
942 |
|
943 if ( is_admin() ) { |
899 $this->is_admin = true; |
944 $this->is_admin = true; |
900 |
945 } |
901 if ( false !== strpos($qv['feed'], 'comments-') ) { |
946 |
902 $qv['feed'] = str_replace('comments-', '', $qv['feed']); |
947 if ( false !== strpos( $qv['feed'], 'comments-' ) ) { |
|
948 $qv['feed'] = str_replace( 'comments-', '', $qv['feed'] ); |
903 $qv['withcomments'] = 1; |
949 $qv['withcomments'] = 1; |
904 } |
950 } |
905 |
951 |
906 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment; |
952 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment; |
907 |
953 |
908 if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) ) |
954 if ( $this->is_feed && ( ! empty( $qv['withcomments'] ) || ( empty( $qv['withoutcomments'] ) && $this->is_singular ) ) ) { |
909 $this->is_comment_feed = true; |
955 $this->is_comment_feed = true; |
910 |
956 } |
911 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 ) ) |
957 |
|
958 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 ) ) { |
912 $this->is_home = true; |
959 $this->is_home = true; |
|
960 } |
913 |
961 |
914 // Correct is_* for page_on_front and page_for_posts |
962 // Correct is_* for page_on_front and page_for_posts |
915 if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) { |
963 if ( $this->is_home && 'page' == get_option( 'show_on_front' ) && get_option( 'page_on_front' ) ) { |
916 $_query = wp_parse_args($this->query); |
964 $_query = wp_parse_args( $this->query ); |
917 // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename. |
965 // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename. |
918 if ( isset($_query['pagename']) && '' == $_query['pagename'] ) |
966 if ( isset( $_query['pagename'] ) && '' == $_query['pagename'] ) { |
919 unset($_query['pagename']); |
967 unset( $_query['pagename'] ); |
|
968 } |
920 |
969 |
921 unset( $_query['embed'] ); |
970 unset( $_query['embed'] ); |
922 |
971 |
923 if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) { |
972 if ( empty( $_query ) || ! array_diff( array_keys( $_query ), array( 'preview', 'page', 'paged', 'cpage' ) ) ) { |
924 $this->is_page = true; |
973 $this->is_page = true; |
925 $this->is_home = false; |
974 $this->is_home = false; |
926 $qv['page_id'] = get_option('page_on_front'); |
975 $qv['page_id'] = get_option( 'page_on_front' ); |
927 // Correct <!--nextpage--> for page_on_front |
976 // Correct <!--nextpage--> for page_on_front |
928 if ( !empty($qv['paged']) ) { |
977 if ( ! empty( $qv['paged'] ) ) { |
929 $qv['page'] = $qv['paged']; |
978 $qv['page'] = $qv['paged']; |
930 unset($qv['paged']); |
979 unset( $qv['paged'] ); |
931 } |
980 } |
932 } |
981 } |
933 } |
982 } |
934 |
983 |
935 if ( '' != $qv['pagename'] ) { |
984 if ( '' != $qv['pagename'] ) { |
936 $this->queried_object = get_page_by_path( $qv['pagename'] ); |
985 $this->queried_object = get_page_by_path( $qv['pagename'] ); |
937 |
986 |
938 if ( $this->queried_object && 'attachment' == $this->queried_object->post_type ) { |
987 if ( $this->queried_object && 'attachment' == $this->queried_object->post_type ) { |
939 if ( preg_match( "/^[^%]*%(?:postname)%/", get_option( 'permalink_structure' ) ) ) { |
988 if ( preg_match( '/^[^%]*%(?:postname)%/', get_option( 'permalink_structure' ) ) ) { |
940 // See if we also have a post with the same slug |
989 // See if we also have a post with the same slug |
941 $post = get_page_by_path( $qv['pagename'], OBJECT, 'post' ); |
990 $post = get_page_by_path( $qv['pagename'], OBJECT, 'post' ); |
942 if ( $post ) { |
991 if ( $post ) { |
943 $this->queried_object = $post; |
992 $this->queried_object = $post; |
944 $this->is_page = false; |
993 $this->is_page = false; |
945 $this->is_single = true; |
994 $this->is_single = true; |
946 } |
995 } |
947 } |
996 } |
948 } |
997 } |
949 |
998 |
950 if ( ! empty( $this->queried_object ) ) { |
999 if ( ! empty( $this->queried_object ) ) { |
951 $this->queried_object_id = (int) $this->queried_object->ID; |
1000 $this->queried_object_id = (int) $this->queried_object->ID; |
952 } else { |
1001 } else { |
953 unset( $this->queried_object ); |
1002 unset( $this->queried_object ); |
954 } |
1003 } |
955 |
1004 |
956 if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) { |
1005 if ( 'page' == get_option( 'show_on_front' ) && isset( $this->queried_object_id ) && $this->queried_object_id == get_option( 'page_for_posts' ) ) { |
957 $this->is_page = false; |
1006 $this->is_page = false; |
958 $this->is_home = true; |
1007 $this->is_home = true; |
959 $this->is_posts_page = true; |
1008 $this->is_posts_page = true; |
960 } |
1009 } |
|
1010 |
|
1011 if ( isset( $this->queried_object_id ) && $this->queried_object_id == get_option( 'wp_page_for_privacy_policy' ) ) { |
|
1012 $this->is_privacy_policy = true; |
|
1013 } |
961 } |
1014 } |
962 |
1015 |
963 if ( $qv['page_id'] ) { |
1016 if ( $qv['page_id'] ) { |
964 if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) { |
1017 if ( 'page' == get_option( 'show_on_front' ) && $qv['page_id'] == get_option( 'page_for_posts' ) ) { |
965 $this->is_page = false; |
1018 $this->is_page = false; |
966 $this->is_home = true; |
1019 $this->is_home = true; |
967 $this->is_posts_page = true; |
1020 $this->is_posts_page = true; |
968 } |
1021 } |
969 } |
1022 |
970 |
1023 if ( $qv['page_id'] == get_option( 'wp_page_for_privacy_policy' ) ) { |
971 if ( !empty($qv['post_type']) ) { |
1024 $this->is_privacy_policy = true; |
972 if ( is_array($qv['post_type']) ) |
1025 } |
973 $qv['post_type'] = array_map('sanitize_key', $qv['post_type']); |
1026 } |
974 else |
1027 |
975 $qv['post_type'] = sanitize_key($qv['post_type']); |
1028 if ( ! empty( $qv['post_type'] ) ) { |
|
1029 if ( is_array( $qv['post_type'] ) ) { |
|
1030 $qv['post_type'] = array_map( 'sanitize_key', $qv['post_type'] ); |
|
1031 } else { |
|
1032 $qv['post_type'] = sanitize_key( $qv['post_type'] ); |
|
1033 } |
976 } |
1034 } |
977 |
1035 |
978 if ( ! empty( $qv['post_status'] ) ) { |
1036 if ( ! empty( $qv['post_status'] ) ) { |
979 if ( is_array( $qv['post_status'] ) ) |
1037 if ( is_array( $qv['post_status'] ) ) { |
980 $qv['post_status'] = array_map('sanitize_key', $qv['post_status']); |
1038 $qv['post_status'] = array_map( 'sanitize_key', $qv['post_status'] ); |
981 else |
1039 } else { |
982 $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']); |
1040 $qv['post_status'] = preg_replace( '|[^a-z0-9_,-]|', '', $qv['post_status'] ); |
983 } |
1041 } |
984 |
1042 } |
985 if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) ) |
1043 |
|
1044 if ( $this->is_posts_page && ( ! isset( $qv['withcomments'] ) || ! $qv['withcomments'] ) ) { |
986 $this->is_comment_feed = false; |
1045 $this->is_comment_feed = false; |
|
1046 } |
987 |
1047 |
988 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment; |
1048 $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment; |
989 // Done correcting is_* for page_on_front and page_for_posts |
1049 // Done correcting is_* for page_on_front and page_for_posts |
990 |
1050 |
991 if ( '404' == $qv['error'] ) |
1051 if ( '404' == $qv['error'] ) { |
992 $this->set_404(); |
1052 $this->set_404(); |
|
1053 } |
993 |
1054 |
994 $this->is_embed = $this->is_embed && ( $this->is_singular || $this->is_404 ); |
1055 $this->is_embed = $this->is_embed && ( $this->is_singular || $this->is_404 ); |
995 |
1056 |
996 $this->query_vars_hash = md5( serialize( $this->query_vars ) ); |
1057 $this->query_vars_hash = md5( serialize( $this->query_vars ) ); |
997 $this->query_vars_changed = false; |
1058 $this->query_vars_changed = false; |
998 |
1059 |
999 /** |
1060 /** |
1000 * Fires after the main query vars have been parsed. |
1061 * Fires after the main query vars have been parsed. |
1001 * |
1062 * |
1020 $tax_query = $q['tax_query']; |
1081 $tax_query = $q['tax_query']; |
1021 } else { |
1082 } else { |
1022 $tax_query = array(); |
1083 $tax_query = array(); |
1023 } |
1084 } |
1024 |
1085 |
1025 if ( !empty($q['taxonomy']) && !empty($q['term']) ) { |
1086 if ( ! empty( $q['taxonomy'] ) && ! empty( $q['term'] ) ) { |
1026 $tax_query[] = array( |
1087 $tax_query[] = array( |
1027 'taxonomy' => $q['taxonomy'], |
1088 'taxonomy' => $q['taxonomy'], |
1028 'terms' => array( $q['term'] ), |
1089 'terms' => array( $q['term'] ), |
1029 'field' => 'slug', |
1090 'field' => 'slug', |
1030 ); |
1091 ); |
1031 } |
1092 } |
1032 |
1093 |
1033 foreach ( get_taxonomies( array() , 'objects' ) as $taxonomy => $t ) { |
1094 foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy => $t ) { |
1034 if ( 'post_tag' == $taxonomy ) |
1095 if ( 'post_tag' == $taxonomy ) { |
1035 continue; // Handled further down in the $q['tag'] block |
1096 continue; // Handled further down in the $q['tag'] block |
1036 |
1097 } |
1037 if ( $t->query_var && !empty( $q[$t->query_var] ) ) { |
1098 |
|
1099 if ( $t->query_var && ! empty( $q[ $t->query_var ] ) ) { |
1038 $tax_query_defaults = array( |
1100 $tax_query_defaults = array( |
1039 'taxonomy' => $taxonomy, |
1101 'taxonomy' => $taxonomy, |
1040 'field' => 'slug', |
1102 'field' => 'slug', |
1041 ); |
1103 ); |
1042 |
1104 |
1043 if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) { |
1105 if ( isset( $t->rewrite['hierarchical'] ) && $t->rewrite['hierarchical'] ) { |
1044 $q[$t->query_var] = wp_basename( $q[$t->query_var] ); |
1106 $q[ $t->query_var ] = wp_basename( $q[ $t->query_var ] ); |
1045 } |
1107 } |
1046 |
1108 |
1047 $term = $q[$t->query_var]; |
1109 $term = $q[ $t->query_var ]; |
1048 |
1110 |
1049 if ( is_array( $term ) ) { |
1111 if ( is_array( $term ) ) { |
1050 $term = implode( ',', $term ); |
1112 $term = implode( ',', $term ); |
1051 } |
1113 } |
1052 |
1114 |
1053 if ( strpos($term, '+') !== false ) { |
1115 if ( strpos( $term, '+' ) !== false ) { |
1054 $terms = preg_split( '/[+]+/', $term ); |
1116 $terms = preg_split( '/[+]+/', $term ); |
1055 foreach ( $terms as $term ) { |
1117 foreach ( $terms as $term ) { |
1056 $tax_query[] = array_merge( $tax_query_defaults, array( |
1118 $tax_query[] = array_merge( |
1057 'terms' => array( $term ) |
1119 $tax_query_defaults, |
1058 ) ); |
1120 array( |
|
1121 'terms' => array( $term ), |
|
1122 ) |
|
1123 ); |
1059 } |
1124 } |
1060 } else { |
1125 } else { |
1061 $tax_query[] = array_merge( $tax_query_defaults, array( |
1126 $tax_query[] = array_merge( |
1062 'terms' => preg_split( '/[,]+/', $term ) |
1127 $tax_query_defaults, |
1063 ) ); |
1128 array( |
|
1129 'terms' => preg_split( '/[,]+/', $term ), |
|
1130 ) |
|
1131 ); |
1064 } |
1132 } |
1065 } |
1133 } |
1066 } |
1134 } |
1067 |
1135 |
1068 // If querystring 'cat' is an array, implode it. |
1136 // If querystring 'cat' is an array, implode it. |
1074 if ( ! empty( $q['cat'] ) && ! $this->is_singular ) { |
1142 if ( ! empty( $q['cat'] ) && ! $this->is_singular ) { |
1075 $cat_in = $cat_not_in = array(); |
1143 $cat_in = $cat_not_in = array(); |
1076 |
1144 |
1077 $cat_array = preg_split( '/[,\s]+/', urldecode( $q['cat'] ) ); |
1145 $cat_array = preg_split( '/[,\s]+/', urldecode( $q['cat'] ) ); |
1078 $cat_array = array_map( 'intval', $cat_array ); |
1146 $cat_array = array_map( 'intval', $cat_array ); |
1079 $q['cat'] = implode( ',', $cat_array ); |
1147 $q['cat'] = implode( ',', $cat_array ); |
1080 |
1148 |
1081 foreach ( $cat_array as $cat ) { |
1149 foreach ( $cat_array as $cat ) { |
1082 if ( $cat > 0 ) |
1150 if ( $cat > 0 ) { |
1083 $cat_in[] = $cat; |
1151 $cat_in[] = $cat; |
1084 elseif ( $cat < 0 ) |
1152 } elseif ( $cat < 0 ) { |
1085 $cat_not_in[] = abs( $cat ); |
1153 $cat_not_in[] = abs( $cat ); |
|
1154 } |
1086 } |
1155 } |
1087 |
1156 |
1088 if ( ! empty( $cat_in ) ) { |
1157 if ( ! empty( $cat_in ) ) { |
1089 $tax_query[] = array( |
1158 $tax_query[] = array( |
1090 'taxonomy' => 'category', |
1159 'taxonomy' => 'category', |
1091 'terms' => $cat_in, |
1160 'terms' => $cat_in, |
1092 'field' => 'term_id', |
1161 'field' => 'term_id', |
1093 'include_children' => true |
1162 'include_children' => true, |
1094 ); |
1163 ); |
1095 } |
1164 } |
1096 |
1165 |
1097 if ( ! empty( $cat_not_in ) ) { |
1166 if ( ! empty( $cat_not_in ) ) { |
1098 $tax_query[] = array( |
1167 $tax_query[] = array( |
1099 'taxonomy' => 'category', |
1168 'taxonomy' => 'category', |
1100 'terms' => $cat_not_in, |
1169 'terms' => $cat_not_in, |
1101 'field' => 'term_id', |
1170 'field' => 'term_id', |
1102 'operator' => 'NOT IN', |
1171 'operator' => 'NOT IN', |
1103 'include_children' => true |
1172 'include_children' => true, |
1104 ); |
1173 ); |
1105 } |
1174 } |
1106 unset( $cat_array, $cat_in, $cat_not_in ); |
1175 unset( $cat_array, $cat_in, $cat_not_in ); |
1107 } |
1176 } |
1108 |
1177 |
1109 if ( ! empty( $q['category__and'] ) && 1 === count( (array) $q['category__and'] ) ) { |
1178 if ( ! empty( $q['category__and'] ) && 1 === count( (array) $q['category__and'] ) ) { |
1110 $q['category__and'] = (array) $q['category__and']; |
1179 $q['category__and'] = (array) $q['category__and']; |
1111 if ( ! isset( $q['category__in'] ) ) |
1180 if ( ! isset( $q['category__in'] ) ) { |
1112 $q['category__in'] = array(); |
1181 $q['category__in'] = array(); |
|
1182 } |
1113 $q['category__in'][] = absint( reset( $q['category__and'] ) ); |
1183 $q['category__in'][] = absint( reset( $q['category__and'] ) ); |
1114 unset( $q['category__and'] ); |
1184 unset( $q['category__and'] ); |
1115 } |
1185 } |
1116 |
1186 |
1117 if ( ! empty( $q['category__in'] ) ) { |
1187 if ( ! empty( $q['category__in'] ) ) { |
1118 $q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) ); |
1188 $q['category__in'] = array_map( 'absint', array_unique( (array) $q['category__in'] ) ); |
1119 $tax_query[] = array( |
1189 $tax_query[] = array( |
1120 'taxonomy' => 'category', |
1190 'taxonomy' => 'category', |
1121 'terms' => $q['category__in'], |
1191 'terms' => $q['category__in'], |
1122 'field' => 'term_id', |
1192 'field' => 'term_id', |
1123 'include_children' => false |
1193 'include_children' => false, |
1124 ); |
1194 ); |
1125 } |
1195 } |
1126 |
1196 |
1127 if ( ! empty($q['category__not_in']) ) { |
1197 if ( ! empty( $q['category__not_in'] ) ) { |
1128 $q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) ); |
1198 $q['category__not_in'] = array_map( 'absint', array_unique( (array) $q['category__not_in'] ) ); |
1129 $tax_query[] = array( |
1199 $tax_query[] = array( |
1130 'taxonomy' => 'category', |
1200 'taxonomy' => 'category', |
1131 'terms' => $q['category__not_in'], |
1201 'terms' => $q['category__not_in'], |
1132 'operator' => 'NOT IN', |
1202 'operator' => 'NOT IN', |
1133 'include_children' => false |
1203 'include_children' => false, |
1134 ); |
1204 ); |
1135 } |
1205 } |
1136 |
1206 |
1137 if ( ! empty($q['category__and']) ) { |
1207 if ( ! empty( $q['category__and'] ) ) { |
1138 $q['category__and'] = array_map( 'absint', array_unique( (array) $q['category__and'] ) ); |
1208 $q['category__and'] = array_map( 'absint', array_unique( (array) $q['category__and'] ) ); |
1139 $tax_query[] = array( |
1209 $tax_query[] = array( |
1140 'taxonomy' => 'category', |
1210 'taxonomy' => 'category', |
1141 'terms' => $q['category__and'], |
1211 'terms' => $q['category__and'], |
1142 'field' => 'term_id', |
1212 'field' => 'term_id', |
1143 'operator' => 'AND', |
1213 'operator' => 'AND', |
1144 'include_children' => false |
1214 'include_children' => false, |
1145 ); |
1215 ); |
1146 } |
1216 } |
1147 |
1217 |
1148 // If querystring 'tag' is array, implode it. |
1218 // If querystring 'tag' is array, implode it. |
1149 if ( is_array( $q['tag'] ) ) { |
1219 if ( is_array( $q['tag'] ) ) { |
1150 $q['tag'] = implode( ',', $q['tag'] ); |
1220 $q['tag'] = implode( ',', $q['tag'] ); |
1151 } |
1221 } |
1152 |
1222 |
1153 // Tag stuff |
1223 // Tag stuff |
1154 if ( '' != $q['tag'] && !$this->is_singular && $this->query_vars_changed ) { |
1224 if ( '' != $q['tag'] && ! $this->is_singular && $this->query_vars_changed ) { |
1155 if ( strpos($q['tag'], ',') !== false ) { |
1225 if ( strpos( $q['tag'], ',' ) !== false ) { |
1156 $tags = preg_split('/[,\r\n\t ]+/', $q['tag']); |
1226 $tags = preg_split( '/[,\r\n\t ]+/', $q['tag'] ); |
1157 foreach ( (array) $tags as $tag ) { |
1227 foreach ( (array) $tags as $tag ) { |
1158 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db'); |
1228 $tag = sanitize_term_field( 'slug', $tag, 0, 'post_tag', 'db' ); |
1159 $q['tag_slug__in'][] = $tag; |
1229 $q['tag_slug__in'][] = $tag; |
1160 } |
1230 } |
1161 } elseif ( preg_match('/[+\r\n\t ]+/', $q['tag'] ) || ! empty( $q['cat'] ) ) { |
1231 } elseif ( preg_match( '/[+\r\n\t ]+/', $q['tag'] ) || ! empty( $q['cat'] ) ) { |
1162 $tags = preg_split('/[+\r\n\t ]+/', $q['tag']); |
1232 $tags = preg_split( '/[+\r\n\t ]+/', $q['tag'] ); |
1163 foreach ( (array) $tags as $tag ) { |
1233 foreach ( (array) $tags as $tag ) { |
1164 $tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db'); |
1234 $tag = sanitize_term_field( 'slug', $tag, 0, 'post_tag', 'db' ); |
1165 $q['tag_slug__and'][] = $tag; |
1235 $q['tag_slug__and'][] = $tag; |
1166 } |
1236 } |
1167 } else { |
1237 } else { |
1168 $q['tag'] = sanitize_term_field('slug', $q['tag'], 0, 'post_tag', 'db'); |
1238 $q['tag'] = sanitize_term_field( 'slug', $q['tag'], 0, 'post_tag', 'db' ); |
1169 $q['tag_slug__in'][] = $q['tag']; |
1239 $q['tag_slug__in'][] = $q['tag']; |
1170 } |
1240 } |
1171 } |
1241 } |
1172 |
1242 |
1173 if ( !empty($q['tag_id']) ) { |
1243 if ( ! empty( $q['tag_id'] ) ) { |
1174 $q['tag_id'] = absint( $q['tag_id'] ); |
1244 $q['tag_id'] = absint( $q['tag_id'] ); |
1175 $tax_query[] = array( |
1245 $tax_query[] = array( |
1176 'taxonomy' => 'post_tag', |
1246 'taxonomy' => 'post_tag', |
1177 'terms' => $q['tag_id'] |
1247 'terms' => $q['tag_id'], |
1178 ); |
1248 ); |
1179 } |
1249 } |
1180 |
1250 |
1181 if ( !empty($q['tag__in']) ) { |
1251 if ( ! empty( $q['tag__in'] ) ) { |
1182 $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) ); |
1252 $q['tag__in'] = array_map( 'absint', array_unique( (array) $q['tag__in'] ) ); |
1183 $tax_query[] = array( |
1253 $tax_query[] = array( |
1184 'taxonomy' => 'post_tag', |
1254 'taxonomy' => 'post_tag', |
1185 'terms' => $q['tag__in'] |
1255 'terms' => $q['tag__in'], |
1186 ); |
1256 ); |
1187 } |
1257 } |
1188 |
1258 |
1189 if ( !empty($q['tag__not_in']) ) { |
1259 if ( ! empty( $q['tag__not_in'] ) ) { |
1190 $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) ); |
1260 $q['tag__not_in'] = array_map( 'absint', array_unique( (array) $q['tag__not_in'] ) ); |
1191 $tax_query[] = array( |
1261 $tax_query[] = array( |
1192 'taxonomy' => 'post_tag', |
1262 'taxonomy' => 'post_tag', |
1193 'terms' => $q['tag__not_in'], |
1263 'terms' => $q['tag__not_in'], |
1194 'operator' => 'NOT IN' |
1264 'operator' => 'NOT IN', |
1195 ); |
1265 ); |
1196 } |
1266 } |
1197 |
1267 |
1198 if ( !empty($q['tag__and']) ) { |
1268 if ( ! empty( $q['tag__and'] ) ) { |
1199 $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) ); |
1269 $q['tag__and'] = array_map( 'absint', array_unique( (array) $q['tag__and'] ) ); |
1200 $tax_query[] = array( |
1270 $tax_query[] = array( |
1201 'taxonomy' => 'post_tag', |
1271 'taxonomy' => 'post_tag', |
1202 'terms' => $q['tag__and'], |
1272 'terms' => $q['tag__and'], |
1203 'operator' => 'AND' |
1273 'operator' => 'AND', |
1204 ); |
1274 ); |
1205 } |
1275 } |
1206 |
1276 |
1207 if ( !empty($q['tag_slug__in']) ) { |
1277 if ( ! empty( $q['tag_slug__in'] ) ) { |
1208 $q['tag_slug__in'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) ); |
1278 $q['tag_slug__in'] = array_map( 'sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) ); |
1209 $tax_query[] = array( |
1279 $tax_query[] = array( |
1210 'taxonomy' => 'post_tag', |
1280 'taxonomy' => 'post_tag', |
1211 'terms' => $q['tag_slug__in'], |
1281 'terms' => $q['tag_slug__in'], |
1212 'field' => 'slug' |
1282 'field' => 'slug', |
1213 ); |
1283 ); |
1214 } |
1284 } |
1215 |
1285 |
1216 if ( !empty($q['tag_slug__and']) ) { |
1286 if ( ! empty( $q['tag_slug__and'] ) ) { |
1217 $q['tag_slug__and'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) ); |
1287 $q['tag_slug__and'] = array_map( 'sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) ); |
1218 $tax_query[] = array( |
1288 $tax_query[] = array( |
1219 'taxonomy' => 'post_tag', |
1289 'taxonomy' => 'post_tag', |
1220 'terms' => $q['tag_slug__and'], |
1290 'terms' => $q['tag_slug__and'], |
1221 'field' => 'slug', |
1291 'field' => 'slug', |
1222 'operator' => 'AND' |
1292 'operator' => 'AND', |
1223 ); |
1293 ); |
1224 } |
1294 } |
1225 |
1295 |
1226 $this->tax_query = new WP_Tax_Query( $tax_query ); |
1296 $this->tax_query = new WP_Tax_Query( $tax_query ); |
1227 |
1297 |
1361 * @since 3.7.0 |
1436 * @since 3.7.0 |
1362 * |
1437 * |
1363 * @return array Stopwords. |
1438 * @return array Stopwords. |
1364 */ |
1439 */ |
1365 protected function get_search_stopwords() { |
1440 protected function get_search_stopwords() { |
1366 if ( isset( $this->stopwords ) ) |
1441 if ( isset( $this->stopwords ) ) { |
1367 return $this->stopwords; |
1442 return $this->stopwords; |
|
1443 } |
1368 |
1444 |
1369 /* translators: This is a comma-separated list of very common words that should be excluded from a search, |
1445 /* translators: This is a comma-separated list of very common words that should be excluded from a search, |
1370 * like a, an, and the. These are usually called "stopwords". You should not simply translate these individual |
1446 * like a, an, and the. These are usually called "stopwords". You should not simply translate these individual |
1371 * words into your language. Instead, look for and provide commonly accepted stopwords in your language. |
1447 * words into your language. Instead, look for and provide commonly accepted stopwords in your language. |
1372 */ |
1448 */ |
1373 $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', |
1449 $words = explode( |
1374 'Comma-separated list of search stopwords in your language' ) ); |
1450 ',', |
|
1451 _x( |
|
1452 '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', |
|
1453 'Comma-separated list of search stopwords in your language' |
|
1454 ) |
|
1455 ); |
1375 |
1456 |
1376 $stopwords = array(); |
1457 $stopwords = array(); |
1377 foreach ( $words as $word ) { |
1458 foreach ( $words as $word ) { |
1378 $word = trim( $word, "\r\n\t " ); |
1459 $word = trim( $word, "\r\n\t " ); |
1379 if ( $word ) |
1460 if ( $word ) { |
1380 $stopwords[] = $word; |
1461 $stopwords[] = $word; |
|
1462 } |
1381 } |
1463 } |
1382 |
1464 |
1383 /** |
1465 /** |
1384 * Filters stopwords used when parsing search terms. |
1466 * Filters stopwords used when parsing search terms. |
1385 * |
1467 * |
1386 * @since 3.7.0 |
1468 * @since 3.7.0 |
1387 * |
1469 * |
1388 * @param array $stopwords Stopwords. |
1470 * @param string[] $stopwords Array of stopwords. |
1389 */ |
1471 */ |
1390 $this->stopwords = apply_filters( 'wp_search_stopwords', $stopwords ); |
1472 $this->stopwords = apply_filters( 'wp_search_stopwords', $stopwords ); |
1391 return $this->stopwords; |
1473 return $this->stopwords; |
1392 } |
1474 } |
1393 |
1475 |
1460 protected function parse_orderby( $orderby ) { |
1543 protected function parse_orderby( $orderby ) { |
1461 global $wpdb; |
1544 global $wpdb; |
1462 |
1545 |
1463 // Used to filter values. |
1546 // Used to filter values. |
1464 $allowed_keys = array( |
1547 $allowed_keys = array( |
1465 'post_name', 'post_author', 'post_date', 'post_title', 'post_modified', |
1548 'post_name', |
1466 'post_parent', 'post_type', 'name', 'author', 'date', 'title', 'modified', |
1549 'post_author', |
1467 'parent', 'type', 'ID', 'menu_order', 'comment_count', 'rand', |
1550 'post_date', |
|
1551 'post_title', |
|
1552 'post_modified', |
|
1553 'post_parent', |
|
1554 'post_type', |
|
1555 'name', |
|
1556 'author', |
|
1557 'date', |
|
1558 'title', |
|
1559 'modified', |
|
1560 'parent', |
|
1561 'type', |
|
1562 'ID', |
|
1563 'menu_order', |
|
1564 'comment_count', |
|
1565 'rand', |
|
1566 'post__in', |
|
1567 'post_parent__in', |
|
1568 'post_name__in', |
1468 ); |
1569 ); |
1469 |
1570 |
1470 $primary_meta_key = ''; |
1571 $primary_meta_key = ''; |
1471 $primary_meta_query = false; |
1572 $primary_meta_query = false; |
1472 $meta_clauses = $this->meta_query->get_clauses(); |
1573 $meta_clauses = $this->meta_query->get_clauses(); |
1473 if ( ! empty( $meta_clauses ) ) { |
1574 if ( ! empty( $meta_clauses ) ) { |
1474 $primary_meta_query = reset( $meta_clauses ); |
1575 $primary_meta_query = reset( $meta_clauses ); |
1475 |
1576 |
1476 if ( ! empty( $primary_meta_query['key'] ) ) { |
1577 if ( ! empty( $primary_meta_query['key'] ) ) { |
1477 $primary_meta_key = $primary_meta_query['key']; |
1578 $primary_meta_key = $primary_meta_query['key']; |
1478 $allowed_keys[] = $primary_meta_key; |
1579 $allowed_keys[] = $primary_meta_key; |
1479 } |
1580 } |
1480 |
1581 |
1481 $allowed_keys[] = 'meta_value'; |
1582 $allowed_keys[] = 'meta_value'; |
1482 $allowed_keys[] = 'meta_value_num'; |
1583 $allowed_keys[] = 'meta_value_num'; |
1483 $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_clauses ) ); |
1584 $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_clauses ) ); |
1484 } |
1585 } |
1485 |
1586 |
1486 // If RAND() contains a seed value, sanitize and add to allowed keys. |
1587 // If RAND() contains a seed value, sanitize and add to allowed keys. |
1487 $rand_with_seed = false; |
1588 $rand_with_seed = false; |
1488 if ( preg_match( '/RAND\(([0-9]+)\)/i', $orderby, $matches ) ) { |
1589 if ( preg_match( '/RAND\(([0-9]+)\)/i', $orderby, $matches ) ) { |
1489 $orderby = sprintf( 'RAND(%s)', intval( $matches[1] ) ); |
1590 $orderby = sprintf( 'RAND(%s)', intval( $matches[1] ) ); |
1490 $allowed_keys[] = $orderby; |
1591 $allowed_keys[] = $orderby; |
1491 $rand_with_seed = true; |
1592 $rand_with_seed = true; |
1492 } |
1593 } |
1493 |
1594 |
1494 if ( ! in_array( $orderby, $allowed_keys, true ) ) { |
1595 if ( ! in_array( $orderby, $allowed_keys, true ) ) { |
1495 return false; |
1596 return false; |
1496 } |
1597 } |
|
1598 |
|
1599 $orderby_clause = ''; |
1497 |
1600 |
1498 switch ( $orderby ) { |
1601 switch ( $orderby ) { |
1499 case 'post_name': |
1602 case 'post_name': |
1500 case 'post_author': |
1603 case 'post_author': |
1501 case 'post_date': |
1604 case 'post_date': |
1635 |
1754 |
1636 // Shorthand. |
1755 // Shorthand. |
1637 $q = &$this->query_vars; |
1756 $q = &$this->query_vars; |
1638 |
1757 |
1639 // Fill again in case pre_get_posts unset some vars. |
1758 // Fill again in case pre_get_posts unset some vars. |
1640 $q = $this->fill_query_vars($q); |
1759 $q = $this->fill_query_vars( $q ); |
1641 |
1760 |
1642 // Parse meta query |
1761 // Parse meta query |
1643 $this->meta_query = new WP_Meta_Query(); |
1762 $this->meta_query = new WP_Meta_Query(); |
1644 $this->meta_query->parse_query_vars( $q ); |
1763 $this->meta_query->parse_query_vars( $q ); |
1645 |
1764 |
1646 // Set a flag if a pre_get_posts hook changed the query vars. |
1765 // Set a flag if a pre_get_posts hook changed the query vars. |
1647 $hash = md5( serialize( $this->query_vars ) ); |
1766 $hash = md5( serialize( $this->query_vars ) ); |
1648 if ( $hash != $this->query_vars_hash ) { |
1767 if ( $hash != $this->query_vars_hash ) { |
1649 $this->query_vars_changed = true; |
1768 $this->query_vars_changed = true; |
1650 $this->query_vars_hash = $hash; |
1769 $this->query_vars_hash = $hash; |
1651 } |
1770 } |
1652 unset($hash); |
1771 unset( $hash ); |
1653 |
1772 |
1654 // First let's clear some variables |
1773 // First let's clear some variables |
1655 $distinct = ''; |
1774 $distinct = ''; |
1656 $whichauthor = ''; |
1775 $whichauthor = ''; |
1657 $whichmimetype = ''; |
1776 $whichmimetype = ''; |
1658 $where = ''; |
1777 $where = ''; |
1659 $limits = ''; |
1778 $limits = ''; |
1660 $join = ''; |
1779 $join = ''; |
1661 $search = ''; |
1780 $search = ''; |
1662 $groupby = ''; |
1781 $groupby = ''; |
1663 $post_status_join = false; |
1782 $post_status_join = false; |
1664 $page = 1; |
1783 $page = 1; |
1665 |
1784 |
1666 if ( isset( $q['caller_get_posts'] ) ) { |
1785 if ( isset( $q['caller_get_posts'] ) ) { |
1667 _deprecated_argument( 'WP_Query', '3.1.0', |
1786 _deprecated_argument( |
|
1787 'WP_Query', |
|
1788 '3.1.0', |
1668 /* translators: 1: caller_get_posts, 2: ignore_sticky_posts */ |
1789 /* translators: 1: caller_get_posts, 2: ignore_sticky_posts */ |
1669 sprintf( __( '%1$s is deprecated. Use %2$s instead.' ), |
1790 sprintf( |
|
1791 __( '%1$s is deprecated. Use %2$s instead.' ), |
1670 '<code>caller_get_posts</code>', |
1792 '<code>caller_get_posts</code>', |
1671 '<code>ignore_sticky_posts</code>' |
1793 '<code>ignore_sticky_posts</code>' |
1672 ) |
1794 ) |
1673 ); |
1795 ); |
1674 |
1796 |
1675 if ( ! isset( $q['ignore_sticky_posts'] ) ) { |
1797 if ( ! isset( $q['ignore_sticky_posts'] ) ) { |
1676 $q['ignore_sticky_posts'] = $q['caller_get_posts']; |
1798 $q['ignore_sticky_posts'] = $q['caller_get_posts']; |
1677 } |
1799 } |
1678 } |
1800 } |
1679 |
1801 |
1680 if ( !isset( $q['ignore_sticky_posts'] ) ) |
1802 if ( ! isset( $q['ignore_sticky_posts'] ) ) { |
1681 $q['ignore_sticky_posts'] = false; |
1803 $q['ignore_sticky_posts'] = false; |
1682 |
1804 } |
1683 if ( !isset($q['suppress_filters']) ) |
1805 |
|
1806 if ( ! isset( $q['suppress_filters'] ) ) { |
1684 $q['suppress_filters'] = false; |
1807 $q['suppress_filters'] = false; |
1685 |
1808 } |
1686 if ( !isset($q['cache_results']) ) { |
1809 |
1687 if ( wp_using_ext_object_cache() ) |
1810 if ( ! isset( $q['cache_results'] ) ) { |
|
1811 if ( wp_using_ext_object_cache() ) { |
1688 $q['cache_results'] = false; |
1812 $q['cache_results'] = false; |
1689 else |
1813 } else { |
1690 $q['cache_results'] = true; |
1814 $q['cache_results'] = true; |
1691 } |
1815 } |
1692 |
1816 } |
1693 if ( !isset($q['update_post_term_cache']) ) |
1817 |
|
1818 if ( ! isset( $q['update_post_term_cache'] ) ) { |
1694 $q['update_post_term_cache'] = true; |
1819 $q['update_post_term_cache'] = true; |
|
1820 } |
1695 |
1821 |
1696 if ( ! isset( $q['lazy_load_term_meta'] ) ) { |
1822 if ( ! isset( $q['lazy_load_term_meta'] ) ) { |
1697 $q['lazy_load_term_meta'] = $q['update_post_term_cache']; |
1823 $q['lazy_load_term_meta'] = $q['update_post_term_cache']; |
1698 } |
1824 } |
1699 |
1825 |
1700 if ( !isset($q['update_post_meta_cache']) ) |
1826 if ( ! isset( $q['update_post_meta_cache'] ) ) { |
1701 $q['update_post_meta_cache'] = true; |
1827 $q['update_post_meta_cache'] = true; |
1702 |
1828 } |
1703 if ( !isset($q['post_type']) ) { |
1829 |
1704 if ( $this->is_search ) |
1830 if ( ! isset( $q['post_type'] ) ) { |
|
1831 if ( $this->is_search ) { |
1705 $q['post_type'] = 'any'; |
1832 $q['post_type'] = 'any'; |
1706 else |
1833 } else { |
1707 $q['post_type'] = ''; |
1834 $q['post_type'] = ''; |
|
1835 } |
1708 } |
1836 } |
1709 $post_type = $q['post_type']; |
1837 $post_type = $q['post_type']; |
1710 if ( empty( $q['posts_per_page'] ) ) { |
1838 if ( empty( $q['posts_per_page'] ) ) { |
1711 $q['posts_per_page'] = get_option( 'posts_per_page' ); |
1839 $q['posts_per_page'] = get_option( 'posts_per_page' ); |
1712 } |
1840 } |
1713 if ( isset($q['showposts']) && $q['showposts'] ) { |
1841 if ( isset( $q['showposts'] ) && $q['showposts'] ) { |
1714 $q['showposts'] = (int) $q['showposts']; |
1842 $q['showposts'] = (int) $q['showposts']; |
1715 $q['posts_per_page'] = $q['showposts']; |
1843 $q['posts_per_page'] = $q['showposts']; |
1716 } |
1844 } |
1717 if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) ) |
1845 if ( ( isset( $q['posts_per_archive_page'] ) && $q['posts_per_archive_page'] != 0 ) && ( $this->is_archive || $this->is_search ) ) { |
1718 $q['posts_per_page'] = $q['posts_per_archive_page']; |
1846 $q['posts_per_page'] = $q['posts_per_archive_page']; |
1719 if ( !isset($q['nopaging']) ) { |
1847 } |
|
1848 if ( ! isset( $q['nopaging'] ) ) { |
1720 if ( $q['posts_per_page'] == -1 ) { |
1849 if ( $q['posts_per_page'] == -1 ) { |
1721 $q['nopaging'] = true; |
1850 $q['nopaging'] = true; |
1722 } else { |
1851 } else { |
1723 $q['nopaging'] = false; |
1852 $q['nopaging'] = false; |
1724 } |
1853 } |
1773 if ( '' !== $q['menu_order'] ) { |
1905 if ( '' !== $q['menu_order'] ) { |
1774 $where .= " AND {$wpdb->posts}.menu_order = " . $q['menu_order']; |
1906 $where .= " AND {$wpdb->posts}.menu_order = " . $q['menu_order']; |
1775 } |
1907 } |
1776 // The "m" parameter is meant for months but accepts datetimes of varying specificity |
1908 // The "m" parameter is meant for months but accepts datetimes of varying specificity |
1777 if ( $q['m'] ) { |
1909 if ( $q['m'] ) { |
1778 $where .= " AND YEAR({$wpdb->posts}.post_date)=" . substr($q['m'], 0, 4); |
1910 $where .= " AND YEAR({$wpdb->posts}.post_date)=" . substr( $q['m'], 0, 4 ); |
1779 if ( strlen($q['m']) > 5 ) { |
1911 if ( strlen( $q['m'] ) > 5 ) { |
1780 $where .= " AND MONTH({$wpdb->posts}.post_date)=" . substr($q['m'], 4, 2); |
1912 $where .= " AND MONTH({$wpdb->posts}.post_date)=" . substr( $q['m'], 4, 2 ); |
1781 } |
1913 } |
1782 if ( strlen($q['m']) > 7 ) { |
1914 if ( strlen( $q['m'] ) > 7 ) { |
1783 $where .= " AND DAYOFMONTH({$wpdb->posts}.post_date)=" . substr($q['m'], 6, 2); |
1915 $where .= " AND DAYOFMONTH({$wpdb->posts}.post_date)=" . substr( $q['m'], 6, 2 ); |
1784 } |
1916 } |
1785 if ( strlen($q['m']) > 9 ) { |
1917 if ( strlen( $q['m'] ) > 9 ) { |
1786 $where .= " AND HOUR({$wpdb->posts}.post_date)=" . substr($q['m'], 8, 2); |
1918 $where .= " AND HOUR({$wpdb->posts}.post_date)=" . substr( $q['m'], 8, 2 ); |
1787 } |
1919 } |
1788 if ( strlen($q['m']) > 11 ) { |
1920 if ( strlen( $q['m'] ) > 11 ) { |
1789 $where .= " AND MINUTE({$wpdb->posts}.post_date)=" . substr($q['m'], 10, 2); |
1921 $where .= " AND MINUTE({$wpdb->posts}.post_date)=" . substr( $q['m'], 10, 2 ); |
1790 } |
1922 } |
1791 if ( strlen($q['m']) > 13 ) { |
1923 if ( strlen( $q['m'] ) > 13 ) { |
1792 $where .= " AND SECOND({$wpdb->posts}.post_date)=" . substr($q['m'], 12, 2); |
1924 $where .= " AND SECOND({$wpdb->posts}.post_date)=" . substr( $q['m'], 12, 2 ); |
1793 } |
1925 } |
1794 } |
1926 } |
1795 |
1927 |
1796 // Handle the other individual date parameters |
1928 // Handle the other individual date parameters |
1797 $date_parameters = array(); |
1929 $date_parameters = array(); |
1798 |
1930 |
1799 if ( '' !== $q['hour'] ) |
1931 if ( '' !== $q['hour'] ) { |
1800 $date_parameters['hour'] = $q['hour']; |
1932 $date_parameters['hour'] = $q['hour']; |
1801 |
1933 } |
1802 if ( '' !== $q['minute'] ) |
1934 |
|
1935 if ( '' !== $q['minute'] ) { |
1803 $date_parameters['minute'] = $q['minute']; |
1936 $date_parameters['minute'] = $q['minute']; |
1804 |
1937 } |
1805 if ( '' !== $q['second'] ) |
1938 |
|
1939 if ( '' !== $q['second'] ) { |
1806 $date_parameters['second'] = $q['second']; |
1940 $date_parameters['second'] = $q['second']; |
1807 |
1941 } |
1808 if ( $q['year'] ) |
1942 |
|
1943 if ( $q['year'] ) { |
1809 $date_parameters['year'] = $q['year']; |
1944 $date_parameters['year'] = $q['year']; |
1810 |
1945 } |
1811 if ( $q['monthnum'] ) |
1946 |
|
1947 if ( $q['monthnum'] ) { |
1812 $date_parameters['monthnum'] = $q['monthnum']; |
1948 $date_parameters['monthnum'] = $q['monthnum']; |
1813 |
1949 } |
1814 if ( $q['w'] ) |
1950 |
|
1951 if ( $q['w'] ) { |
1815 $date_parameters['week'] = $q['w']; |
1952 $date_parameters['week'] = $q['w']; |
1816 |
1953 } |
1817 if ( $q['day'] ) |
1954 |
|
1955 if ( $q['day'] ) { |
1818 $date_parameters['day'] = $q['day']; |
1956 $date_parameters['day'] = $q['day']; |
|
1957 } |
1819 |
1958 |
1820 if ( $date_parameters ) { |
1959 if ( $date_parameters ) { |
1821 $date_query = new WP_Date_Query( array( $date_parameters ) ); |
1960 $date_query = new WP_Date_Query( array( $date_parameters ) ); |
1822 $where .= $date_query->get_sql(); |
1961 $where .= $date_query->get_sql(); |
1823 } |
1962 } |
1824 unset( $date_parameters, $date_query ); |
1963 unset( $date_parameters, $date_query ); |
1825 |
1964 |
1826 // Handle complex date queries |
1965 // Handle complex date queries |
1827 if ( ! empty( $q['date_query'] ) ) { |
1966 if ( ! empty( $q['date_query'] ) ) { |
1828 $this->date_query = new WP_Date_Query( $q['date_query'] ); |
1967 $this->date_query = new WP_Date_Query( $q['date_query'] ); |
1829 $where .= $this->date_query->get_sql(); |
1968 $where .= $this->date_query->get_sql(); |
1830 } |
1969 } |
1831 |
|
1832 |
1970 |
1833 // If we've got a post_type AND it's not "any" post_type. |
1971 // If we've got a post_type AND it's not "any" post_type. |
1834 if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) { |
1972 if ( ! empty( $q['post_type'] ) && 'any' != $q['post_type'] ) { |
1835 foreach ( (array)$q['post_type'] as $_post_type ) { |
1973 foreach ( (array) $q['post_type'] as $_post_type ) { |
1836 $ptype_obj = get_post_type_object($_post_type); |
1974 $ptype_obj = get_post_type_object( $_post_type ); |
1837 if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) ) |
1975 if ( ! $ptype_obj || ! $ptype_obj->query_var || empty( $q[ $ptype_obj->query_var ] ) ) { |
1838 continue; |
1976 continue; |
|
1977 } |
1839 |
1978 |
1840 if ( ! $ptype_obj->hierarchical ) { |
1979 if ( ! $ptype_obj->hierarchical ) { |
1841 // Non-hierarchical post types can directly use 'name'. |
1980 // Non-hierarchical post types can directly use 'name'. |
1842 $q['name'] = $q[ $ptype_obj->query_var ]; |
1981 $q['name'] = $q[ $ptype_obj->query_var ]; |
1843 } else { |
1982 } else { |
1844 // Hierarchical post types will operate through 'pagename'. |
1983 // Hierarchical post types will operate through 'pagename'. |
1845 $q['pagename'] = $q[ $ptype_obj->query_var ]; |
1984 $q['pagename'] = $q[ $ptype_obj->query_var ]; |
1846 $q['name'] = ''; |
1985 $q['name'] = ''; |
1847 } |
1986 } |
1848 |
1987 |
1849 // Only one request for a slug is possible, this is why name & pagename are overwritten above. |
1988 // Only one request for a slug is possible, this is why name & pagename are overwritten above. |
1850 break; |
1989 break; |
1851 } //end foreach |
1990 } //end foreach |
1852 unset($ptype_obj); |
1991 unset( $ptype_obj ); |
1853 } |
1992 } |
1854 |
1993 |
1855 if ( '' !== $q['title'] ) { |
1994 if ( '' !== $q['title'] ) { |
1856 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_title = %s", stripslashes( $q['title'] ) ); |
1995 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_title = %s", stripslashes( $q['title'] ) ); |
1857 } |
1996 } |
1858 |
1997 |
1859 // Parameters related to 'post_name'. |
1998 // Parameters related to 'post_name'. |
1860 if ( '' != $q['name'] ) { |
1999 if ( '' != $q['name'] ) { |
1861 $q['name'] = sanitize_title_for_query( $q['name'] ); |
2000 $q['name'] = sanitize_title_for_query( $q['name'] ); |
1862 $where .= " AND {$wpdb->posts}.post_name = '" . $q['name'] . "'"; |
2001 $where .= " AND {$wpdb->posts}.post_name = '" . $q['name'] . "'"; |
1863 } elseif ( '' != $q['pagename'] ) { |
2002 } elseif ( '' != $q['pagename'] ) { |
1864 if ( isset($this->queried_object_id) ) { |
2003 if ( isset( $this->queried_object_id ) ) { |
1865 $reqpage = $this->queried_object_id; |
2004 $reqpage = $this->queried_object_id; |
1866 } else { |
2005 } else { |
1867 if ( 'page' != $q['post_type'] ) { |
2006 if ( 'page' != $q['post_type'] ) { |
1868 foreach ( (array)$q['post_type'] as $_post_type ) { |
2007 foreach ( (array) $q['post_type'] as $_post_type ) { |
1869 $ptype_obj = get_post_type_object($_post_type); |
2008 $ptype_obj = get_post_type_object( $_post_type ); |
1870 if ( !$ptype_obj || !$ptype_obj->hierarchical ) |
2009 if ( ! $ptype_obj || ! $ptype_obj->hierarchical ) { |
1871 continue; |
2010 continue; |
1872 |
2011 } |
1873 $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type); |
2012 |
1874 if ( $reqpage ) |
2013 $reqpage = get_page_by_path( $q['pagename'], OBJECT, $_post_type ); |
|
2014 if ( $reqpage ) { |
1875 break; |
2015 break; |
|
2016 } |
1876 } |
2017 } |
1877 unset($ptype_obj); |
2018 unset( $ptype_obj ); |
1878 } else { |
2019 } else { |
1879 $reqpage = get_page_by_path($q['pagename']); |
2020 $reqpage = get_page_by_path( $q['pagename'] ); |
1880 } |
2021 } |
1881 if ( !empty($reqpage) ) |
2022 if ( ! empty( $reqpage ) ) { |
1882 $reqpage = $reqpage->ID; |
2023 $reqpage = $reqpage->ID; |
1883 else |
2024 } else { |
1884 $reqpage = 0; |
2025 $reqpage = 0; |
1885 } |
2026 } |
1886 |
2027 } |
1887 $page_for_posts = get_option('page_for_posts'); |
2028 |
1888 if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) { |
2029 $page_for_posts = get_option( 'page_for_posts' ); |
|
2030 if ( ( 'page' != get_option( 'show_on_front' ) ) || empty( $page_for_posts ) || ( $reqpage != $page_for_posts ) ) { |
1889 $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) ); |
2031 $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) ); |
1890 $q['name'] = $q['pagename']; |
2032 $q['name'] = $q['pagename']; |
1891 $where .= " AND ({$wpdb->posts}.ID = '$reqpage')"; |
2033 $where .= " AND ({$wpdb->posts}.ID = '$reqpage')"; |
1892 $reqpage_obj = get_post( $reqpage ); |
2034 $reqpage_obj = get_post( $reqpage ); |
1893 if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) { |
2035 if ( is_object( $reqpage_obj ) && 'attachment' == $reqpage_obj->post_type ) { |
1894 $this->is_attachment = true; |
2036 $this->is_attachment = true; |
1895 $post_type = $q['post_type'] = 'attachment'; |
2037 $post_type = $q['post_type'] = 'attachment'; |
1896 $this->is_page = true; |
2038 $this->is_page = true; |
1897 $q['attachment_id'] = $reqpage; |
2039 $q['attachment_id'] = $reqpage; |
1898 } |
2040 } |
1899 } |
2041 } |
1900 } elseif ( '' != $q['attachment'] ) { |
2042 } elseif ( '' != $q['attachment'] ) { |
1901 $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) ); |
2043 $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) ); |
1902 $q['name'] = $q['attachment']; |
2044 $q['name'] = $q['attachment']; |
1903 $where .= " AND {$wpdb->posts}.post_name = '" . $q['attachment'] . "'"; |
2045 $where .= " AND {$wpdb->posts}.post_name = '" . $q['attachment'] . "'"; |
1904 } elseif ( is_array( $q['post_name__in'] ) && ! empty( $q['post_name__in'] ) ) { |
2046 } elseif ( is_array( $q['post_name__in'] ) && ! empty( $q['post_name__in'] ) ) { |
1905 $q['post_name__in'] = array_map( 'sanitize_title_for_query', $q['post_name__in'] ); |
2047 $q['post_name__in'] = array_map( 'sanitize_title_for_query', $q['post_name__in'] ); |
1906 $post_name__in = "'" . implode( "','", $q['post_name__in'] ) . "'"; |
2048 $post_name__in = "'" . implode( "','", $q['post_name__in'] ) . "'"; |
1907 $where .= " AND {$wpdb->posts}.post_name IN ($post_name__in)"; |
2049 $where .= " AND {$wpdb->posts}.post_name IN ($post_name__in)"; |
1908 } |
2050 } |
1909 |
2051 |
1910 // If an attachment is requested by number, let it supersede any post number. |
2052 // If an attachment is requested by number, let it supersede any post number. |
1911 if ( $q['attachment_id'] ) |
2053 if ( $q['attachment_id'] ) { |
1912 $q['p'] = absint($q['attachment_id']); |
2054 $q['p'] = absint( $q['attachment_id'] ); |
|
2055 } |
1913 |
2056 |
1914 // If a post number is specified, load that post |
2057 // If a post number is specified, load that post |
1915 if ( $q['p'] ) { |
2058 if ( $q['p'] ) { |
1916 $where .= " AND {$wpdb->posts}.ID = " . $q['p']; |
2059 $where .= " AND {$wpdb->posts}.ID = " . $q['p']; |
1917 } elseif ( $q['post__in'] ) { |
2060 } elseif ( $q['post__in'] ) { |
1918 $post__in = implode(',', array_map( 'absint', $q['post__in'] )); |
2061 $post__in = implode( ',', array_map( 'absint', $q['post__in'] ) ); |
1919 $where .= " AND {$wpdb->posts}.ID IN ($post__in)"; |
2062 $where .= " AND {$wpdb->posts}.ID IN ($post__in)"; |
1920 } elseif ( $q['post__not_in'] ) { |
2063 } elseif ( $q['post__not_in'] ) { |
1921 $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] )); |
2064 $post__not_in = implode( ',', array_map( 'absint', $q['post__not_in'] ) ); |
1922 $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)"; |
2065 $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)"; |
1923 } |
2066 } |
1924 |
2067 |
1925 if ( is_numeric( $q['post_parent'] ) ) { |
2068 if ( is_numeric( $q['post_parent'] ) ) { |
1926 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_parent = %d ", $q['post_parent'] ); |
2069 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_parent = %d ", $q['post_parent'] ); |
1927 } elseif ( $q['post_parent__in'] ) { |
2070 } elseif ( $q['post_parent__in'] ) { |
1928 $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) ); |
2071 $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) ); |
1929 $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)"; |
2072 $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)"; |
1930 } elseif ( $q['post_parent__not_in'] ) { |
2073 } elseif ( $q['post_parent__not_in'] ) { |
1931 $post_parent__not_in = implode( ',', array_map( 'absint', $q['post_parent__not_in'] ) ); |
2074 $post_parent__not_in = implode( ',', array_map( 'absint', $q['post_parent__not_in'] ) ); |
1932 $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)"; |
2075 $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)"; |
1933 } |
2076 } |
1934 |
2077 |
1935 if ( $q['page_id'] ) { |
2078 if ( $q['page_id'] ) { |
1936 if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) { |
2079 if ( ( 'page' != get_option( 'show_on_front' ) ) || ( $q['page_id'] != get_option( 'page_for_posts' ) ) ) { |
1937 $q['p'] = $q['page_id']; |
2080 $q['p'] = $q['page_id']; |
1938 $where = " AND {$wpdb->posts}.ID = " . $q['page_id']; |
2081 $where = " AND {$wpdb->posts}.ID = " . $q['page_id']; |
1939 } |
2082 } |
1940 } |
2083 } |
1941 |
2084 |
1942 // If a search pattern is specified, load the posts that match. |
2085 // If a search pattern is specified, load the posts that match. |
1943 if ( strlen( $q['s'] ) ) { |
2086 if ( strlen( $q['s'] ) ) { |
2041 unset( $the_tag ); |
2186 unset( $the_tag ); |
2042 } |
2187 } |
2043 } |
2188 } |
2044 } |
2189 } |
2045 |
2190 |
2046 if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) { |
2191 if ( ! empty( $this->tax_query->queries ) || ! empty( $this->meta_query->queries ) ) { |
2047 $groupby = "{$wpdb->posts}.ID"; |
2192 $groupby = "{$wpdb->posts}.ID"; |
2048 } |
2193 } |
2049 |
2194 |
2050 // Author/user stuff |
2195 // Author/user stuff |
2051 |
2196 |
2052 if ( ! empty( $q['author'] ) && $q['author'] != '0' ) { |
2197 if ( ! empty( $q['author'] ) && $q['author'] != '0' ) { |
2053 $q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) ); |
2198 $q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) ); |
2054 $authors = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $q['author'] ) ) ); |
2199 $authors = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $q['author'] ) ) ); |
2055 foreach ( $authors as $author ) { |
2200 foreach ( $authors as $author ) { |
2056 $key = $author > 0 ? 'author__in' : 'author__not_in'; |
2201 $key = $author > 0 ? 'author__in' : 'author__not_in'; |
2057 $q[$key][] = abs( $author ); |
2202 $q[ $key ][] = abs( $author ); |
2058 } |
2203 } |
2059 $q['author'] = implode( ',', $authors ); |
2204 $q['author'] = implode( ',', $authors ); |
2060 } |
2205 } |
2061 |
2206 |
2062 if ( ! empty( $q['author__not_in'] ) ) { |
2207 if ( ! empty( $q['author__not_in'] ) ) { |
2063 $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) ); |
2208 $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) ); |
2064 $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) "; |
2209 $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) "; |
2065 } elseif ( ! empty( $q['author__in'] ) ) { |
2210 } elseif ( ! empty( $q['author__in'] ) ) { |
2066 $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) ); |
2211 $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) ); |
2067 $where .= " AND {$wpdb->posts}.post_author IN ($author__in) "; |
2212 $where .= " AND {$wpdb->posts}.post_author IN ($author__in) "; |
2068 } |
2213 } |
2069 |
2214 |
2070 // Author stuff for nice URLs |
2215 // Author stuff for nice URLs |
2071 |
2216 |
2072 if ( '' != $q['author_name'] ) { |
2217 if ( '' != $q['author_name'] ) { |
2073 if ( strpos($q['author_name'], '/') !== false ) { |
2218 if ( strpos( $q['author_name'], '/' ) !== false ) { |
2074 $q['author_name'] = explode('/', $q['author_name']); |
2219 $q['author_name'] = explode( '/', $q['author_name'] ); |
2075 if ( $q['author_name'][ count($q['author_name'])-1 ] ) { |
2220 if ( $q['author_name'][ count( $q['author_name'] ) - 1 ] ) { |
2076 $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash |
2221 $q['author_name'] = $q['author_name'][ count( $q['author_name'] ) - 1 ]; // no trailing slash |
2077 } else { |
2222 } else { |
2078 $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailing slash |
2223 $q['author_name'] = $q['author_name'][ count( $q['author_name'] ) - 2 ]; // there was a trailing slash |
2079 } |
2224 } |
2080 } |
2225 } |
2081 $q['author_name'] = sanitize_title_for_query( $q['author_name'] ); |
2226 $q['author_name'] = sanitize_title_for_query( $q['author_name'] ); |
2082 $q['author'] = get_user_by('slug', $q['author_name']); |
2227 $q['author'] = get_user_by( 'slug', $q['author_name'] ); |
2083 if ( $q['author'] ) |
2228 if ( $q['author'] ) { |
2084 $q['author'] = $q['author']->ID; |
2229 $q['author'] = $q['author']->ID; |
2085 $whichauthor .= " AND ({$wpdb->posts}.post_author = " . absint($q['author']) . ')'; |
2230 } |
|
2231 $whichauthor .= " AND ({$wpdb->posts}.post_author = " . absint( $q['author'] ) . ')'; |
2086 } |
2232 } |
2087 |
2233 |
2088 // Matching by comment count. |
2234 // Matching by comment count. |
2089 if ( isset( $q['comment_count'] ) ) { |
2235 if ( isset( $q['comment_count'] ) ) { |
2090 // Numeric comment count is converted to array format. |
2236 // Numeric comment count is converted to array format. |
2229 |
2382 |
2230 if ( ! empty( $q['comment_status'] ) ) { |
2383 if ( ! empty( $q['comment_status'] ) ) { |
2231 $where .= $wpdb->prepare( " AND {$wpdb->posts}.comment_status = %s ", $q['comment_status'] ); |
2384 $where .= $wpdb->prepare( " AND {$wpdb->posts}.comment_status = %s ", $q['comment_status'] ); |
2232 } |
2385 } |
2233 |
2386 |
2234 if ( ! empty( $q['ping_status'] ) ) { |
2387 if ( ! empty( $q['ping_status'] ) ) { |
2235 $where .= $wpdb->prepare( " AND {$wpdb->posts}.ping_status = %s ", $q['ping_status'] ); |
2388 $where .= $wpdb->prepare( " AND {$wpdb->posts}.ping_status = %s ", $q['ping_status'] ); |
2236 } |
2389 } |
2237 |
2390 |
2238 if ( 'any' == $post_type ) { |
2391 if ( 'any' == $post_type ) { |
2239 $in_search_post_types = get_post_types( array('exclude_from_search' => false) ); |
2392 $in_search_post_types = get_post_types( array( 'exclude_from_search' => false ) ); |
2240 if ( empty( $in_search_post_types ) ) { |
2393 if ( empty( $in_search_post_types ) ) { |
2241 $where .= ' AND 1=0 '; |
2394 $where .= ' AND 1=0 '; |
2242 } else { |
2395 } else { |
2243 $where .= " AND {$wpdb->posts}.post_type IN ('" . join( "', '", array_map( 'esc_sql', $in_search_post_types ) ) . "')"; |
2396 $where .= " AND {$wpdb->posts}.post_type IN ('" . join( "', '", array_map( 'esc_sql', $in_search_post_types ) ) . "')"; |
2244 } |
2397 } |
2245 } elseif ( !empty( $post_type ) && is_array( $post_type ) ) { |
2398 } elseif ( ! empty( $post_type ) && is_array( $post_type ) ) { |
2246 $where .= " AND {$wpdb->posts}.post_type IN ('" . join("', '", esc_sql( $post_type ) ) . "')"; |
2399 $where .= " AND {$wpdb->posts}.post_type IN ('" . join( "', '", esc_sql( $post_type ) ) . "')"; |
2247 } elseif ( ! empty( $post_type ) ) { |
2400 } elseif ( ! empty( $post_type ) ) { |
2248 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_type = %s", $post_type ); |
2401 $where .= $wpdb->prepare( " AND {$wpdb->posts}.post_type = %s", $post_type ); |
2249 $post_type_object = get_post_type_object ( $post_type ); |
2402 $post_type_object = get_post_type_object( $post_type ); |
2250 } elseif ( $this->is_attachment ) { |
2403 } elseif ( $this->is_attachment ) { |
2251 $where .= " AND {$wpdb->posts}.post_type = 'attachment'"; |
2404 $where .= " AND {$wpdb->posts}.post_type = 'attachment'"; |
2252 $post_type_object = get_post_type_object ( 'attachment' ); |
2405 $post_type_object = get_post_type_object( 'attachment' ); |
2253 } elseif ( $this->is_page ) { |
2406 } elseif ( $this->is_page ) { |
2254 $where .= " AND {$wpdb->posts}.post_type = 'page'"; |
2407 $where .= " AND {$wpdb->posts}.post_type = 'page'"; |
2255 $post_type_object = get_post_type_object ( 'page' ); |
2408 $post_type_object = get_post_type_object( 'page' ); |
2256 } else { |
2409 } else { |
2257 $where .= " AND {$wpdb->posts}.post_type = 'post'"; |
2410 $where .= " AND {$wpdb->posts}.post_type = 'post'"; |
2258 $post_type_object = get_post_type_object ( 'post' ); |
2411 $post_type_object = get_post_type_object( 'post' ); |
2259 } |
2412 } |
2260 |
2413 |
2261 $edit_cap = 'edit_post'; |
2414 $edit_cap = 'edit_post'; |
2262 $read_cap = 'read_post'; |
2415 $read_cap = 'read_post'; |
2263 |
2416 |
2264 if ( ! empty( $post_type_object ) ) { |
2417 if ( ! empty( $post_type_object ) ) { |
2265 $edit_others_cap = $post_type_object->cap->edit_others_posts; |
2418 $edit_others_cap = $post_type_object->cap->edit_others_posts; |
2266 $read_private_cap = $post_type_object->cap->read_private_posts; |
2419 $read_private_cap = $post_type_object->cap->read_private_posts; |
2267 } else { |
2420 } else { |
2268 $edit_others_cap = 'edit_others_' . $post_type_cap . 's'; |
2421 $edit_others_cap = 'edit_others_' . $post_type_cap . 's'; |
2269 $read_private_cap = 'read_private_' . $post_type_cap . 's'; |
2422 $read_private_cap = 'read_private_' . $post_type_cap . 's'; |
2270 } |
2423 } |
2271 |
2424 |
2272 $user_id = get_current_user_id(); |
2425 $user_id = get_current_user_id(); |
2273 |
2426 |
2274 $q_status = array(); |
2427 $q_status = array(); |
2275 if ( ! empty( $q['post_status'] ) ) { |
2428 if ( ! empty( $q['post_status'] ) ) { |
2276 $statuswheres = array(); |
2429 $statuswheres = array(); |
2277 $q_status = $q['post_status']; |
2430 $q_status = $q['post_status']; |
2278 if ( ! is_array( $q_status ) ) |
2431 if ( ! is_array( $q_status ) ) { |
2279 $q_status = explode(',', $q_status); |
2432 $q_status = explode( ',', $q_status ); |
|
2433 } |
2280 $r_status = array(); |
2434 $r_status = array(); |
2281 $p_status = array(); |
2435 $p_status = array(); |
2282 $e_status = array(); |
2436 $e_status = array(); |
2283 if ( in_array( 'any', $q_status ) ) { |
2437 if ( in_array( 'any', $q_status ) ) { |
2284 foreach ( get_post_stati( array( 'exclude_from_search' => true ) ) as $status ) { |
2438 foreach ( get_post_stati( array( 'exclude_from_search' => true ) ) as $status ) { |
2296 } |
2450 } |
2297 } |
2451 } |
2298 } |
2452 } |
2299 } |
2453 } |
2300 |
2454 |
2301 if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) { |
2455 if ( empty( $q['perm'] ) || 'readable' != $q['perm'] ) { |
2302 $r_status = array_merge($r_status, $p_status); |
2456 $r_status = array_merge( $r_status, $p_status ); |
2303 unset($p_status); |
2457 unset( $p_status ); |
2304 } |
2458 } |
2305 |
2459 |
2306 if ( !empty($e_status) ) { |
2460 if ( ! empty( $e_status ) ) { |
2307 $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")"; |
2461 $statuswheres[] = '(' . join( ' AND ', $e_status ) . ')'; |
2308 } |
2462 } |
2309 if ( !empty($r_status) ) { |
2463 if ( ! empty( $r_status ) ) { |
2310 if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) ) { |
2464 if ( ! empty( $q['perm'] ) && 'editable' == $q['perm'] && ! current_user_can( $edit_others_cap ) ) { |
2311 $statuswheres[] = "({$wpdb->posts}.post_author = $user_id " . "AND (" . join( ' OR ', $r_status ) . "))"; |
2465 $statuswheres[] = "({$wpdb->posts}.post_author = $user_id " . 'AND (' . join( ' OR ', $r_status ) . '))'; |
2312 } else { |
2466 } else { |
2313 $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")"; |
2467 $statuswheres[] = '(' . join( ' OR ', $r_status ) . ')'; |
2314 } |
2468 } |
2315 } |
2469 } |
2316 if ( !empty($p_status) ) { |
2470 if ( ! empty( $p_status ) ) { |
2317 if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) ) { |
2471 if ( ! empty( $q['perm'] ) && 'readable' == $q['perm'] && ! current_user_can( $read_private_cap ) ) { |
2318 $statuswheres[] = "({$wpdb->posts}.post_author = $user_id " . "AND (" . join( ' OR ', $p_status ) . "))"; |
2472 $statuswheres[] = "({$wpdb->posts}.post_author = $user_id " . 'AND (' . join( ' OR ', $p_status ) . '))'; |
2319 } else { |
2473 } else { |
2320 $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")"; |
2474 $statuswheres[] = '(' . join( ' OR ', $p_status ) . ')'; |
2321 } |
2475 } |
2322 } |
2476 } |
2323 if ( $post_status_join ) { |
2477 if ( $post_status_join ) { |
2324 $join .= " LEFT JOIN {$wpdb->posts} AS p2 ON ({$wpdb->posts}.post_parent = p2.ID) "; |
2478 $join .= " LEFT JOIN {$wpdb->posts} AS p2 ON ({$wpdb->posts}.post_parent = p2.ID) "; |
2325 foreach ( $statuswheres as $index => $statuswhere ) { |
2479 foreach ( $statuswheres as $index => $statuswhere ) { |
2326 $statuswheres[$index] = "($statuswhere OR ({$wpdb->posts}.post_status = 'inherit' AND " . str_replace( $wpdb->posts, 'p2', $statuswhere ) . "))"; |
2480 $statuswheres[ $index ] = "($statuswhere OR ({$wpdb->posts}.post_status = 'inherit' AND " . str_replace( $wpdb->posts, 'p2', $statuswhere ) . '))'; |
2327 } |
2481 } |
2328 } |
2482 } |
2329 $where_status = implode( ' OR ', $statuswheres ); |
2483 $where_status = implode( ' OR ', $statuswheres ); |
2330 if ( ! empty( $where_status ) ) { |
2484 if ( ! empty( $where_status ) ) { |
2331 $where .= " AND ($where_status)"; |
2485 $where .= " AND ($where_status)"; |
2332 } |
2486 } |
2333 } elseif ( !$this->is_singular ) { |
2487 } elseif ( ! $this->is_singular ) { |
2334 $where .= " AND ({$wpdb->posts}.post_status = 'publish'"; |
2488 $where .= " AND ({$wpdb->posts}.post_status = 'publish'"; |
2335 |
2489 |
2336 // Add public states. |
2490 // Add public states. |
2337 $public_states = get_post_stati( array('public' => true) ); |
2491 $public_states = get_post_stati( array( 'public' => true ) ); |
2338 foreach ( (array) $public_states as $state ) { |
2492 foreach ( (array) $public_states as $state ) { |
2339 if ( 'publish' == $state ) // Publish is hard-coded above. |
2493 if ( 'publish' == $state ) { // Publish is hard-coded above. |
2340 continue; |
2494 continue; |
|
2495 } |
2341 $where .= " OR {$wpdb->posts}.post_status = '$state'"; |
2496 $where .= " OR {$wpdb->posts}.post_status = '$state'"; |
2342 } |
2497 } |
2343 |
2498 |
2344 if ( $this->is_admin ) { |
2499 if ( $this->is_admin ) { |
2345 // Add protected states that should show in the admin all list. |
2500 // Add protected states that should show in the admin all list. |
2346 $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) ); |
2501 $admin_all_states = get_post_stati( |
|
2502 array( |
|
2503 'protected' => true, |
|
2504 'show_in_admin_all_list' => true, |
|
2505 ) |
|
2506 ); |
2347 foreach ( (array) $admin_all_states as $state ) { |
2507 foreach ( (array) $admin_all_states as $state ) { |
2348 $where .= " OR {$wpdb->posts}.post_status = '$state'"; |
2508 $where .= " OR {$wpdb->posts}.post_status = '$state'"; |
2349 } |
2509 } |
2350 } |
2510 } |
2351 |
2511 |
2352 if ( is_user_logged_in() ) { |
2512 if ( is_user_logged_in() ) { |
2353 // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states. |
2513 // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states. |
2354 $private_states = get_post_stati( array('private' => true) ); |
2514 $private_states = get_post_stati( array( 'private' => true ) ); |
2355 foreach ( (array) $private_states as $state ) { |
2515 foreach ( (array) $private_states as $state ) { |
2356 $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'"; |
2516 $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'"; |
2357 } |
2517 } |
2358 } |
2518 } |
2359 |
2519 |
2461 * @since 2.8.0 |
2622 * @since 2.8.0 |
2462 * |
2623 * |
2463 * @param string $climits The JOIN clause of the query. |
2624 * @param string $climits The JOIN clause of the query. |
2464 * @param WP_Query $this The WP_Query instance (passed by reference). |
2625 * @param WP_Query $this The WP_Query instance (passed by reference). |
2465 */ |
2626 */ |
2466 $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) ); |
2627 $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option( 'posts_per_rss' ), &$this ) ); |
2467 } |
2628 } |
2468 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : ''; |
2629 $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : ''; |
2469 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : ''; |
2630 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : ''; |
2470 |
2631 |
2471 $comments = (array) $wpdb->get_results("SELECT $distinct {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits"); |
2632 $comments = (array) $wpdb->get_results( "SELECT $distinct {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits" ); |
2472 // Convert to WP_Comment |
2633 // Convert to WP_Comment |
2473 $this->comments = array_map( 'get_comment', $comments ); |
2634 $this->comments = array_map( 'get_comment', $comments ); |
2474 $this->comment_count = count($this->comments); |
2635 $this->comment_count = count( $this->comments ); |
2475 |
2636 |
2476 $post_ids = array(); |
2637 $post_ids = array(); |
2477 |
2638 |
2478 foreach ( $this->comments as $comment ) |
2639 foreach ( $this->comments as $comment ) { |
2479 $post_ids[] = (int) $comment->comment_post_ID; |
2640 $post_ids[] = (int) $comment->comment_post_ID; |
2480 |
2641 } |
2481 $post_ids = join(',', $post_ids); |
2642 |
2482 $join = ''; |
2643 $post_ids = join( ',', $post_ids ); |
|
2644 $join = ''; |
2483 if ( $post_ids ) { |
2645 if ( $post_ids ) { |
2484 $where = "AND {$wpdb->posts}.ID IN ($post_ids) "; |
2646 $where = "AND {$wpdb->posts}.ID IN ($post_ids) "; |
2485 } else { |
2647 } else { |
2486 $where = "AND 0"; |
2648 $where = 'AND 0'; |
2487 } |
2649 } |
2488 } |
2650 } |
2489 |
2651 |
2490 $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' ); |
2652 $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' ); |
2491 |
2653 |
2492 /* |
2654 /* |
2493 * Apply post-paging filters on where and join. Only plugins that |
2655 * Apply post-paging filters on where and join. Only plugins that |
2494 * manipulate paging queries should use these hooks. |
2656 * manipulate paging queries should use these hooks. |
2495 */ |
2657 */ |
2496 if ( !$q['suppress_filters'] ) { |
2658 if ( ! $q['suppress_filters'] ) { |
2497 /** |
2659 /** |
2498 * Filters the WHERE clause of the query. |
2660 * Filters the WHERE clause of the query. |
2499 * |
2661 * |
2500 * Specifically for manipulating paging queries. |
2662 * Specifically for manipulating paging queries. |
2501 * |
2663 * |
2698 * Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT, |
2860 * Covers the WHERE, GROUP BY, JOIN, ORDER BY, DISTINCT, |
2699 * fields (SELECT), and LIMITS clauses. |
2861 * fields (SELECT), and LIMITS clauses. |
2700 * |
2862 * |
2701 * @since 3.1.0 |
2863 * @since 3.1.0 |
2702 * |
2864 * |
2703 * @param array $pieces The pieces of the query. |
2865 * @param string[] $pieces Associative array of the pieces of the query. |
2704 * @param WP_Query $this The WP_Query instance (passed by reference). |
2866 * @param WP_Query $this The WP_Query instance (passed by reference). |
2705 */ |
2867 */ |
2706 $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) ); |
2868 $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) ); |
2707 |
2869 |
2708 $where = isset( $clauses[ 'where' ] ) ? $clauses[ 'where' ] : ''; |
2870 $where = isset( $clauses['where'] ) ? $clauses['where'] : ''; |
2709 $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : ''; |
2871 $groupby = isset( $clauses['groupby'] ) ? $clauses['groupby'] : ''; |
2710 $join = isset( $clauses[ 'join' ] ) ? $clauses[ 'join' ] : ''; |
2872 $join = isset( $clauses['join'] ) ? $clauses['join'] : ''; |
2711 $orderby = isset( $clauses[ 'orderby' ] ) ? $clauses[ 'orderby' ] : ''; |
2873 $orderby = isset( $clauses['orderby'] ) ? $clauses['orderby'] : ''; |
2712 $distinct = isset( $clauses[ 'distinct' ] ) ? $clauses[ 'distinct' ] : ''; |
2874 $distinct = isset( $clauses['distinct'] ) ? $clauses['distinct'] : ''; |
2713 $fields = isset( $clauses[ 'fields' ] ) ? $clauses[ 'fields' ] : ''; |
2875 $fields = isset( $clauses['fields'] ) ? $clauses['fields'] : ''; |
2714 $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : ''; |
2876 $limits = isset( $clauses['limits'] ) ? $clauses['limits'] : ''; |
2715 } |
2877 } |
2716 |
2878 |
2717 if ( ! empty($groupby) ) |
2879 if ( ! empty( $groupby ) ) { |
2718 $groupby = 'GROUP BY ' . $groupby; |
2880 $groupby = 'GROUP BY ' . $groupby; |
2719 if ( !empty( $orderby ) ) |
2881 } |
|
2882 if ( ! empty( $orderby ) ) { |
2720 $orderby = 'ORDER BY ' . $orderby; |
2883 $orderby = 'ORDER BY ' . $orderby; |
|
2884 } |
2721 |
2885 |
2722 $found_rows = ''; |
2886 $found_rows = ''; |
2723 if ( !$q['no_found_rows'] && !empty($limits) ) |
2887 if ( ! $q['no_found_rows'] && ! empty( $limits ) ) { |
2724 $found_rows = 'SQL_CALC_FOUND_ROWS'; |
2888 $found_rows = 'SQL_CALC_FOUND_ROWS'; |
|
2889 } |
2725 |
2890 |
2726 $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM {$wpdb->posts} $join WHERE 1=1 $where $groupby $orderby $limits"; |
2891 $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM {$wpdb->posts} $join WHERE 1=1 $where $groupby $orderby $limits"; |
2727 |
2892 |
2728 if ( !$q['suppress_filters'] ) { |
2893 if ( ! $q['suppress_filters'] ) { |
2729 /** |
2894 /** |
2730 * Filters the completed SQL query before sending. |
2895 * Filters the completed SQL query before sending. |
2731 * |
2896 * |
2732 * @since 2.0.0 |
2897 * @since 2.0.0 |
2733 * |
2898 * |
2864 /** This filter is documented in wp-includes/query.php */ |
3029 /** This filter is documented in wp-includes/query.php */ |
2865 $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) ); |
3030 $corderby = apply_filters_ref_array( 'comment_feed_orderby', array( 'comment_date_gmt DESC', &$this ) ); |
2866 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : ''; |
3031 $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : ''; |
2867 |
3032 |
2868 /** This filter is documented in wp-includes/query.php */ |
3033 /** This filter is documented in wp-includes/query.php */ |
2869 $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) ); |
3034 $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option( 'posts_per_rss' ), &$this ) ); |
2870 |
3035 |
2871 $comments_request = "SELECT {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits"; |
3036 $comments_request = "SELECT {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits"; |
2872 $comments = $wpdb->get_results($comments_request); |
3037 $comments = $wpdb->get_results( $comments_request ); |
2873 // Convert to WP_Comment |
3038 // Convert to WP_Comment |
2874 $this->comments = array_map( 'get_comment', $comments ); |
3039 $this->comments = array_map( 'get_comment', $comments ); |
2875 $this->comment_count = count($this->comments); |
3040 $this->comment_count = count( $this->comments ); |
2876 } |
3041 } |
2877 |
3042 |
2878 // Check post status to determine if post should be displayed. |
3043 // Check post status to determine if post should be displayed. |
2879 if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) { |
3044 if ( ! empty( $this->posts ) && ( $this->is_single || $this->is_page ) ) { |
2880 $status = get_post_status($this->posts[0]); |
3045 $status = get_post_status( $this->posts[0] ); |
2881 if ( 'attachment' === $this->posts[0]->post_type && 0 === (int) $this->posts[0]->post_parent ) { |
3046 if ( 'attachment' === $this->posts[0]->post_type && 0 === (int) $this->posts[0]->post_parent ) { |
2882 $this->is_page = false; |
3047 $this->is_page = false; |
2883 $this->is_single = true; |
3048 $this->is_single = true; |
2884 $this->is_attachment = true; |
3049 $this->is_attachment = true; |
2885 } |
3050 } |
2886 $post_status_obj = get_post_status_object($status); |
3051 $post_status_obj = get_post_status_object( $status ); |
2887 |
3052 |
2888 // If the post_status was specifically requested, let it pass through. |
3053 // If the post_status was specifically requested, let it pass through. |
2889 if ( !$post_status_obj->public && ! in_array( $status, $q_status ) ) { |
3054 if ( ! $post_status_obj->public && ! in_array( $status, $q_status ) ) { |
2890 |
3055 |
2891 if ( ! is_user_logged_in() ) { |
3056 if ( ! is_user_logged_in() ) { |
2892 // User must be logged in to view unpublished posts. |
3057 // User must be logged in to view unpublished posts. |
2893 $this->posts = array(); |
3058 $this->posts = array(); |
2894 } else { |
3059 } else { |
2895 if ( $post_status_obj->protected ) { |
3060 if ( $post_status_obj->protected ) { |
2896 // User must have edit permissions on the draft to preview. |
3061 // User must have edit permissions on the draft to preview. |
2897 if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) { |
3062 if ( ! current_user_can( $edit_cap, $this->posts[0]->ID ) ) { |
2898 $this->posts = array(); |
3063 $this->posts = array(); |
2899 } else { |
3064 } else { |
2900 $this->is_preview = true; |
3065 $this->is_preview = true; |
2901 if ( 'future' != $status ) |
3066 if ( 'future' != $status ) { |
2902 $this->posts[0]->post_date = current_time('mysql'); |
3067 $this->posts[0]->post_date = current_time( 'mysql' ); |
|
3068 } |
2903 } |
3069 } |
2904 } elseif ( $post_status_obj->private ) { |
3070 } elseif ( $post_status_obj->private ) { |
2905 if ( ! current_user_can($read_cap, $this->posts[0]->ID) ) |
3071 if ( ! current_user_can( $read_cap, $this->posts[0]->ID ) ) { |
2906 $this->posts = array(); |
3072 $this->posts = array(); |
|
3073 } |
2907 } else { |
3074 } else { |
2908 $this->posts = array(); |
3075 $this->posts = array(); |
2909 } |
3076 } |
2910 } |
3077 } |
2911 } |
3078 } |
2922 $this->posts[0] = get_post( apply_filters_ref_array( 'the_preview', array( $this->posts[0], &$this ) ) ); |
3089 $this->posts[0] = get_post( apply_filters_ref_array( 'the_preview', array( $this->posts[0], &$this ) ) ); |
2923 } |
3090 } |
2924 } |
3091 } |
2925 |
3092 |
2926 // Put sticky posts at the top of the posts array |
3093 // Put sticky posts at the top of the posts array |
2927 $sticky_posts = get_option('sticky_posts'); |
3094 $sticky_posts = get_option( 'sticky_posts' ); |
2928 if ( $this->is_home && $page <= 1 && is_array($sticky_posts) && !empty($sticky_posts) && !$q['ignore_sticky_posts'] ) { |
3095 if ( $this->is_home && $page <= 1 && is_array( $sticky_posts ) && ! empty( $sticky_posts ) && ! $q['ignore_sticky_posts'] ) { |
2929 $num_posts = count($this->posts); |
3096 $num_posts = count( $this->posts ); |
2930 $sticky_offset = 0; |
3097 $sticky_offset = 0; |
2931 // Loop over posts and relocate stickies to the front. |
3098 // Loop over posts and relocate stickies to the front. |
2932 for ( $i = 0; $i < $num_posts; $i++ ) { |
3099 for ( $i = 0; $i < $num_posts; $i++ ) { |
2933 if ( in_array($this->posts[$i]->ID, $sticky_posts) ) { |
3100 if ( in_array( $this->posts[ $i ]->ID, $sticky_posts ) ) { |
2934 $sticky_post = $this->posts[$i]; |
3101 $sticky_post = $this->posts[ $i ]; |
2935 // Remove sticky from current position |
3102 // Remove sticky from current position |
2936 array_splice($this->posts, $i, 1); |
3103 array_splice( $this->posts, $i, 1 ); |
2937 // Move to front, after other stickies |
3104 // Move to front, after other stickies |
2938 array_splice($this->posts, $sticky_offset, 0, array($sticky_post)); |
3105 array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) ); |
2939 // Increment the sticky offset. The next sticky will be placed at this offset. |
3106 // Increment the sticky offset. The next sticky will be placed at this offset. |
2940 $sticky_offset++; |
3107 $sticky_offset++; |
2941 // Remove post from sticky posts array |
3108 // Remove post from sticky posts array |
2942 $offset = array_search($sticky_post->ID, $sticky_posts); |
3109 $offset = array_search( $sticky_post->ID, $sticky_posts ); |
2943 unset( $sticky_posts[$offset] ); |
3110 unset( $sticky_posts[ $offset ] ); |
2944 } |
3111 } |
2945 } |
3112 } |
2946 |
3113 |
2947 // If any posts have been excluded specifically, Ignore those that are sticky. |
3114 // If any posts have been excluded specifically, Ignore those that are sticky. |
2948 if ( !empty($sticky_posts) && !empty($q['post__not_in']) ) |
3115 if ( ! empty( $sticky_posts ) && ! empty( $q['post__not_in'] ) ) { |
2949 $sticky_posts = array_diff($sticky_posts, $q['post__not_in']); |
3116 $sticky_posts = array_diff( $sticky_posts, $q['post__not_in'] ); |
|
3117 } |
2950 |
3118 |
2951 // Fetch sticky posts that weren't in the query results |
3119 // Fetch sticky posts that weren't in the query results |
2952 if ( !empty($sticky_posts) ) { |
3120 if ( ! empty( $sticky_posts ) ) { |
2953 $stickies = get_posts( array( |
3121 $stickies = get_posts( |
2954 'post__in' => $sticky_posts, |
3122 array( |
2955 'post_type' => $post_type, |
3123 'post__in' => $sticky_posts, |
2956 'post_status' => 'publish', |
3124 'post_type' => $post_type, |
2957 'nopaging' => true |
3125 'post_status' => 'publish', |
2958 ) ); |
3126 'nopaging' => true, |
|
3127 ) |
|
3128 ); |
2959 |
3129 |
2960 foreach ( $stickies as $sticky_post ) { |
3130 foreach ( $stickies as $sticky_post ) { |
2961 array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) ); |
3131 array_splice( $this->posts, $sticky_offset, 0, array( $sticky_post ) ); |
2962 $sticky_offset++; |
3132 $sticky_offset++; |
2963 } |
3133 } |