get_pages() – Retrieve an array of pages (or hierarchical post type items).

You appear to be a bot. Output may be restricted

Description

Retrieve an array of pages (or hierarchical post type items).

Usage

$WP_Post[]|int[]|false = get_pages( $args );

Parameters

$args
( array|string ) optional – { Optional. Array or string of arguments to retrieve pages.
$child_of
( int ) optional – Page ID to return child and grandchild pages of. Note: The value of $hierarchical has no bearing on whether $child_of returns hierarchical results. Default 0, or no restriction.
$sort_order
( string ) optional – How to sort retrieved pages. Accepts 'ASC', 'DESC'. Default 'ASC'.
$sort_column
( string ) optional – What columns to sort pages by, comma-separated. Accepts 'post_author', 'post_date', 'post_title', 'post_name', 'post_modified', 'menu_order', 'post_modified_gmt', 'post_parent', 'ID', 'rand', 'comment_count'. 'post_' can be omitted for any values that start with it. Default 'post_title'.
$hierarchical
( bool ) optional – Whether to return pages hierarchically. If false in conjunction with $child_of also being false, both arguments will be disregarded. Default true.
$exclude
( int[] ) optional – Array of page IDs to exclude. Default empty array.
$include
( int[] ) optional – Array of page IDs to include. Cannot be used with `$child_of`, `$parent`, `$exclude`, `$meta_key`, `$meta_value`, or `$hierarchical`. Default empty array.
$meta_key
( string ) optional – Only include pages with this meta key. Default empty.
$meta_value
( string ) optional – Only include pages with this meta value. Requires `$meta_key`. Default empty.
$authors
( string ) optional – A comma-separated list of author IDs. Default empty.
$parent
( int ) optional – Page ID to return direct children of. Default -1, or no restriction.
$exclude_tree
( string|int[] ) optional – Comma-separated string or array of page IDs to exclude. Default empty array.
$number
( int ) optional – The number of pages to return. Default 0, or all pages.
$offset
( int ) optional – The number of pages to skip before returning. Requires `$number`. Default 0.
$post_type
( string ) optional – The post type to query. Default 'page'.
$post_status
( string|array ) optional – A comma-separated list or array of post statuses to include. Default 'publish'. }

Returns

WP_Post[]|int[]|false Array of pages (or hierarchical post type items). Boolean false if the specified post type is not hierarchical or the specified status is not supported by the post type.

Source

File name: wordpress/wp-includes/post.php
Lines:

201 to 279 of 279
    $orderby_array[] = $orderby;

  }
  $sort_column = ! empty( $orderby_array ) ? implode( ',', $orderby_array ) : "$wpdb->posts.post_title";

  $sort_order = strtoupper( $parsed_args['sort_order'] );
  if ( '' !== $sort_order && ! in_array( $sort_order, array( 'ASC', 'DESC' ), true ) ) {
    $sort_order = 'ASC';
  }

  $query  = "SELECT * FROM $wpdb->posts $join WHERE ($where_post_type) $where ";
  $query .= $author_query;
  $query .= ' ORDER BY ' . $sort_column . ' ' . $sort_order;

  if ( ! empty( $number ) ) {
    $query .= ' LIMIT ' . $offset . ',' . $number;
  }

  $pages = $wpdb->get_results( $query );

  if ( empty( $pages ) ) {
    wp_cache_set( $cache_key, array(), 'posts' );

    
/** This filter is documented in wp-includes/post.php */
    $pages = apply_filters( 'get_pages', array(), $parsed_args );

    return $pages;
  }

  // Sanitize before caching so it'll only get done once.
  $num_pages = count( $pages );
  for ( $i = 0; $i < $num_pages; $i++ ) {
    $pages[ $i ] = sanitize_post( $pages[ $i ], 'raw' );
  }

  // Update cache.
  update_post_cache( $pages );

  if ( $child_of || $hierarchical ) {
    $pages = get_page_children( $child_of, $pages );
  }

  if ( ! empty( $parsed_args['exclude_tree'] ) ) {
    $exclude = wp_parse_id_list( $parsed_args['exclude_tree'] );
    foreach ( $exclude as $id ) {
      $children = get_page_children( $id, $pages );
      foreach ( $children as $child ) {
        $exclude[] = $child->ID;
      }
    }

    $num_pages = count( $pages );
    for ( $i = 0; $i < $num_pages; $i++ ) {
      if ( in_array( $pages[ $i ]->ID, $exclude, true ) ) {
        unset( $pages[ $i ] );
      }
    }
  }

  $page_structure = array();
  foreach ( $pages as $page ) {
    $page_structure[] = $page->ID;
  }

  wp_cache_set( $cache_key, $page_structure, 'posts' );

  // Convert to WP_Post instances.
  $pages = array_map( 'get_post', $pages );

  
/**
 * Filters the retrieved list of pages.
 *
 * @since 2.1.0
 *
 * @param WP_Post[] $pages       Array of page objects.
 * @param array     $parsed_args Array of get_pages() arguments.
 */
  return apply_filters( 'get_pages', $pages, $parsed_args );
}
 

 View on GitHub View on Trac