Changeset 42401 for trunk/src/wp-includes/query.php
- Timestamp:
- 12/15/2017 08:30:50 AM (8 years ago)
- File:
-
- 1 edited
-
trunk/src/wp-includes/query.php (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/query.php
r42355 r42401 846 846 * 847 847 * @since 2.1.0 848 *849 * @global wpdb $wpdb WordPress database abstraction object.850 848 */ 851 849 function wp_old_slug_redirect() { 852 850 if ( is_404() && '' !== get_query_var( 'name' ) ) { 853 global $wpdb;854 855 851 // Guess the current post_type based on the query vars. 856 852 if ( get_query_var( 'post_type' ) ) { … … 876 872 } 877 873 878 $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, get_query_var( 'name' ) ); 879 880 // if year, monthnum, or day have been specified, make our query more precise 881 // just in case there are multiple identical _wp_old_slug values 882 if ( get_query_var( 'year' ) ) { 883 $query .= $wpdb->prepare( ' AND YEAR(post_date) = %d', get_query_var( 'year' ) ); 874 $id = _find_post_by_old_slug( $post_type ); 875 876 if ( ! $id ) { 877 $id = _find_post_by_old_date( $post_type ); 884 878 } 885 if ( get_query_var( 'monthnum' ) ) { 886 $query .= $wpdb->prepare( ' AND MONTH(post_date) = %d', get_query_var( 'monthnum' ) ); 887 } 888 if ( get_query_var( 'day' ) ) { 889 $query .= $wpdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) ); 890 } 891 892 $id = (int) $wpdb->get_var( $query ); 879 880 /** 881 * Filters the old slug redirect post ID. 882 * 883 * @since 4.9.2 884 * 885 * @param string $id The redirect post ID. 886 */ 887 $id = apply_filters( 'old_slug_redirect_post_id', $id ); 893 888 894 889 if ( ! $id ) { … … 923 918 924 919 /** 920 * Find the post ID for redirecting an old slug. 921 * 922 * @see wp_old_slug_redirect() 923 * 924 * @since 4.9.2 925 * @access private 926 * 927 * @global wpdb $wpdb WordPress database abstraction object. 928 * 929 * @param string $post_type The current post type based on the query vars. 930 * @return int $id The Post ID. 931 */ 932 function _find_post_by_old_slug( $post_type ) { 933 global $wpdb; 934 935 $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, get_query_var( 'name' ) ); 936 937 // if year, monthnum, or day have been specified, make our query more precise 938 // just in case there are multiple identical _wp_old_slug values 939 if ( get_query_var( 'year' ) ) { 940 $query .= $wpdb->prepare( " AND YEAR(post_date) = %d", get_query_var( 'year' ) ); 941 } 942 if ( get_query_var( 'monthnum' ) ) { 943 $query .= $wpdb->prepare( " AND MONTH(post_date) = %d", get_query_var( 'monthnum' ) ); 944 } 945 if ( get_query_var( 'day' ) ) { 946 $query .= $wpdb->prepare( " AND DAYOFMONTH(post_date) = %d", get_query_var( 'day' ) ); 947 } 948 949 $id = (int) $wpdb->get_var( $query ); 950 951 return $id; 952 } 953 954 /** 955 * Find the post ID for redirecting an old date. 956 * 957 * @see wp_old_slug_redirect() 958 * 959 * @since 4.9.2 960 * @access private 961 * 962 * @global wpdb $wpdb WordPress database abstraction object. 963 * 964 * @param string $post_type The current post type based on the query vars. 965 * @return int $id The Post ID. 966 */ 967 968 function _find_post_by_old_date( $post_type ) { 969 global $wpdb; 970 971 $date_query = ''; 972 if ( get_query_var( 'year' ) ) { 973 $date_query .= $wpdb->prepare( " AND YEAR(pm_date.meta_value) = %d", get_query_var( 'year' ) ); 974 } 975 if ( get_query_var( 'monthnum' ) ) { 976 $date_query .= $wpdb->prepare( " AND MONTH(pm_date.meta_value) = %d", get_query_var( 'monthnum' ) ); 977 } 978 if ( get_query_var( 'day' ) ) { 979 $date_query .= $wpdb->prepare( " AND DAYOFMONTH(pm_date.meta_value) = %d", get_query_var( 'day' ) ); 980 } 981 982 $id = 0; 983 if ( $date_query ) { 984 $id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta AS pm_date, $wpdb->posts WHERE ID = post_id AND post_type = %s AND meta_key = '_wp_old_date' AND post_name = %s" . $date_query, $post_type, get_query_var( 'name' ) ) ); 985 986 if ( ! $id ) { 987 // Check to see if an old slug matches the old date 988 $id = (int) $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts, $wpdb->postmeta AS pm_slug, $wpdb->postmeta AS pm_date WHERE ID = pm_slug.post_id AND ID = pm_date.post_id AND post_type = %s AND pm_slug.meta_key = '_wp_old_slug' AND pm_slug.meta_value = %s AND pm_date.meta_key = '_wp_old_date'" . $date_query, $post_type, get_query_var( 'name' ) ) ); 989 } 990 } 991 992 return $id; 993 } 994 995 /** 925 996 * Set up global post data. 926 997 *
Note: See TracChangeset
for help on using the changeset viewer.