Make WordPress Core


Ignore:
Timestamp:
01/24/2018 07:11:40 AM (9 years ago)
Author:
pento
Message:

Canonical URLs: Redirect to the correct URL when the post date changes.

When a post slug is changed, we store a copy of the old slug, so that we can redirect visitors visiting the old URL to the new URL.

In the same way, this stores a copy of the old date, when the post date changes, so we can redirect visitors to the new URL.

Merge of [42401,42587,42588] to the 4.9 branch.

Props nickmomrik, frank-klein.
Fixes #15397.

Location:
branches/4.9
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.9

  • branches/4.9/src/wp-includes/query.php

    r41168 r42589  
    844844 *
    845845 * @since 2.1.0
    846  *
    847  * @global wpdb $wpdb WordPress database abstraction object.
    848846 */
    849847function wp_old_slug_redirect() {
    850848        if ( is_404() && '' !== get_query_var( 'name' ) ) {
    851                 global $wpdb;
    852 
    853849                // Guess the current post_type based on the query vars.
    854850                if ( get_query_var( 'post_type' ) ) {
     
    874870                }
    875871
    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' ) );
    877 
    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' ) );
     872                $id = _find_post_by_old_slug( $post_type );
     873
     874                if ( ! $id ) {
     875                        $id = _find_post_by_old_date( $post_type );
    882876                }
    883                 if ( get_query_var( 'monthnum' ) ) {
    884                         $query .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var( 'monthnum' ) );
    885                 }
    886                 if ( get_query_var( 'day' ) ) {
    887                         $query .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var( 'day' ) );
    888                 }
    889 
    890                 $id = (int) $wpdb->get_var( $query );
     877
     878                /**
     879                 * Filters the old slug redirect post ID.
     880                 *
     881                 * @since 4.9.3
     882                 *
     883                 * @param string $id The redirect post ID.
     884                 */
     885                $id = apply_filters( 'old_slug_redirect_post_id', $id );
    891886
    892887                if ( ! $id ) {
     
    921916
    922917/**
     918 * Find the post ID for redirecting an old slug.
     919 *
     920 * @see wp_old_slug_redirect()
     921 *
     922 * @since 4.9.3
     923 * @access private
     924 *
     925 * @global wpdb $wpdb WordPress database abstraction object.
     926 *
     927 * @param string $post_type The current post type based on the query vars.
     928 * @return int $id The Post ID.
     929 */
     930function _find_post_by_old_slug( $post_type ) {
     931        global $wpdb;
     932
     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' ) );
     934
     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' ) );
     939        }
     940        if ( get_query_var( 'monthnum' ) ) {
     941                $query .= $wpdb->prepare( " AND MONTH(post_date) = %d", get_query_var( 'monthnum' ) );
     942        }
     943        if ( get_query_var( 'day' ) ) {
     944                $query .= $wpdb->prepare( " AND DAYOFMONTH(post_date) = %d", get_query_var( 'day' ) );
     945        }
     946
     947        $id = (int) $wpdb->get_var( $query );
     948
     949        return $id;
     950}
     951
     952/**
     953 * Find the post ID for redirecting an old date.
     954 *
     955 * @see wp_old_slug_redirect()
     956 *
     957 * @since 4.9.3
     958 * @access private
     959 *
     960 * @global wpdb $wpdb WordPress database abstraction object.
     961 *
     962 * @param string $post_type The current post type based on the query vars.
     963 * @return int $id The Post ID.
     964 */
     965
     966function _find_post_by_old_date( $post_type ) {
     967        global $wpdb;
     968
     969        $date_query = '';
     970        if ( get_query_var( 'year' ) ) {
     971                $date_query .= $wpdb->prepare( " AND YEAR(pm_date.meta_value) = %d", get_query_var( 'year' ) );
     972        }
     973        if ( get_query_var( 'monthnum' ) ) {
     974                $date_query .= $wpdb->prepare( " AND MONTH(pm_date.meta_value) = %d", get_query_var( 'monthnum' ) );
     975        }
     976        if ( get_query_var( 'day' ) ) {
     977                $date_query .= $wpdb->prepare( " AND DAYOFMONTH(pm_date.meta_value) = %d", get_query_var( 'day' ) );
     978        }
     979
     980        $id = 0;
     981        if ( $date_query ) {
     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' ) ) );
     983
     984                if ( ! $id ) {
     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' ) ) );
     987                }
     988        }
     989
     990        return $id;
     991}
     992
     993/**
    923994 * Set up global post data.
    924995 *
Note: See TracChangeset for help on using the changeset viewer.