WordPressを読む 27-9 /blog/wp-includes/query.php 9
2014/12/11
目次
- 1 /blog/wp-includes/query.php 9
- 2 WP_Query::is_page()
- 3 WP_Query::is_paged()
- 4 WP_Query::is_preview()
- 5 WP_Query::is_robots()
- 6 WP_Query::is_search()
- 7 WP_Query::is_single()
- 8 WP_Query::is_singular()
- 9 WP_Query::is_time()
- 10 WP_Query::is_trackback()
- 11 WP_Query::is_year()
- 12 WP_Query::is_404()
- 13 WP_Query::is_main_query()
- 14 WP_Query::reset_postdata()
- 15 関数 wp_old_slug_redirect()
- 16 関数 setup_postdata()
/blog/wp-includes/query.php 9
WP_Query::is_page()
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 | /** * Is the query for an existing single page? * * If the $page parameter is specified, this function will additionally * check if the query is for one of the pages specified. * * @see WP_Query::is_single() * @see WP_Query::is_singular() * * @since 3.1.0 * * @param mixed $page Page ID, title, slug, path, or array of such. * @return bool */ public function is_page( $page = '' ) { if ( !$this->is_page ) return false; if ( empty( $page ) ) return true; $page_obj = $this->get_queried_object(); $page = (array) $page; if ( in_array( $page_obj->ID, $page ) ) { return true; } elseif ( in_array( $page_obj->post_title, $page ) ) { return true; } else if ( in_array( $page_obj->post_name, $page ) ) { return true; } else { foreach ( $page as $pagepath ) { if ( ! strpos( $pagepath, '/' ) ) { continue; } $pagepath_obj = get_page_by_path( $pagepath ); if ( $pagepath_obj && ( $pagepath_obj->ID == $page_obj->ID ) ) { return true; } } } return false; } |
WP_Query::is_paged()
1 2 3 4 5 6 7 8 9 10 | /** * Is the query for paged result and not for the first page? * * @since 3.1.0 * * @return bool */ public function is_paged() { return (bool) $this->is_paged; } |
WP_Query::is_preview()
1 2 3 4 5 6 7 8 9 10 | /** * Is the query for a post or page preview? * * @since 3.1.0 * * @return bool */ public function is_preview() { return (bool) $this->is_preview; } |
WP_Query::is_robots()
1 2 3 4 5 6 7 8 9 10 | /** * Is the query for the robots file? * * @since 3.1.0 * * @return bool */ public function is_robots() { return (bool) $this->is_robots; } |
WP_Query::is_search()
1 2 3 4 5 6 7 8 9 10 | /** * Is the query for a search? * * @since 3.1.0 * * @return bool */ public function is_search() { return (bool) $this->is_search; } |
WP_Query::is_single()
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 | /** * Is the query for an existing single post? * * Works for any post type, except attachments and pages * * If the $post parameter is specified, this function will additionally * check if the query is for one of the Posts specified. * * @see WP_Query::is_page() * @see WP_Query::is_singular() * * @since 3.1.0 * * @param mixed $post Post ID, title, slug, path, or array of such. * @return bool */ public function is_single( $post = '' ) { if ( !$this->is_single ) return false; if ( empty($post) ) return true; $post_obj = $this->get_queried_object(); $post = (array) $post; if ( in_array( $post_obj->ID, $post ) ) { return true; } elseif ( in_array( $post_obj->post_title, $post ) ) { return true; } elseif ( in_array( $post_obj->post_name, $post ) ) { return true; } else { foreach ( $post as $postpath ) { if ( ! strpos( $postpath, '/' ) ) { continue; } $postpath_obj = get_page_by_path( $postpath, OBJECT, $post_obj->post_type ); if ( $postpath_obj && ( $postpath_obj->ID == $post_obj->ID ) ) { return true; } } } return false; } |
WP_Query::is_singular()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | /** * Is the query for an existing single post of any post type (post, attachment, page, ... )? * * If the $post_types parameter is specified, this function will additionally * check if the query is for one of the Posts Types specified. * * @see WP_Query::is_page() * @see WP_Query::is_single() * * @since 3.1.0 * * @param mixed $post_types Optional. Post Type or array of Post Types * @return bool */ public function is_singular( $post_types = '' ) { if ( empty( $post_types ) || !$this->is_singular ) return (bool) $this->is_singular; $post_obj = $this->get_queried_object(); return in_array( $post_obj->post_type, (array) $post_types ); } |
WP_Query::is_time()
1 2 3 4 5 6 7 8 9 10 | /** * Is the query for a specific time? * * @since 3.1.0 * * @return bool */ public function is_time() { return (bool) $this->is_time; } |
WP_Query::is_trackback()
1 2 3 4 5 6 7 8 9 10 | /** * Is the query for a trackback endpoint call? * * @since 3.1.0 * * @return bool */ public function is_trackback() { return (bool) $this->is_trackback; } |
WP_Query::is_year()
1 2 3 4 5 6 7 8 9 10 | /** * Is the query for an existing year archive? * * @since 3.1.0 * * @return bool */ public function is_year() { return (bool) $this->is_year; } |
WP_Query::is_404()
1 2 3 4 5 6 7 8 9 10 | /** * Is the query a 404 (returns no results)? * * @since 3.1.0 * * @return bool */ public function is_404() { return (bool) $this->is_404; } |
WP_Query::is_main_query()
1 2 3 4 5 6 7 8 9 10 11 | /** * Is the query the main query? * * @since 3.3.0 * * @return bool */ public function is_main_query() { global $wp_the_query; return $wp_the_query === $this; } |
WP_Query::reset_postdata()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | /** * After looping through a nested query, this function * restores the $post global to the current post in this query. * * @since 3.7.0 * * @return bool */ public function reset_postdata() { if ( ! empty( $this->post ) ) { $GLOBALS['post'] = $this->post; setup_postdata( $this->post ); } } } |
WP_Query::クラス定義終了
関数 wp_old_slug_redirect()
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 | /** * Redirect old slugs to the correct permalink. * * Attempts to find the current slug from the past slugs. * * @since 2.1.0 * @uses $wp_query * @uses $wpdb * * @return null If no link is found, null is returned. */ function wp_old_slug_redirect() { global $wp_query; if ( is_404() && '' != $wp_query->query_vars['name'] ) : global $wpdb; // Guess the current post_type based on the query vars. if ( get_query_var('post_type') ) $post_type = get_query_var('post_type'); elseif ( !empty($wp_query->query_vars['pagename']) ) $post_type = 'page'; else $post_type = 'post'; if ( is_array( $post_type ) ) { if ( count( $post_type ) > 1 ) return; $post_type = array_shift( $post_type ); } // Do not attempt redirect for hierarchical post types if ( is_post_type_hierarchical( $post_type ) ) return; $query = $wpdb->prepare("SELECT post_id FROM $wpdb->postmeta, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_slug' AND meta_value = %s", $post_type, $wp_query->query_vars['name']); // if year, monthnum, or day have been specified, make our query more precise // just in case there are multiple identical _wp_old_slug values if ( '' != $wp_query->query_vars['year'] ) $query .= $wpdb->prepare(" AND YEAR(post_date) = %d", $wp_query->query_vars['year']); if ( '' != $wp_query->query_vars['monthnum'] ) $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", $wp_query->query_vars['monthnum']); if ( '' != $wp_query->query_vars['day'] ) $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", $wp_query->query_vars['day']); $id = (int) $wpdb->get_var($query); if ( ! $id ) return; $link = get_permalink($id); if ( !$link ) return; wp_redirect( $link, 301 ); // Permanent redirect exit; endif; } |
関数 setup_postdata()
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 | /** * Set up global post data. * * @since 1.5.0 * * @param object $post Post data. * @uses do_action_ref_array() Calls 'the_post' * @return bool True when finished. */ function setup_postdata( $post ) { global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages; $id = (int) $post->ID; $authordata = get_userdata($post->post_author); $currentday = mysql2date('d.m.y', $post->post_date, false); $currentmonth = mysql2date('m', $post->post_date, false); $numpages = 1; $multipage = 0; $page = get_query_var('page'); if ( ! $page ) $page = 1; if ( is_single() || is_page() || is_feed() ) $more = 1; $content = $post->post_content; if ( false !== strpos( $content, '<!--nextpage-->' ) ) { if ( $page > 1 ) $more = 1; $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content ); $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content ); $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content ); // Ignore nextpage at the beginning of the content. if ( 0 === strpos( $content, '<!--nextpage-->' ) ) $content = substr( $content, 15 ); $pages = explode('<!--nextpage-->', $content); $numpages = count($pages); if ( $numpages > 1 ) $multipage = 1; } else { $pages = array( $post->post_content ); } /** * Fires once the post data has been setup. * * @since 2.8.0 * * @param WP_Post &$post The Post object (passed by reference). */ do_action_ref_array( 'the_post', array( &$post ) ); return true; } |