| 404 | * Retrieves post data by a given field. |
| 405 | * |
| 406 | * See {@link &get_post()} for optional filter and output arguments values. |
| 407 | * |
| 408 | * @since 3.1.0 |
| 409 | * |
| 410 | * @param string $field The field to retrieve the post with. id | title | path |
| 411 | * @param int|string $value A value for $field. A post ID, title, or path. |
| 412 | * @param string|array $args List of arguments to overwrite the defaults |
| 413 | * string $post_type Optional. Post type. Default post. |
| 414 | * string $output Optional. Output type. |
| 415 | * string $filter How the return value should be filtered. |
| 416 | * @return mixed |
| 417 | */ |
| 418 | function get_post_by($field, $value, $args = '') { |
| 419 | global $wpdb; |
| 420 | |
| 421 | $defaults = array( |
| 422 | 'post_type' => 'post', 'output' => 'OBJECT', |
| 423 | 'filter' => 'raw' |
| 424 | ); |
| 425 | |
| 426 | $r = wp_parse_args( $args, $defaults ); |
| 427 | extract( $r, EXTR_SKIP ); |
| 428 | |
| 429 | switch ($field) { |
| 430 | case 'id': |
| 431 | $p = get_post( $value, $output, $filter ); |
| 432 | if ( $p->post_type == $post_type ) |
| 433 | return $p; |
| 434 | else |
| 435 | return null; |
| 436 | break; |
| 437 | |
| 438 | case 'title': |
| 439 | $post = $wpdb->get_var( $wpdb->prepare( "SELECT * FROM $wpdb->posts WHERE post_title = %s AND post_type = %s", $value, $post_type ) ); |
| 440 | if ( $post ) |
| 441 | return $post; |
| 442 | else |
| 443 | return null; |
| 444 | break; |
| 445 | |
| 446 | case 'path': |
| 447 | $page_path = rawurlencode( urldecode( $value ) ); |
| 448 | $page_path = str_replace( '%2F', '/', $page_path ); |
| 449 | $page_path = str_replace( '%20', ' ', $page_path ); |
| 450 | $page_paths = '/' . trim( $page_path, '/' ); |
| 451 | $leaf_path = sanitize_title( basename( $page_paths ) ); |
| 452 | $page_paths = explode( '/', $page_paths ); |
| 453 | $full_path = ''; |
| 454 | foreach ( (array) $page_paths as $pathdir ) |
| 455 | $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title( $pathdir ); |
| 456 | |
| 457 | $pages = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name = %s AND (post_type = %s OR post_type = 'attachment')", $leaf_path, $post_type )); |
| 458 | |
| 459 | if ( empty( $pages ) ) |
| 460 | return null; |
| 461 | |
| 462 | foreach ( $pages as $page ) { |
| 463 | $path = '/' . $leaf_path; |
| 464 | $curpage = $page; |
| 465 | while ( $curpage->post_parent != 0 ) { |
| 466 | $curpage = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = %d and post_type = %s", $curpage->post_parent, $post_type ) ); |
| 467 | $path = '/' . $curpage->post_name . $path; |
| 468 | } |
| 469 | |
| 470 | if ( $path == $full_path ) |
| 471 | return get_post( $page->ID, $output, $filter ); |
| 472 | } |
| 473 | break; |
| 474 | |
| 475 | default: |
| 476 | return null; |
| 477 | } |
| 478 | |
| 479 | } |
| 480 | |
| 481 | /** |
2891 | | global $wpdb; |
2892 | | $page_path = rawurlencode(urldecode($page_path)); |
2893 | | $page_path = str_replace('%2F', '/', $page_path); |
2894 | | $page_path = str_replace('%20', ' ', $page_path); |
2895 | | $page_paths = '/' . trim($page_path, '/'); |
2896 | | $leaf_path = sanitize_title(basename($page_paths)); |
2897 | | $page_paths = explode('/', $page_paths); |
2898 | | $full_path = ''; |
2899 | | foreach ( (array) $page_paths as $pathdir ) |
2900 | | $full_path .= ( $pathdir != '' ? '/' : '' ) . sanitize_title($pathdir); |
2901 | | |
2902 | | $pages = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_name = %s AND (post_type = %s OR post_type = 'attachment')", $leaf_path, $post_type )); |
2903 | | |
2904 | | if ( empty($pages) ) |
2905 | | return null; |
2906 | | |
2907 | | foreach ( $pages as $page ) { |
2908 | | $path = '/' . $leaf_path; |
2909 | | $curpage = $page; |
2910 | | while ( $curpage->post_parent != 0 ) { |
2911 | | $curpage = $wpdb->get_row( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE ID = %d and post_type = %s", $curpage->post_parent, $post_type )); |
2912 | | $path = '/' . $curpage->post_name . $path; |
2913 | | } |
2914 | | |
2915 | | if ( $path == $full_path ) |
2916 | | return get_page($page->ID, $output, $post_type); |
2917 | | } |
2918 | | |
2919 | | return null; |
| 2969 | $args = array( |
| 2970 | 'output' => $output, 'post_type' => $post_type |
| 2971 | ); |
| 2972 | return get_post_by('path', $page_path, $args); |