WordPressを読む 27-3 /blog/wp-includes/query.php 3
2014/12/11
目次
/blog/wp-includes/query.php 3
WP_Query::init_query_flags()
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 | /** * Resets query flags to false. * * The query flags are what page info WordPress was able to figure out. * * @since 2.0.0 * @access private */ private function init_query_flags() { $this->is_single = false; $this->is_preview = false; $this->is_page = false; $this->is_archive = false; $this->is_date = false; $this->is_year = false; $this->is_month = false; $this->is_day = false; $this->is_time = false; $this->is_author = false; $this->is_category = false; $this->is_tag = false; $this->is_tax = false; $this->is_search = false; $this->is_feed = false; $this->is_comment_feed = false; $this->is_trackback = false; $this->is_home = false; $this->is_404 = false; $this->is_comments_popup = false; $this->is_paged = false; $this->is_admin = false; $this->is_attachment = false; $this->is_singular = false; $this->is_robots = false; $this->is_posts_page = false; $this->is_post_type_archive = false; } |
WP_Query::init()
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 | /** * Initiates object properties and sets default values. * * @since 1.5.0 * @access public */ public function init() { unset($this->posts); unset($this->query); $this->query_vars = array(); unset($this->queried_object); unset($this->queried_object_id); $this->post_count = 0; $this->current_post = -1; $this->in_the_loop = false; unset( $this->request ); unset( $this->post ); unset( $this->comments ); unset( $this->comment ); $this->comment_count = 0; $this->current_comment = -1; $this->found_posts = 0; $this->max_num_pages = 0; $this->max_num_comment_pages = 0; $this->init_query_flags(); } |
WP_Query::parse_query_vars()
1 2 3 4 5 6 7 8 9 | /** * Reparse the query vars. * * @since 1.5.0 * @access public */ public function parse_query_vars() { $this->parse_query(); } |
WP_Query::fill_query_vars()
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 | /** * Fills in the query variables, which do not exist within the parameter. * * @since 2.1.0 * @access public * * @param array $array Defined query variables. * @return array Complete query variables with undefined ones filled in empty. */ public function fill_query_vars($array) { $keys = array( 'error' , 'm' , 'p' , 'post_parent' , 'subpost' , 'subpost_id' , 'attachment' , 'attachment_id' , 'name' , 'static' , 'pagename' , 'page_id' , 'second' , 'minute' , 'hour' , 'day' , 'monthnum' , 'year' , 'w' , 'category_name' , 'tag' , 'cat' , 'tag_id' , 'author' , 'author_name' , 'feed' , 'tb' , 'paged' , 'comments_popup' , 'meta_key' , 'meta_value' , 'preview' , 's' , 'sentence' , 'fields' , 'menu_order' ); foreach ( $keys as $key ) { if ( !isset($array[$key]) ) $array[$key] = ''; } $array_keys = array( 'category__in', 'category__not_in', 'category__and', 'post__in', 'post__not_in', 'tag__in', 'tag__not_in', 'tag__and', 'tag_slug__in', 'tag_slug__and', 'post_parent__in', 'post_parent__not_in', 'author__in', 'author__not_in' ); foreach ( $array_keys as $key ) { if ( !isset($array[$key]) ) $array[$key] = array(); } return $array; } |
WP_Query::parse_query()
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 | /** * Parse a query string and set query type booleans. * * @since 1.5.0 * @access public * * @param string|array $query { * Optional. Array or string of Query parameters. * * @type int $attachment_id Attachment post ID. Used for 'attachment' post_type. * @type int|string $author Author ID, or comma-separated list of IDs. * @type string $author_name User 'user_nicename'. * @type array $author__in An array of author IDs to query from. * @type array $author__not_in An array of author IDs not to query from. * @type bool $cache_results Whether to cache post information. Default true. * @type int|string $cat Category ID or comma-separated list of IDs (this or any children). * @type array $category__and An array of category IDs (AND in). * @type array $category__in An array of category IDs (OR in, no children). * @type array $category__not_in An array of category IDs (NOT in). * @type string $category_name Use category slug (not name, this or any children). * @type int $comments_per_page The number of comments to return per page. * Default 'comments_per_page' option. * @type int|string $comments_popup Whether the query is within the comments popup. Default empty. * @type array $date_query An associative array of WP_Date_Query arguments. * {@see WP_Date_Query::__construct()} * @type int $day Day of the month. Default empty. Accepts numbers 1-31. * @type bool $exact Whether to search by exact keyword. Default false. * @type string|array $fields Which fields to return. Single field or all fields (string), * or array of fields. 'id=>parent' uses 'id' and 'post_parent'. * Default all fields. Accepts 'ids', 'id=>parent'. * @type int $hour Hour of the day. Default empty. Accepts numbers 0-23. * @type bool $ignore_sticky_posts Whether to ignore sticky posts or not. Setting this to false * excludes stickies from 'post__in'. Accepts 1|true, 0|false. * Default 0|false. * @type int $m Combination YearMonth. Accepts any four-digit year and month * numbers 1-12. Default empty. * @type string $meta_compare Comparison operator to test the 'meta_value'. * @type string $meta_key Custom field key. * @type array $meta_query An associative array of WP_Meta_Query arguments. * {@see WP_Meta_Query->queries} * @type string $meta_value Custom field value. * @type int $meta_value_num Custom field value number. * @type int $menu_order The menu order of the posts. * @type int $monthnum The two-digit month. Default empty. Accepts numbers 1-12. * @type string $name Post slug. * @type bool $nopaging Show all posts (true) or paginate (false). Default false. * @type bool $no_found_rows Whether to skip counting the total rows found. Enabling can improve * performance. Default false. * @type int $offset The number of posts to offset before retrieval. * @type string $order Designates ascending or descending order of posts. Default 'DESC'. * Accepts 'ASC', 'DESC'. * @type string $orderby Sort retrieved posts by parameter. One or more options can be * passed. To use 'meta_value', or 'meta_value_num', * 'meta_key=keyname' must be also be defined. Default 'date'. * Accepts 'none', 'name', 'author', 'date', 'title', 'modified', * 'menu_order', 'parent', 'ID', 'rand', 'comment_count'. * @type int $p Post ID. * @type int $page Show the number of posts that would show up on page X of a * static front page. * @type int $paged The number of the current page. * @type int $page_id Page ID. * @type string $pagename Page slug. * @type string $perm Show posts if user has the appropriate capability. * @type array $post__in An array of post IDs to retrieve, sticky posts will be included * @type string $post_mime_type The mime type of the post. Used for 'attachment' post_type. * @type array $post__not_in An array of post IDs not to retrieve. Note: a string of comma- * separated IDs will NOT work. * @type int $post_parent Page ID to retrieve child pages for. Use 0 to only retrieve * top-level pages. * @type array $post_parent__in An array containing parent page IDs to query child pages from. * @type array $post_parent__not_in An array containing parent page IDs not to query child pages from. * @type string|array $post_type A post type slug (string) or array of post type slugs. * Default 'any' if using 'tax_query'. * @type string|array $post_status A post status (string) or array of post statuses. * @type int $posts_per_page The number of posts to query for. Use -1 to request all posts. * @type int $posts_per_archive_page The number of posts to query for by archive page. Overrides * 'posts_per_page' when is_archive(), or is_search() are true. * @type string $s Search keyword. * @type int $second Second of the minute. Default empty. Accepts numbers 0-60. * @type array $search_terms Array of search terms. * @type bool $sentence Whether to search by phrase. Default false. * @type bool $suppress_filters Whether to suppress filters. Default false. * @type string $tag Tag slug. Comma-separated (either), Plus-separated (all). * @type array $tag__and An array of tag ids (AND in). * @type array $tag__in An array of tag ids (OR in). * @type array $tag__not_in An array of tag ids (NOT in). * @type int $tag_id Tag id or comma-separated list of IDs. * @type array $tag_slug__and An array of tag slugs (AND in). * @type array $tag_slug__in An array of tag slugs (OR in). unless 'ignore_sticky_posts' is * true. Note: a string of comma-separated IDs will NOT work. * @type array $tax_query An associative array of WP_Tax_Query arguments. * {@see WP_Tax_Query->queries} * @type bool $update_post_meta_cache Whether to update the post meta cache. Default true. * @type bool $update_post_term_cache Whether to update the post term cache. Default true. * @type int $w The week number of the year. Default empty. Accepts numbers 0-53. * @type int $year The four-digit year. Default empty. Accepts any four-digit year. * } */ public function parse_query( $query = '' ) { if ( ! empty( $query ) ) { $this->init(); $this->query = $this->query_vars = wp_parse_args( $query ); } elseif ( ! isset( $this->query ) ) { $this->query = $this->query_vars; } $this->query_vars = $this->fill_query_vars($this->query_vars); $qv = &$this->query_vars; $this->query_vars_changed = true; if ( ! empty($qv['robots']) ) $this->is_robots = true; $qv['p'] = absint($qv['p']); $qv['page_id'] = absint($qv['page_id']); $qv['year'] = absint($qv['year']); $qv['monthnum'] = absint($qv['monthnum']); $qv['day'] = absint($qv['day']); $qv['w'] = absint($qv['w']); $qv['m'] = preg_replace( '|[^0-9]|', '', $qv['m'] ); $qv['paged'] = absint($qv['paged']); $qv['cat'] = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // comma separated list of positive or negative integers $qv['author'] = preg_replace( '|[^0-9,-]|', '', $qv['author'] ); // comma separated list of positive or negative integers $qv['pagename'] = trim( $qv['pagename'] ); $qv['name'] = trim( $qv['name'] ); if ( '' !== $qv['hour'] ) $qv['hour'] = absint($qv['hour']); if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']); if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']); if ( '' !== $qv['menu_order'] ) $qv['menu_order'] = absint($qv['menu_order']); // Fairly insane upper bound for search string lengths. if ( ! empty( $qv['s'] ) && strlen( $qv['s'] ) > 1600 ) $qv['s'] = ''; // Compat. Map subpost to attachment. if ( '' != $qv['subpost'] ) $qv['attachment'] = $qv['subpost']; if ( '' != $qv['subpost_id'] ) $qv['attachment_id'] = $qv['subpost_id']; $qv['attachment_id'] = absint($qv['attachment_id']); if ( ('' != $qv['attachment']) || !empty($qv['attachment_id']) ) { $this->is_single = true; $this->is_attachment = true; } elseif ( '' != $qv['name'] ) { $this->is_single = true; } elseif ( $qv['p'] ) { $this->is_single = true; } elseif ( ('' !== $qv['hour']) && ('' !== $qv['minute']) &&('' !== $qv['second']) && ('' != $qv['year']) && ('' != $qv['monthnum']) && ('' != $qv['day']) ) { // If year, month, day, hour, minute, and second are set, a single // post is being queried. $this->is_single = true; } elseif ( '' != $qv['static'] || '' != $qv['pagename'] || !empty($qv['page_id']) ) { $this->is_page = true; $this->is_single = false; } else { // Look for archive queries. Dates, categories, authors, search, post type archives. if ( isset( $this->query['s'] ) ) { $this->is_search = true; } if ( '' !== $qv['second'] ) { $this->is_time = true; $this->is_date = true; } if ( '' !== $qv['minute'] ) { $this->is_time = true; $this->is_date = true; } if ( '' !== $qv['hour'] ) { $this->is_time = true; $this->is_date = true; } if ( $qv['day'] ) { if ( ! $this->is_date ) { $date = sprintf( '%04d-%02d-%02d', $qv['year'], $qv['monthnum'], $qv['day'] ); if ( $qv['monthnum'] && $qv['year'] && ! wp_checkdate( $qv['monthnum'], $qv['day'], $qv['year'], $date ) ) { $qv['error'] = '404'; } else { $this->is_day = true; $this->is_date = true; } } } if ( $qv['monthnum'] ) { if ( ! $this->is_date ) { if ( 12 < $qv['monthnum'] ) { $qv['error'] = '404'; } else { $this->is_month = true; $this->is_date = true; } } } if ( $qv['year'] ) { if ( ! $this->is_date ) { $this->is_year = true; $this->is_date = true; } } if ( $qv['m'] ) { $this->is_date = true; if ( strlen($qv['m']) > 9 ) { $this->is_time = true; } else if ( strlen($qv['m']) > 7 ) { $this->is_day = true; } else if ( strlen($qv['m']) > 5 ) { $this->is_month = true; } else { $this->is_year = true; } } if ( '' != $qv['w'] ) { $this->is_date = true; } $this->query_vars_hash = false; $this->parse_tax_query( $qv ); foreach ( $this->tax_query->queries as $tax_query ) { if ( 'NOT IN' != $tax_query['operator'] ) { switch ( $tax_query['taxonomy'] ) { case 'category': $this->is_category = true; break; case 'post_tag': $this->is_tag = true; break; default: $this->is_tax = true; } } } unset( $tax_query ); if ( empty($qv['author']) || ($qv['author'] == '0') ) { $this->is_author = false; } else { $this->is_author = true; } if ( '' != $qv['author_name'] ) $this->is_author = true; if ( !empty( $qv['post_type'] ) && ! is_array( $qv['post_type'] ) ) { $post_type_obj = get_post_type_object( $qv['post_type'] ); if ( ! empty( $post_type_obj->has_archive ) ) $this->is_post_type_archive = true; } if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax ) $this->is_archive = true; } if ( '' != $qv['feed'] ) $this->is_feed = true; if ( '' != $qv['tb'] ) $this->is_trackback = true; if ( '' != $qv['paged'] && ( intval($qv['paged']) > 1 ) ) $this->is_paged = true; if ( '' != $qv['comments_popup'] ) $this->is_comments_popup = true; // if we're previewing inside the write screen if ( '' != $qv['preview'] ) $this->is_preview = true; if ( is_admin() ) $this->is_admin = true; if ( false !== strpos($qv['feed'], 'comments-') ) { $qv['feed'] = str_replace('comments-', '', $qv['feed']); $qv['withcomments'] = 1; } $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment; if ( $this->is_feed && ( !empty($qv['withcomments']) || ( empty($qv['withoutcomments']) && $this->is_singular ) ) ) $this->is_comment_feed = true; if ( !( $this->is_singular || $this->is_archive || $this->is_search || $this->is_feed || $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_comments_popup || $this->is_robots ) ) $this->is_home = true; // Correct is_* for page_on_front and page_for_posts if ( $this->is_home && 'page' == get_option('show_on_front') && get_option('page_on_front') ) { $_query = wp_parse_args($this->query); // pagename can be set and empty depending on matched rewrite rules. Ignore an empty pagename. if ( isset($_query['pagename']) && '' == $_query['pagename'] ) unset($_query['pagename']); if ( empty($_query) || !array_diff( array_keys($_query), array('preview', 'page', 'paged', 'cpage') ) ) { $this->is_page = true; $this->is_home = false; $qv['page_id'] = get_option('page_on_front'); // Correct <!--nextpage--> for page_on_front if ( !empty($qv['paged']) ) { $qv['page'] = $qv['paged']; unset($qv['paged']); } } } if ( '' != $qv['pagename'] ) { $this->queried_object = get_page_by_path($qv['pagename']); if ( !empty($this->queried_object) ) $this->queried_object_id = (int) $this->queried_object->ID; else unset($this->queried_object); if ( 'page' == get_option('show_on_front') && isset($this->queried_object_id) && $this->queried_object_id == get_option('page_for_posts') ) { $this->is_page = false; $this->is_home = true; $this->is_posts_page = true; } } if ( $qv['page_id'] ) { if ( 'page' == get_option('show_on_front') && $qv['page_id'] == get_option('page_for_posts') ) { $this->is_page = false; $this->is_home = true; $this->is_posts_page = true; } } if ( !empty($qv['post_type']) ) { if ( is_array($qv['post_type']) ) $qv['post_type'] = array_map('sanitize_key', $qv['post_type']); else $qv['post_type'] = sanitize_key($qv['post_type']); } if ( ! empty( $qv['post_status'] ) ) { if ( is_array( $qv['post_status'] ) ) $qv['post_status'] = array_map('sanitize_key', $qv['post_status']); else $qv['post_status'] = preg_replace('|[^a-z0-9_,-]|', '', $qv['post_status']); } if ( $this->is_posts_page && ( ! isset($qv['withcomments']) || ! $qv['withcomments'] ) ) $this->is_comment_feed = false; $this->is_singular = $this->is_single || $this->is_page || $this->is_attachment; // Done correcting is_* for page_on_front and page_for_posts if ( '404' == $qv['error'] ) $this->set_404(); $this->query_vars_hash = md5( serialize( $this->query_vars ) ); $this->query_vars_changed = false; /** * Fires after the main query vars have been parsed. * * @since 1.5.0 * * @param WP_Query &$this The WP_Query instance (passed by reference). */ do_action_ref_array( 'parse_query', array( &$this ) ); } |