546 | | // if any of post_type, year, monthnum, or day are set, use them to refine the query |
547 | | if ( get_query_var('post_type') ) |
548 | | $where .= $wpdb->prepare(" AND post_type = %s", get_query_var('post_type')); |
549 | | else |
550 | | $where .= " AND post_type IN ('" . implode( "', '", get_post_types( array( 'public' => true ) ) ) . "')"; |
| 547 | $where = $wpdb->prepare("post_name LIKE %s", $wpdb->esc_like( get_query_var('name') ) . '%'); |
552 | | if ( get_query_var('year') ) |
553 | | $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year')); |
554 | | if ( get_query_var('monthnum') ) |
555 | | $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum')); |
556 | | if ( get_query_var('day') ) |
557 | | $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day')); |
| 549 | // if any of post_type, year, monthnum, or day are set, use them to refine the query |
| 550 | if ( get_query_var( 'post_type' ) ) { |
| 551 | $where .= $wpdb->prepare( " AND post_type = %s", get_query_var( 'post_type' ) ); |
| 552 | } else { |
| 553 | $query_params = array(); |
| 554 | parse_str( $query['query'], $query_params ); |
| 555 | $post_types = get_post_types( array( 'public' => true, '_builtin' => false ), 'objects' ); |
559 | | $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'"); |
560 | | if ( ! $post_id ) |
561 | | return false; |
562 | | if ( get_query_var( 'feed' ) ) |
563 | | return get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) ); |
564 | | elseif ( get_query_var( 'page' ) ) |
565 | | return trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' ); |
566 | | else |
567 | | return get_permalink( $post_id ); |
| 557 | $in = array(); |
| 558 | foreach ( $post_types as $obj ) { |
| 559 | if ( ! is_post_type_viewable( $obj ) || ! $obj->query_var ) { |
| 560 | continue; |
| 561 | } |
| 562 | |
| 563 | if ( false !== strpos( $query['path'], $obj->query_var . '/' ) ) { |
| 564 | $in[] = $obj->name; |
| 565 | break; |
| 566 | } elseif ( ! empty( $query_params[ $obj->query_var ] ) ) { |
| 567 | $in[] = $obj->name; |
| 568 | break; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | if ( ! empty( $in ) ) { |
| 573 | $where .= " AND post_type IN ('" . implode( "', '", $in ) . "')"; |
| 574 | } elseif ( get_query_var( 'pagename' ) && ! ( get_query_var('year') || get_query_var('monthnum') || get_query_var('day') ) ) { |
| 575 | // /2015/something/ when /2015/09/18/something is the real permalink |
| 576 | $match = preg_match( '#/[0-9]{4}/#', $query['path'] ); |
| 577 | if ( ! $match || ( $match[0] == get_query_var( 'pagename' ) ) ) { |
| 578 | $where .= ' AND post_type = "page"'; |
| 579 | } else { |
| 580 | $where .= ' AND post_type = "post"'; |
| 581 | } |
| 582 | } else { |
| 583 | $where .= ' AND post_type = "post"'; |
| 584 | } |
570 | | return false; |
| 587 | if ( get_query_var('year') ) |
| 588 | $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year')); |
| 589 | if ( get_query_var('monthnum') ) |
| 590 | $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum')); |
| 591 | if ( get_query_var('day') ) |
| 592 | $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day')); |
| 593 | |
| 594 | $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'"); |
| 595 | if ( ! $post_id ) |
| 596 | return false; |
| 597 | if ( get_query_var( 'feed' ) ) |
| 598 | return get_post_comments_feed_link( $post_id, get_query_var( 'feed' ) ); |
| 599 | elseif ( get_query_var( 'page' ) ) |
| 600 | return trailingslashit( get_permalink( $post_id ) ) . user_trailingslashit( get_query_var( 'page' ), 'single_paged' ); |
| 601 | else |
| 602 | return get_permalink( $post_id ); |