434 | | $where = $wpdb->prepare("post_name LIKE %s", like_escape( get_query_var('name') ) . '%'); |
| 434 | // Attempt to redirect bare category slugs if the permalink structure starts |
| 435 | // with the %category% tag. |
| 436 | if ( isset( $parsed_url['path'] ) |
| 437 | && preg_match( '#^[^%]+%category%#', $wp_rewrite->permalink_structure ) |
| 438 | && $cat = get_category_by_path( $parsed_url['path'] ) |
| 439 | ) { |
| 440 | if ( ! is_wp_error( $cat ) ) |
| 441 | return get_term_link( $cat ); |
| 442 | } |
436 | | // if any of post_type, year, monthnum, or day are set, use them to refine the query |
437 | | if ( get_query_var('post_type') ) |
438 | | $where .= $wpdb->prepare(" AND post_type = %s", get_query_var('post_type')); |
439 | | if ( get_query_var('year') ) |
440 | | $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year')); |
441 | | if ( get_query_var('monthnum') ) |
442 | | $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum')); |
443 | | if ( get_query_var('day') ) |
444 | | $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day')); |
| 444 | if ( get_query_var('name') ) { |
| 445 | $where = $wpdb->prepare("post_name LIKE %s", like_escape( get_query_var('name') ) . '%'); |
446 | | $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'"); |
447 | | if ( !$post_id ) |
448 | | return false; |
449 | | return get_permalink($post_id); |
| 447 | // if any of post_type, year, monthnum, or day are set, use them to refine the query |
| 448 | if ( get_query_var('post_type') ) |
| 449 | $where .= $wpdb->prepare(" AND post_type = %s", get_query_var('post_type')); |
| 450 | if ( get_query_var('year') ) |
| 451 | $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year')); |
| 452 | if ( get_query_var('monthnum') ) |
| 453 | $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum')); |
| 454 | if ( get_query_var('day') ) |
| 455 | $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day')); |
| 456 | |
| 457 | $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'"); |
| 458 | if ( ! $post_id ) |
| 459 | return false; |
| 460 | return get_permalink( $post_id ); |
| 461 | } |
| 462 | |
| 463 | return false; |