| 3367 | | $inclusions = ''; |
| 3368 | | if ( !empty($include) ) { |
| 3369 | | $child_of = 0; //ignore child_of, parent, exclude, meta_key, and meta_value params if using include |
| 3370 | | $parent = -1; |
| 3371 | | $exclude = ''; |
| 3372 | | $meta_key = ''; |
| 3373 | | $meta_value = ''; |
| 3374 | | $hierarchical = false; |
| 3375 | | $incpages = wp_parse_id_list( $include ); |
| 3376 | | if ( ! empty( $incpages ) ) { |
| 3377 | | foreach ( $incpages as $incpage ) { |
| 3378 | | if (empty($inclusions)) |
| 3379 | | $inclusions = $wpdb->prepare(' AND ( ID = %d ', $incpage); |
| 3380 | | else |
| 3381 | | $inclusions .= $wpdb->prepare(' OR ID = %d ', $incpage); |
| 3382 | | } |
| 3383 | | } |
| 3384 | | } |
| 3385 | | if (!empty($inclusions)) |
| 3386 | | $inclusions .= ')'; |
| | 3374 | // TODO Rename $post_authors to $requested_authors |
| | 3375 | if ( ! empty( $authors ) ) { |
| | 3376 | $post_authors = preg_split( '/[\s,]+/',$authors ); |
| 3388 | | $exclusions = ''; |
| 3389 | | if ( !empty($exclude) ) { |
| 3390 | | $expages = wp_parse_id_list( $exclude ); |
| 3391 | | if ( ! empty( $expages ) ) { |
| 3392 | | foreach ( $expages as $expage ) { |
| 3393 | | if (empty($exclusions)) |
| 3394 | | $exclusions = $wpdb->prepare(' AND ( ID <> %d ', $expage); |
| 3395 | | else |
| 3396 | | $exclusions .= $wpdb->prepare(' AND ID <> %d ', $expage); |
| 3397 | | } |
| 3398 | | } |
| 3399 | | } |
| 3400 | | if (!empty($exclusions)) |
| 3401 | | $exclusions .= ')'; |
| 3402 | | |
| 3403 | | $author_query = ''; |
| 3404 | | if (!empty($authors)) { |
| 3405 | | $post_authors = preg_split('/[\s,]+/',$authors); |
| 3406 | | |
| 3414 | | if ( empty($post_author->ID) ) |
| 3415 | | continue; |
| 3416 | | $post_author = $post_author->ID; |
| 3417 | | } |
| 3418 | | |
| 3419 | | if ( '' == $author_query ) |
| 3420 | | $author_query = $wpdb->prepare(' post_author = %d ', $post_author); |
| 3421 | | else |
| 3422 | | $author_query .= $wpdb->prepare(' OR post_author = %d ', $post_author); |
| 3423 | | } |
| 3424 | | if ( '' != $author_query ) |
| 3425 | | $author_query = " AND ($author_query)"; |
| | 3385 | $author_ids[] = $post_author->ID; |
| | 3386 | } else { |
| | 3387 | $author_ids[] = $post_author; |
| | 3388 | } |
| | 3389 | } |
| 3429 | | $join = ''; |
| 3430 | | $where = "$exclusions $inclusions "; |
| 3431 | | if ( ! empty( $meta_key ) || ! empty( $meta_value ) ) { |
| 3432 | | $join = " LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )"; |
| | 3394 | // get_pages() API supports both post_author and author for sorting, and WP_Query only supports the version without the post_ prefix |
| | 3395 | // get_pages accepts comma-separated list of sort_columns, and WP_Query accepts a space-separated one |
| | 3396 | // TODO get_pages had this additional sorting field. Figure out what to do about this one: $allowed_keys = array( 'modified_gmt', 'post_modified_gmt' ); |
| | 3397 | $sort_column = str_replace( array( 'post_', ',' ), array( '', ' ' ), $sort_column ); |
| 3434 | | // meta_key and meta_value might be slashed |
| 3435 | | $meta_key = stripslashes($meta_key); |
| 3436 | | $meta_value = stripslashes($meta_value); |
| 3437 | | if ( ! empty( $meta_key ) ) |
| 3438 | | $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s", $meta_key); |
| 3439 | | if ( ! empty( $meta_value ) ) |
| 3440 | | $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_value = %s", $meta_value); |
| | 3399 | if ( ! empty( $r['number'] ) && empty( $r['posts_per_page'] ) ) |
| | 3400 | $r['posts_per_page'] = $r['number']; |
| | 3401 | if ( ! empty( $include ) ) { |
| | 3402 | $incposts = wp_parse_id_list( $include ); |
| | 3403 | $r['posts_per_page'] = count($incposts); // only the number of posts included |
| | 3404 | $r['post__in'] = $incposts; |
| | 3405 | } elseif ( ! empty( $exclude ) ) |
| | 3406 | $r['post__not_in'] = wp_parse_id_list( $exclude ); |
| 3447 | | if ( 1 == count( $post_status ) ) { |
| 3448 | | $where_post_type = $wpdb->prepare( "post_type = %s AND post_status = %s", $post_type, array_shift( $post_status ) ); |
| 3449 | | } else { |
| 3450 | | $post_status = implode( "', '", $post_status ); |
| 3451 | | $where_post_type = $wpdb->prepare( "post_type = %s AND post_status IN ('$post_status')", $post_type ); |
| 3452 | | } |
| | 3416 | $r['ignore_sticky_posts'] = true; |
| | 3417 | $r['no_found_rows'] = true; |
| 3454 | | $orderby_array = array(); |
| 3455 | | $allowed_keys = array('author', 'post_author', 'date', 'post_date', 'title', 'post_title', 'name', 'post_name', 'modified', |
| 3456 | | 'post_modified', 'modified_gmt', 'post_modified_gmt', 'menu_order', 'parent', 'post_parent', |
| 3457 | | 'ID', 'rand', 'comment_count'); |
| 3458 | | foreach ( explode( ',', $sort_column ) as $orderby ) { |
| 3459 | | $orderby = trim( $orderby ); |
| 3460 | | if ( !in_array( $orderby, $allowed_keys ) ) |
| 3461 | | continue; |
| | 3419 | $get_pages = new WP_Query; |
| | 3420 | $pages = $get_pages->query( $r ); |
| 3463 | | switch ( $orderby ) { |
| 3464 | | case 'menu_order': |
| 3465 | | break; |
| 3466 | | case 'ID': |
| 3467 | | $orderby = "$wpdb->posts.ID"; |
| 3468 | | break; |
| 3469 | | case 'rand': |
| 3470 | | $orderby = 'RAND()'; |
| 3471 | | break; |
| 3472 | | case 'comment_count': |
| 3473 | | $orderby = "$wpdb->posts.comment_count"; |
| 3474 | | break; |
| 3475 | | default: |
| 3476 | | if ( 0 === strpos( $orderby, 'post_' ) ) |
| 3477 | | $orderby = "$wpdb->posts." . $orderby; |
| 3478 | | else |
| 3479 | | $orderby = "$wpdb->posts.post_" . $orderby; |
| 3480 | | } |
| 3481 | | |
| 3482 | | $orderby_array[] = $orderby; |
| 3483 | | |
| 3484 | | } |
| 3485 | | $sort_column = ! empty( $orderby_array ) ? implode( ',', $orderby_array ) : "$wpdb->posts.post_title"; |
| 3486 | | |
| 3487 | | $sort_order = strtoupper( $sort_order ); |
| 3488 | | if ( '' !== $sort_order && !in_array( $sort_order, array( 'ASC', 'DESC' ) ) ) |
| 3489 | | $sort_order = 'ASC'; |
| 3490 | | |
| 3491 | | $query = "SELECT * FROM $wpdb->posts $join WHERE ($where_post_type) $where "; |
| 3492 | | $query .= $author_query; |
| 3493 | | $query .= " ORDER BY " . $sort_column . " " . $sort_order ; |
| 3494 | | |
| 3495 | | if ( !empty($number) ) |
| 3496 | | $query .= ' LIMIT ' . $offset . ',' . $number; |
| 3497 | | |
| 3498 | | $pages = $wpdb->get_results($query); |
| 3499 | | |