redirect_guess_404_permalink

Attempts to guess the correct URL based on query vars.

Description

(false|string) redirect_guess_404_permalink(); 

Returns (false|string)

The correct URL if one is found. False on failure.


Usage

  1. if ( !function_exists( 'redirect_guess_404_permalink' ) ) { 
  2. require_once ABSPATH . WPINC . '/canonical.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = redirect_guess_404_permalink(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/canonical.php  
  1. function redirect_guess_404_permalink() { 
  2. global $wpdb; 
  3.  
  4. if ( get_query_var('name') ) { 
  5. $where = $wpdb->prepare("post_name LIKE %s", $wpdb->esc_like( get_query_var('name') ) . '%'); 
  6.  
  7. // if any of post_type, year, monthnum, or day are set, use them to refine the query 
  8. if ( get_query_var('post_type') ) 
  9. $where .= $wpdb->prepare(" AND post_type = %s", get_query_var('post_type')); 
  10. else 
  11. $where .= " AND post_type IN ('" . implode( "', '", get_post_types( array( 'public' => true ) ) ) . "')"; 
  12.  
  13. if ( get_query_var('year') ) 
  14. $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year')); 
  15. if ( get_query_var('monthnum') ) 
  16. $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum')); 
  17. if ( get_query_var('day') ) 
  18. $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day')); 
  19.  
  20. $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'"); 
  21. if ( ! $post_id ) 
  22. return false; 
  23. if ( get_query_var( 'feed' ) ) 
  24. return get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) ); 
  25. elseif ( get_query_var( 'page' ) && 1 < get_query_var( 'page' ) ) 
  26. return trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' ); 
  27. else 
  28. return get_permalink( $post_id ); 
  29.  
  30. return false;