4424 | | * Retrieve a list of pages. |
4425 | | * |
4426 | | * @global wpdb $wpdb WordPress database abstraction object. |
4427 | | * |
4428 | | * @since 1.5.0 |
4429 | | * |
4430 | | * @param array|string $args { |
4431 | | * Optional. Array or string of arguments to retrieve pages. |
4432 | | * |
4433 | | * @type int $child_of Page ID to return child and grandchild pages of. |
4434 | | * Default 0, or no restriction. |
4435 | | * @type string $sort_order How to sort retrieved pages. Accepts 'ASC', 'DESC'. Default 'ASC'. |
4436 | | * @type string $sort_column What columns to sort pages by, comma-separated. Accepts 'post_author', |
4437 | | * 'post_date', 'post_title', 'post_name', 'post_modified', 'menu_order', |
4438 | | * 'post_modified_gmt', 'post_parent', 'ID', 'rand', 'comment_count'. |
4439 | | * 'post_' can be omitted for any values that start with it. |
4440 | | * Default 'post_title'. |
4441 | | * @type bool $hierarchical Whether to return pages hierarchically. Default true. |
4442 | | * @type array $exclude Array of page IDs to exclude. Default empty array. |
4443 | | * @type array $include Array of page IDs to include. Cannot be used with `$child_of`, |
4444 | | * `$parent`, `$exclude`, `$meta_key`, `$meta_value`, or `$hierarchical`. |
4445 | | * Default empty array. |
4446 | | * @type string $meta_key Only include pages with this meta key. Default empty. |
4447 | | * @type string $meta_value Only include pages with this meta value. Requires `$meta_key`. |
4448 | | * Default empty. |
4449 | | * @type string $authors A comma-separated list of author IDs. Default empty. |
4450 | | * @type int $parent Page ID to return direct children of. `$hierarchical` must be false. |
4451 | | * Default -1, or no restriction. |
4452 | | * @type string|array $exclude_tree Comma-separated string or array of page IDs to exclude. |
4453 | | * Default empty array. |
4454 | | * @type int $number The number of pages to return. Default 0, or all pages. |
4455 | | * @type int $offset The number of pages to skip before returning. Requires `$number`. |
4456 | | * Default 0. |
4457 | | * @type string $post_type The post type to query. Default 'page'. |
4458 | | * @type string $post_status A comma-separated list of post status types to include. |
4459 | | * Default 'publish'. |
4460 | | * } |
4461 | | * @return array List of pages matching defaults or `$args`. |
| 4424 | * Force input to be an array if not already |
| 4425 | * @param mixed $input input variable to be coerced into array (not designed as a universal conversion, just the standard WP input strings) |
| 4426 | * @return array an array, even if invalid input given |
4466 | | $defaults = array( |
| 4436 | /** |
| 4437 | * check if post stati are all valid |
| 4438 | * @param array $stati an array of post stati to verify against all post stati |
| 4439 | * @return bool true if stati is an array and it matches elements within all post stati |
| 4440 | */ |
| 4441 | function post_stati_valid( $stati ) { |
| 4442 | // Make sure we have a valid post status. |
| 4443 | |
| 4444 | // first lets deal with strings that can become arrays |
| 4445 | $stati = wp_coerce_to_array( $stati ); |
| 4446 | |
| 4447 | if ( !array_diff( $stati, get_post_stati() ) && !empty($stati) ) { |
| 4448 | return true; |
| 4449 | } |
| 4450 | return false; |
| 4451 | } |
| 4452 | |
| 4453 | /** |
| 4454 | * provide default options for get_pages to try to make function more managable and process-descriptable |
| 4455 | */ |
| 4456 | function get_pages_defaults() { |
| 4457 | return array( |
4476 | | $r = wp_parse_args( $args, $defaults ); |
| 4468 | /** |
| 4469 | * provides parsing of $args input for get_pages |
| 4470 | * @param string|array &$args |
| 4471 | */ |
| 4472 | function get_pages_process_args( &$args ) { |
| 4473 | $args = wp_parse_args( $args, get_pages_defaults() ); |
| 4474 | $args['number'] = intval($args['number']); |
| 4475 | $args['offset'] = intval($args['offset']); |
| 4476 | $args['child_of'] = intval($args['child_of']); |
| 4477 | } |
4478 | | $number = (int) $r['number']; |
4479 | | $offset = (int) $r['offset']; |
4480 | | $child_of = (int) $r['child_of']; |
4481 | | $hierarchical = $r['hierarchical']; |
4482 | | $exclude = $r['exclude']; |
4483 | | $meta_key = $r['meta_key']; |
4484 | | $meta_value = $r['meta_value']; |
4485 | | $parent = $r['parent']; |
4486 | | $post_status = $r['post_status']; |
4487 | | |
4488 | | // Make sure the post type is hierarchical. |
4489 | | $hierarchical_post_types = get_post_types( array( 'hierarchical' => true ) ); |
4490 | | if ( ! in_array( $r['post_type'], $hierarchical_post_types ) ) { |
4491 | | return false; |
4492 | | } |
4493 | | |
4494 | | if ( $parent > 0 && ! $child_of ) { |
4495 | | $hierarchical = false; |
4496 | | } |
4497 | | |
4498 | | // Make sure we have a valid post status. |
4499 | | if ( ! is_array( $post_status ) ) { |
4500 | | $post_status = explode( ',', $post_status ); |
4501 | | } |
4502 | | if ( array_diff( $post_status, get_post_stati() ) ) { |
4503 | | return false; |
4504 | | } |
4505 | | |
4506 | | // $args can be whatever, only use the args defined in defaults to compute the key. |
4507 | | $key = md5( serialize( wp_array_slice_assoc( $r, array_keys( $defaults ) ) ) ); |
| 4479 | /** |
| 4480 | * provides boxed caching for get_pages |
| 4481 | * @param array|string $args the arguments for get_pages, used to build the cache hash key |
| 4482 | */ |
| 4483 | function get_pages_cached( $args ) { |
| 4484 | global $cache_key; |
| 4485 | |
| 4486 | $key = md5( serialize( wp_array_slice_assoc( $args, array_keys( get_pages_defaults() ) ) ) ); |
| 4504 | /** |
| 4505 | * provides updating of cache input for get_pages based upon cache_key global hash key |
| 4506 | * @param array &$args arguments (in parsed form), to get_pages as value |
| 4507 | * @param array &$posts posts returned from get_pages as reference |
| 4508 | */ |
| 4509 | function get_pages_update_cache( $args, &$posts ) { |
| 4510 | global $cache_key; |
| 4511 | // Sanitize before caching so it'll only get done once. |
| 4512 | $num_pages = count( $pages ); |
| 4513 | for ( $i = 0; $i < $num_pages; $i++ ) { |
| 4514 | $pages[ $i ] = sanitize_post( $pages[ $i ], 'raw' ); |
| 4515 | } |
| 4516 | |
| 4517 | // Update cache. |
| 4518 | update_post_cache( $pages ); |
| 4519 | |
| 4520 | if ( $args[ 'child_of' ] || $args[ 'heirarchical' ] ) { |
| 4521 | $pages = get_page_children( $args[ 'child_of' ], $pages ); |
| 4522 | } |
| 4523 | |
| 4524 | if ( ! empty( $args[ 'exclude_tree' ] ) ) { |
| 4525 | $args[ 'exclude' ] = wp_parse_id_list( $args[ 'exclude_tree' ] ); |
| 4526 | foreach( $args[ 'exclude' ] as $id ) { |
| 4527 | $children = get_page_children( $id, $pages ); |
| 4528 | foreach ( $children as $child ) { |
| 4529 | $args[ 'exclude' ][] = $child->ID; |
| 4530 | } |
| 4531 | } |
| 4532 | |
| 4533 | $num_pages = count( $pages ); |
| 4534 | for ( $i = 0; $i < $num_pages; $i++ ) { |
| 4535 | if ( in_array( $pages[ $i ]->ID, $args[ 'exclude' ] ) ) { |
| 4536 | unset( $pages[ $i ] ); |
| 4537 | } |
| 4538 | } |
| 4539 | } |
| 4540 | |
| 4541 | $page_structure = array(); |
| 4542 | foreach ( $pages as $page ) { |
| 4543 | $page_structure[] = $page->ID; |
| 4544 | } |
| 4545 | |
| 4546 | wp_cache_set( $cache_key, $page_structure, 'posts' ); |
| 4547 | |
| 4548 | // Convert to WP_Post instances |
| 4549 | $pages = array_map( 'get_post', $pages ); |
| 4550 | } |
| 4551 | |
| 4552 | /** |
| 4553 | * provides / generates includes string for get_pages |
| 4554 | * @param array &$args arguments (in parsed form), to get_pages as reference |
| 4555 | * @return string a string of SQL / WP_SQL compatible with querying the WP DB |
| 4556 | */ |
| 4557 | function build_get_pages_includes( &$args ) { |
4524 | | if ( ! empty( $r['include'] ) ) { |
4525 | | $child_of = 0; //ignore child_of, parent, exclude, meta_key, and meta_value params if using include |
4526 | | $parent = -1; |
4527 | | $exclude = ''; |
4528 | | $meta_key = ''; |
4529 | | $meta_value = ''; |
4530 | | $hierarchical = false; |
4531 | | $incpages = wp_parse_id_list( $r['include'] ); |
| 4559 | if ( ! empty( $args[ 'include' ] ) ) { |
| 4560 | $args[ 'child_of' ] = 0; //ignore child_of, parent, exclude, meta_key, and meta_value params if using include |
| 4561 | $args[ 'parent' ] = -1; |
| 4562 | $args[ 'exclude' ] = ''; |
| 4563 | $args[ 'meta_key' ] = ''; |
| 4564 | $args[ 'meta_value' ] = ''; |
| 4565 | $args[ 'heirarchical' ] = false; |
| 4566 | $incpages = wp_parse_id_list( $args[ 'include' ] ); |
4581 | | $meta_key = wp_unslash($meta_key); |
4582 | | $meta_value = wp_unslash($meta_value); |
4583 | | if ( '' !== $meta_key ) { |
4584 | | $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s", $meta_key); |
| 4646 | $args['meta_key'] = wp_unslash($args['meta_key']); |
| 4647 | $args['meta_value'] = wp_unslash($args['meta_value']); |
| 4648 | if ( '' !== $args['meta_key'] ) { |
| 4649 | $where .= $wpdb->prepare(" AND $wpdb->postmeta.meta_key = %s", $args['meta_key']); |
4659 | | if ( empty($pages) ) { |
4660 | | /** This filter is documented in wp-includes/post.php */ |
4661 | | $pages = apply_filters( 'get_pages', array(), $r ); |
4662 | | return $pages; |
4663 | | } |
| 4725 | /** |
| 4726 | * Retrieve a list of pages. |
| 4727 | * |
| 4728 | * @global wpdb $wpdb WordPress database abstraction object. |
| 4729 | * |
| 4730 | * @since 1.5.0 |
| 4731 | * |
| 4732 | * @param array|string $args { |
| 4733 | * Optional. Array or string of arguments to retrieve pages. |
| 4734 | * |
| 4735 | * @type int $child_of Page ID to return child and grandchild pages of. |
| 4736 | * Default 0, or no restriction. |
| 4737 | * @type string $sort_order How to sort retrieved pages. Accepts 'ASC', 'DESC'. Default 'ASC'. |
| 4738 | * @type string $sort_column What columns to sort pages by, comma-separated. Accepts 'post_author', |
| 4739 | * 'post_date', 'post_title', 'post_name', 'post_modified', 'menu_order', |
| 4740 | * 'post_modified_gmt', 'post_parent', 'ID', 'rand', 'comment_count'. |
| 4741 | * 'post_' can be omitted for any values that start with it. |
| 4742 | * Default 'post_title'. |
| 4743 | * @type bool $hierarchical Whether to return pages hierarchically. Default true. |
| 4744 | * @type array $exclude Array of page IDs to exclude. Default empty array. |
| 4745 | * @type array $include Array of page IDs to include. Cannot be used with `$child_of`, |
| 4746 | * `$parent`, `$exclude`, `$meta_key`, `$meta_value`, or `$hierarchical`. |
| 4747 | * Default empty array. |
| 4748 | * @type string $meta_key Only include pages with this meta key. Default empty. |
| 4749 | * @type string $meta_value Only include pages with this meta value. Requires `$meta_key`. |
| 4750 | * Default empty. |
| 4751 | * @type string $authors A comma-separated list of author IDs. Default empty. |
| 4752 | * @type int $parent Page ID to return direct children of. `$hierarchical` must be false. |
| 4753 | * Default -1, or no restriction. |
| 4754 | * @type string|array $exclude_tree Comma-separated string or array of page IDs to exclude. |
| 4755 | * Default empty array. |
| 4756 | * @type int $number The number of pages to return. Default 0, or all pages. |
| 4757 | * @type int $offset The number of pages to skip before returning. Requires `$number`. |
| 4758 | * Default 0. |
| 4759 | * @type string $post_type The post type to query. Default 'page'. |
| 4760 | * @type string $post_status A comma-separated list of post status types to include. |
| 4761 | * Default 'publish'. |
| 4762 | * } |
| 4763 | * @return array List of pages matching defaults or `$args`. |
| 4764 | */ |
| 4765 | function get_pages( $args = array() ) { |
4674 | | if ( $child_of || $hierarchical ) { |
4675 | | $pages = get_page_children($child_of, $pages); |
4676 | | } |
4677 | | |
4678 | | if ( ! empty( $r['exclude_tree'] ) ) { |
4679 | | $exclude = wp_parse_id_list( $r['exclude_tree'] ); |
4680 | | foreach( $exclude as $id ) { |
4681 | | $children = get_page_children( $id, $pages ); |
4682 | | foreach ( $children as $child ) { |
4683 | | $exclude[] = $child->ID; |
4684 | | } |
| 4778 | if ( $args[ 'parent' ] > 0 && ! $args[ 'child_of' ] ) { |
| 4779 | $args[ 'heirarchical' ] = false; |