redirect_guess_404_permalink()
最后更新于:2021-11-27 21:02:01
redirect_guess_404_permalink()Attempts to guess the correct URL for a 404 request based on query vars.
响应
(string|false) The correct URL if one is found. False on failure.
源文件
文件: gc-includes/canonical.php
function redirect_guess_404_permalink() {
global $gcdb;
/**
* Filters whether to attempt to guess a redirect URL for a 404 request.
*
* 响应ing a false value from the filter will disable the URL guessing
* and return early without performing a redirect.
*
* @since 5.5.0
*
* @param bool $do_redirect_guess Whether to attempt to guess a redirect URL
* for a 404 request. Default true.
*/
if ( false === apply_filters( 'do_redirect_guess_404_permalink', true ) ) {
return false;
}
/**
* Short-circuits the redirect URL guessing for 404 requests.
*
* 响应ing a non-null value from the filter will effectively short-circuit
* the URL guessing, returning the passed value instead.
*
* @since 5.5.0
*
* @param null|string|false $pre Whether to short-circuit guessing the redirect for a 404.
* Default null to continue with the URL guessing.
*/
$pre = apply_filters( 'pre_redirect_guess_404_permalink', null );
if ( null !== $pre ) {
return $pre;
}
if ( get_query_var( 'name' ) ) {
/**
* Filters whether to perform a strict guess for a 404 redirect.
*
* 响应ing a truthy value from the filter will redirect only exact post_name matches.
*
* @since 5.5.0
*
* @param bool $strict_guess Whether to perform a strict guess. Default false (loose guess).
*/
$strict_guess = apply_filters( 'strict_redirect_guess_404_permalink', false );
if ( $strict_guess ) {
$where = $gcdb->prepare( 'post_name = %s', get_query_var( 'name' ) );
} else {
$where = $gcdb->prepare( 'post_name LIKE %s', $gcdb->esc_like( get_query_var( 'name' ) ) . '%' );
}
// If any of post_type, year, monthnum, or day are set, use them to refine the query.
if ( get_query_var( 'post_type' ) ) {
if ( is_array( get_query_var( 'post_type' ) ) ) {
// phpcs:ignore GeChiUI.DB.PreparedSQLPlaceholders.UnfinishedPrepare
$where .= " AND post_type IN ('" . join( "', '", esc_sql( get_query_var( 'post_type' ) ) ) . "')";
} else {
$where .= $gcdb->prepare( ' AND post_type = %s', get_query_var( 'post_type' ) );
}
} else {
$where .= " AND post_type IN ('" . implode( "', '", get_post_types( array( 'public' => true ) ) ) . "')";
}
if ( get_query_var( 'year' ) ) {
$where .= $gcdb->prepare( ' AND YEAR(post_date) = %d', get_query_var( 'year' ) );
}
if ( get_query_var( 'monthnum' ) ) {
$where .= $gcdb->prepare( ' AND MONTH(post_date) = %d', get_query_var( 'monthnum' ) );
}
if ( get_query_var( 'day' ) ) {
$where .= $gcdb->prepare( ' AND DAYOFMONTH(post_date) = %d', get_query_var( 'day' ) );
}
// phpcs:ignore GeChiUI.DB.PreparedSQL.InterpolatedNotPrepared
$post_id = $gcdb->get_var( "SELECT ID FROM $gcdb->posts WHERE $where AND post_status = 'publish'" );
if ( ! $post_id ) {
return false;
}
if ( get_query_var( 'feed' ) ) {
return get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) );
} elseif ( get_query_var( 'page' ) > 1 ) {
return trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' );
} else {
return get_permalink( $post_id );
}
}
return false;
}