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:

1 to 100 of 279
function get_pages( $args = array() ) {
  global $wpdb;

  $defaults = array(
    'child_of'     => 0,
    'sort_order'   => 'ASC',
    'sort_column'  => 'post_title',
    'hierarchical' => 1,
    'exclude'      => array(),
    'include'      => array(),
    'meta_key'     => '',
    'meta_value'   => '',
    'authors'      => '',
    'parent'       => -1,
    'exclude_tree' => array(),
    'number'       => '',
    'offset'       => 0,
    'post_type'    => 'page',
    'post_status'  => 'publish',
  );

  $parsed_args = wp_parse_args( $args, $defaults );

  $number       = (int) $parsed_args['number'];
  $offset       = (int) $parsed_args['offset'];
  $child_of     = (int) $parsed_args['child_of'];
  $hierarchical = $parsed_args['hierarchical'];
  $exclude      = $parsed_args['exclude'];
  $meta_key     = $parsed_args['meta_key'];
  $meta_value   = $parsed_args['meta_value'];
  $parent       = $parsed_args['parent'];
  $post_status  = $parsed_args['post_status'];

  // Make sure the post type is hierarchical.
  $hierarchical_post_types = get_post_types( array( 'hierarchical' => true ) );
  if ( ! in_array( $parsed_args['post_type'], $hierarchical_post_types, true ) ) {
    return false;
  }

  if ( $parent > 0 && ! $child_of ) {
    $hierarchical = false;
  }

  // Make sure we have a valid post status.
  if ( ! is_array( $post_status ) ) {
    $post_status = explode( ',', $post_status );
  }
  if ( array_diff( $post_status, get_post_stati() ) ) {
    return false;
  }

  // $args can be whatever, only use the args defined in defaults to compute the key.
  $key          = md5( serialize( wp_array_slice_assoc( $parsed_args, array_keys( $defaults ) ) ) );
  $last_changed = wp_cache_get_last_changed( 'posts' );

  $cache_key = "get_pages:$key:$last_changed";
  $cache     = wp_cache_get( $cache_key, 'posts' );
  if ( false !== $cache ) {
    _prime_post_caches( $cache, false, false );

    // Convert to WP_Post instances.
    $pages = array_map( 'get_post', $cache );
    
/** This filter is documented in wp-includes/post.php */
    $pages = apply_filters( 'get_pages', $pages, $parsed_args );

    return $pages;
  }

  $inclusions = '';
  if ( ! empty( $parsed_args['include'] ) ) {
    $child_of     = 0; // Ignore child_of, parent, exclude, meta_key, and meta_value params if using include.
    $parent       = -1;
    $exclude      = '';
    $meta_key     = '';
    $meta_value   = '';
    $hierarchical = false;
    $incpages     = wp_parse_id_list( $parsed_args['include'] );
    if ( ! empty( $incpages ) ) {
      $inclusions = ' AND ID IN (' . implode( ',', $incpages ) . ')';
    }
  }

  $exclusions = '';
  if ( ! empty( $exclude ) ) {
    $expages = wp_parse_id_list( $exclude );
    if ( ! empty( $expages ) ) {
      $exclusions = ' AND ID NOT IN (' . implode( ',', $expages ) . ')';
    }
  }

  $author_query = '';
  if ( ! empty( $parsed_args['authors'] ) ) {
    $post_authors = wp_parse_list( $parsed_args['authors'] );

    if ( ! empty( $post_authors ) ) {
      foreach ( $post_authors as $post_author ) {
        // Do we have an author id or an author login?
        if ( 0 == (int) $post_author ) {
          $post_author = get_user_by( 'login', $post_author );
          if ( empty( $post_author ) ) {
 

 View on GitHub View on Trac