Make WordPress Core

Changeset 42401

Timestamp:
12/15/2017 08:30:50 AM (7 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.

Props nickmomrik.
Fixes #15397 for trunk.

Location:
trunk
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/default-filters.php

    r42343 r42401  
    355355add_action( 'attachment_updated', 'wp_check_for_changed_slugs', 12, 3 );
    356356
     357
     358
     359
     360
    357361// Nonce check for Post Previews
    358362add_action( 'init', '_show_post_preview' );
  • trunk/src/wp-includes/post.php

    r42398 r42401  
    57095709
    57105710/**
     5711
     5712
     5713
     5714
     5715
     5716
     5717
     5718
     5719
     5720
     5721
     5722
     5723
     5724
     5725
     5726
     5727
     5728
     5729
     5730
     5731
     5732
     5733
     5734
     5735
     5736
     5737
     5738
     5739
     5740
     5741
     5742
     5743
     5744
     5745
     5746
     5747
     5748
     5749
     5750
     5751
    57115752 * Retrieve the private post SQL based on capability.
    57125753 *
  • trunk/src/wp-includes/query.php

    r42355 r42401  
    846846 *
    847847 * @since 2.1.0
    848  *
    849  * @global wpdb $wpdb WordPress database abstraction object.
    850848 */
    851849function wp_old_slug_redirect() {
    852850    if ( is_404() && '' !== get_query_var( 'name' ) ) {
    853         global $wpdb;
    854 
    855851        // Guess the current post_type based on the query vars.
    856852        if ( get_query_var( 'post_type' ) ) {
     
    876872        }
    877873
    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 );
    884878        }
    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 );
    893888
    894889        if ( ! $id ) {
     
    923918
    924919/**
     920
     921
     922
     923
     924
     925
     926
     927
     928
     929
     930
     931
     932
     933
     934
     935
     936
     937
     938
     939
     940
     941
     942
     943
     944
     945
     946
     947
     948
     949
     950
     951
     952
     953
     954
     955
     956
     957
     958
     959
     960
     961
     962
     963
     964
     965
     966
     967
     968
     969
     970
     971
     972
     973
     974
     975
     976
     977
     978
     979
     980
     981
     982
     983
     984
     985
     986
     987
     988
     989
     990
     991
     992
     993
     994
     995
    925996 * Set up global post data.
    926997 *
Note: See TracChangeset for help on using the changeset viewer.