WordPressを読む 27-7 /blog/wp-includes/query.php 7
2014/12/11
目次
- 1 /blog/wp-includes/query.php 7
- 2 WP_Query::set_found_posts()
- 3 WP_Query::next_post()
- 4 WP_Query::the_post()
- 5 WP_Query::have_posts()
- 6 WP_Query::rewind_posts()
- 7 WP_Query::next_comment()
- 8 WP_Query::the_comment()
- 9 WP_Query::have_comments()
- 10 WP_Query::rewind_comments()
- 11 WP_Query::query()
- 12 WP_Query::get_queried_object()
- 13 WP_Query::get_queried_object_id()
- 14 WP_Query::__construct()
- 15 WP_Query::__get()
- 16 WP_Query::__isset()
- 17 WP_Query::__unset()
- 18 WP_Query:: __call()
- 19 WP_Query::is_archive()
/blog/wp-includes/query.php 7
WP_Query::set_found_posts()
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 | /** * Set up the amount of found posts and the number of pages (if limit clause was used) * for the current query. * * @since 3.5.0 * @access private */ private function set_found_posts( $q, $limits ) { global $wpdb; // Bail if posts is an empty array. Continue if posts is an empty string, // null, or false to accommodate caching plugins that fill posts later. if ( $q['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) ) return; if ( ! empty( $limits ) ) { /** * Filter the query to run for retrieving the found posts. * * @since 2.1.0 * * @param string $found_posts The query to run to find the found posts. * @param WP_Query &$this The WP_Query instance (passed by reference). */ $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) ); } else { $this->found_posts = count( $this->posts ); } /** * Filter the number of found posts for the query. * * @since 2.1.0 * * @param int $found_posts The number of posts found. * @param WP_Query &$this The WP_Query instance (passed by reference). */ $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ); if ( ! empty( $limits ) ) $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] ); } |
WP_Query::next_post()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** * Set up the next post and iterate current post index. * * @since 1.5.0 * @access public * * @return WP_Post Next post. */ public function next_post() { $this->current_post++; $this->post = $this->posts[$this->current_post]; return $this->post; } |
WP_Query::the_post()
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 | /** * Sets up the current post. * * Retrieves the next post, sets up the post, sets the 'in the loop' * property to true. * * @since 1.5.0 * @access public * @uses $post * @uses do_action_ref_array() Calls 'loop_start' if loop has just started */ public function the_post() { global $post; $this->in_the_loop = true; if ( $this->current_post == -1 ) // loop has just started /** * Fires once the loop is started. * * @since 2.0.0 * * @param WP_Query &$this The WP_Query instance (passed by reference). */ do_action_ref_array( 'loop_start', array( &$this ) ); $post = $this->next_post(); setup_postdata($post); } |
WP_Query::have_posts()
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 | /** * Whether there are more posts available in the loop. * * Calls action 'loop_end', when the loop is complete. * * @since 1.5.0 * @access public * @uses do_action_ref_array() Calls 'loop_end' if loop is ended * * @return bool True if posts are available, false if end of loop. */ public function have_posts() { if ( $this->current_post + 1 < $this->post_count ) { return true; } elseif ( $this->current_post + 1 == $this->post_count && $this->post_count > 0 ) { /** * Fires once the loop has ended. * * @since 2.0.0 * * @param WP_Query &$this The WP_Query instance (passed by reference). */ do_action_ref_array( 'loop_end', array( &$this ) ); // Do some cleaning up after the loop $this->rewind_posts(); } $this->in_the_loop = false; return false; } |
WP_Query::rewind_posts()
1 2 3 4 5 6 7 8 9 10 11 12 | /** * Rewind the posts and reset post index. * * @since 1.5.0 * @access public */ public function rewind_posts() { $this->current_post = -1; if ( $this->post_count > 0 ) { $this->post = $this->posts[0]; } } |
WP_Query::next_comment()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * Iterate current comment index and return comment object. * * @since 2.2.0 * @access public * * @return object Comment object. */ public function next_comment() { $this->current_comment++; $this->comment = $this->comments[$this->current_comment]; return $this->comment; } |
WP_Query::the_comment()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | /** * Sets up the current comment. * * @since 2.2.0 * @access public * @global object $comment Current comment. * @uses do_action() Calls 'comment_loop_start' hook when first comment is processed. */ public function the_comment() { global $comment; $comment = $this->next_comment(); if ( $this->current_comment == 0 ) { /** * Fires once the comment loop is started. * * @since 2.2.0 */ do_action( 'comment_loop_start' ); } } |
WP_Query::have_comments()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /** * Whether there are more comments available. * * Automatically rewinds comments when finished. * * @since 2.2.0 * @access public * * @return bool True, if more comments. False, if no more posts. */ public function have_comments() { if ( $this->current_comment + 1 < $this->comment_count ) { return true; } elseif ( $this->current_comment + 1 == $this->comment_count ) { $this->rewind_comments(); } return false; } |
WP_Query::rewind_comments()
1 2 3 4 5 6 7 8 9 10 11 12 | /** * Rewind the comments, resets the comment index and comment to first. * * @since 2.2.0 * @access public */ public function rewind_comments() { $this->current_comment = -1; if ( $this->comment_count > 0 ) { $this->comment = $this->comments[0]; } } |
WP_Query::query()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | /** * Sets up the WordPress query by parsing query string. * * @since 1.5.0 * @access public * * @param string $query URL query string. * @return array List of posts. */ public function query( $query ) { $this->init(); $this->query = $this->query_vars = wp_parse_args( $query ); return $this->get_posts(); } |
WP_Query::get_queried_object()
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 | /** * Retrieve queried object. * * If queried object is not set, then the queried object will be set from * the category, tag, taxonomy, posts page, single post, page, or author * query variable. After it is set up, it will be returned. * * @since 1.5.0 * @access public * * @return object */ public function get_queried_object() { if ( isset($this->queried_object) ) return $this->queried_object; $this->queried_object = null; $this->queried_object_id = 0; if ( $this->is_category || $this->is_tag || $this->is_tax ) { if ( $this->is_category ) { if ( $this->get( 'cat' ) ) { $term = get_term( $this->get( 'cat' ), 'category' ); } elseif ( $this->get( 'category_name' ) ) { $term = get_term_by( 'slug', $this->get( 'category_name' ), 'category' ); } } elseif ( $this->is_tag ) { if ( $this->get( 'tag_id' ) ) { $term = get_term( $this->get( 'tag_id' ), 'post_tag' ); } elseif ( $this->get( 'tag' ) ) { $term = get_term_by( 'slug', $this->get( 'tag' ), 'post_tag' ); } } else { $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' ); $query = reset( $tax_query_in_and ); if ( $query['terms'] ) { if ( 'term_id' == $query['field'] ) { $term = get_term( reset( $query['terms'] ), $query['taxonomy'] ); } else { $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] ); } } } if ( ! empty( $term ) && ! is_wp_error( $term ) ) { $this->queried_object = $term; $this->queried_object_id = (int) $term->term_id; if ( $this->is_category && 'category' === $this->queried_object->taxonomy ) _make_cat_compat( $this->queried_object ); } } elseif ( $this->is_post_type_archive ) { $post_type = $this->get( 'post_type' ); if ( is_array( $post_type ) ) $post_type = reset( $post_type ); $this->queried_object = get_post_type_object( $post_type ); } elseif ( $this->is_posts_page ) { $page_for_posts = get_option('page_for_posts'); $this->queried_object = get_post( $page_for_posts ); $this->queried_object_id = (int) $this->queried_object->ID; } elseif ( $this->is_singular && ! empty( $this->post ) ) { $this->queried_object = $this->post; $this->queried_object_id = (int) $this->post->ID; } elseif ( $this->is_author ) { $this->queried_object_id = (int) $this->get('author'); $this->queried_object = get_userdata( $this->queried_object_id ); } return $this->queried_object; } |
WP_Query::get_queried_object_id()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | /** * Retrieve ID of the current queried object. * * @since 1.5.0 * @access public * * @return int */ public function get_queried_object_id() { $this->get_queried_object(); if ( isset($this->queried_object_id) ) { return $this->queried_object_id; } return 0; } |
WP_Query::__construct()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | /** * Constructor. * * Sets up the WordPress query, if parameter is not empty. * * @since 1.5.0 * @access public * * @param string $query URL query string. * @return WP_Query */ public function __construct($query = '') { if ( ! empty($query) ) { $this->query($query); } } |
WP_Query::__get()
1 2 3 4 5 6 7 8 9 10 11 12 | /** * Make private properties readable for backwards compatibility. * * @since 4.0.0 * @access public * * @param string $name Property to get. * @return mixed Property. */ public function __get( $name ) { return $this->$name; } |
WP_Query::__isset()
1 2 3 4 5 6 7 8 9 10 11 12 | /** * Make private properties settable for backwards compatibility. * * @since 4.0.0 * @access public * * @param string $name Property to check if set. * @return bool Whether the property is set. */ public function __isset( $name ) { return isset( $this->$name ); } |
WP_Query::__unset()
1 2 3 4 5 6 7 8 9 10 11 | /** * Make private properties settable for backwards compatibility. * * @since 4.0.0 * @access public * * @param string $name Property to unset. */ public function __unset( $name ) { unset( $this->$name ); } |
WP_Query:: __call()
1 2 3 4 5 6 7 8 9 10 11 12 13 | /** * Make private/protected methods readable for backwards compatibility. * * @since 4.0.0 * @access public * * @param callable $name Method to call. * @param array $arguments Arguments to pass when calling. * @return mixed|bool Return value of the callback, otherwise false. */ public function __call( $name, $arguments ) { return call_user_func_array( array( $this, $name ), $arguments ); } |
WP_Query::is_archive()
1 2 3 4 5 6 7 8 9 10 11 12 | /** * Is the query for an existing archive page? * * Month, Year, Category, Author, Post Type archive... * * @since 3.1.0 * * @return bool */ public function is_archive() { return (bool) $this->is_archive; } |