843
843
* Attempts to find the current slug from the past slugs.
847
* @global wpdb $wpdb WordPress database abstraction object.
849
847
function wp_old_slug_redirect() {
850
848
if ( is_404() && '' !== get_query_var( 'name' ) ) {
853
849
// Guess the current post_type based on the query vars.
854
850
if ( get_query_var( 'post_type' ) ) {
855
851
$post_type = get_query_var( 'post_type' );
876
$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' ) );
878
// if year, monthnum, or day have been specified, make our query more precise
879
// just in case there are multiple identical _wp_old_slug values
880
if ( get_query_var( 'year' ) ) {
881
$query .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var( 'year' ) );
883
if ( get_query_var( 'monthnum' ) ) {
884
$query .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var( 'monthnum' ) );
886
if ( get_query_var( 'day' ) ) {
887
$query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var( 'day' ) );
890
$id = (int) $wpdb->get_var( $query );
872
$id = _find_post_by_old_slug( $post_type );
875
$id = _find_post_by_old_date( $post_type );
879
* Filters the old slug redirect post ID.
883
* @param string $id The redirect post ID.
885
$id = apply_filters( 'old_slug_redirect_post_id', $id );
918
* Find the post ID for redirecting an old slug.
920
* @see wp_old_slug_redirect()
925
* @global wpdb $wpdb WordPress database abstraction object.
927
* @param string $post_type The current post type based on the query vars.
928
* @return int $id The Post ID.
930
function _find_post_by_old_slug( $post_type ) {
933
$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' ) );
935
// if year, monthnum, or day have been specified, make our query more precise
936
// just in case there are multiple identical _wp_old_slug values
937
if ( get_query_var( 'year' ) ) {
938
$query .= $wpdb->prepare( " AND YEAR(post_date) = %d", get_query_var( 'year' ) );
940
if ( get_query_var( 'monthnum' ) ) {
941
$query .= $wpdb->prepare( " AND MONTH(post_date) = %d", get_query_var( 'monthnum' ) );
943
if ( get_query_var( 'day' ) ) {
944
$query .= $wpdb->prepare( " AND DAYOFMONTH(post_date) = %d", get_query_var( 'day' ) );
947
$id = (int) $wpdb->get_var( $query );
953
* Find the post ID for redirecting an old date.
955
* @see wp_old_slug_redirect()
960
* @global wpdb $wpdb WordPress database abstraction object.
962
* @param string $post_type The current post type based on the query vars.
963
* @return int $id The Post ID.
966
function _find_post_by_old_date( $post_type ) {
970
if ( get_query_var( 'year' ) ) {
971
$date_query .= $wpdb->prepare( " AND YEAR(pm_date.meta_value) = %d", get_query_var( 'year' ) );
973
if ( get_query_var( 'monthnum' ) ) {
974
$date_query .= $wpdb->prepare( " AND MONTH(pm_date.meta_value) = %d", get_query_var( 'monthnum' ) );
976
if ( get_query_var( 'day' ) ) {
977
$date_query .= $wpdb->prepare( " AND DAYOFMONTH(pm_date.meta_value) = %d", get_query_var( 'day' ) );
982
$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
// Check to see if an old slug matches the old date
986
$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' ) ) );
923
994
* Set up global post data.