WordPress.org

Make WordPress Core

Ticket #16557: filters to tweak permalink guessing.diff

File filters to tweak permalink guessing.diff, 1.8 KB (added by simonwheatley, 7 years ago)

One possibility to allow filtering the where clauses for posts and pages

  • wp-includes/canonical.php

     
    391391        if ( !get_query_var('name') )
    392392                return false;
    393393
    394         $where = $wpdb->prepare("post_name LIKE %s", like_escape( get_query_var('name') ) . '%');
     394        $page_where = $wpdb->prepare("post_name LIKE %s", like_escape( get_query_var('name') ) . '%');
     395        $page_where = apply_filters( 'redirect_guess_page_where', $page_where );
    395396
    396397        // if any of post_type, year, monthnum, or day are set, use them to refine the query
     398        $post_where = '';
    397399        if ( get_query_var('post_type') )
    398                 $where .= $wpdb->prepare(" AND post_type = %s", get_query_var('post_type'));
     400                $post_where .= $wpdb->prepare(" AND post_type = %s", get_query_var('post_type'));
    399401        if ( get_query_var('year') )
    400                 $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year'));
     402                $post_where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year'));
    401403        if ( get_query_var('monthnum') )
    402                 $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum'));
     404                $post_where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum'));
    403405        if ( get_query_var('day') )
    404                 $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day'));
     406                $post_where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day'));
     407        $post_where = apply_filters( 'redirect_guess_post_where', $post_where );
    405408
    406         $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
     409        $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $page_where $post_where AND post_status = 'publish'");
    407410        if ( !$post_id )
    408411                return false;
    409412        return get_permalink($post_id);