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:

101 to 200 of 279
            continue;
          }
          if ( empty( $post_author->ID ) ) {
            continue;
          }
          $post_author = $post_author->ID;
        }

        if ( '' === $author_query ) {
          $author_query = $wpdb->prepare( ' post_author = %d ', $post_author );
        } else {
          $author_query .= $wpdb->prepare( ' OR post_author = %d ', $post_author );
        }
      }
      if ( '' !== $author_query ) {
        $author_query = " AND ($author_query)";
      }
    }
  }

  $join  = '';
  $where = "$exclusions $inclusions ";
  if ( '' !== $meta_key || '' !== $meta_value ) {
    $join = " LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )";

    // meta_key and meta_value might be slashed.
    $meta_key   = wp_unslash( $meta_key );
    $meta_value = wp_unslash( $meta_value );
    if ( '' !== $meta_key ) {
      $where .= $wpdb->prepare( " AND $wpdb->postmeta.meta_key = %s", $meta_key );
    }
    if ( '' !== $meta_value ) {
      $where .= $wpdb->prepare( " AND $wpdb->postmeta.meta_value = %s", $meta_value );
    }
  }

  if ( is_array( $parent ) ) {
    $post_parent__in = implode( ',', array_map( 'absint', (array) $parent ) );
    if ( ! empty( $post_parent__in ) ) {
      $where .= " AND post_parent IN ($post_parent__in)";
    }
  } elseif ( $parent >= 0 ) {
    $where .= $wpdb->prepare( ' AND post_parent = %d ', $parent );
  }

  if ( 1 === count( $post_status ) ) {
    $where_post_type = $wpdb->prepare( 'post_type = %s AND post_status = %s', $parsed_args['post_type'], reset( $post_status ) );
  } else {
    $post_status     = implode( "', '", $post_status );
    $where_post_type = $wpdb->prepare( "post_type = %s AND post_status IN ('$post_status')", $parsed_args['post_type'] );
  }

  $orderby_array = array();
  $allowed_keys  = array(
    'author',
    'post_author',
    'date',
    'post_date',
    'title',
    'post_title',
    'name',
    'post_name',
    'modified',
    'post_modified',
    'modified_gmt',
    'post_modified_gmt',
    'menu_order',
    'parent',
    'post_parent',
    'ID',
    'rand',
    'comment_count',
  );

  foreach ( explode( ',', $parsed_args['sort_column'] ) as $orderby ) {
    $orderby = trim( $orderby );
    if ( ! in_array( $orderby, $allowed_keys, true ) ) {
      continue;
    }

    switch ( $orderby ) {
      case 'menu_order':
        break;
      case 'ID':
        $orderby = "$wpdb->posts.ID";
        break;
      case 'rand':
        $orderby = 'RAND()';
        break;
      case 'comment_count':
        $orderby = "$wpdb->posts.comment_count";
        break;
      default:
        if ( 0 === strpos( $orderby, 'post_' ) ) {
          $orderby = "$wpdb->posts." . $orderby;
        } else {
          $orderby = "$wpdb->posts.post_" . $orderby;
        }
    }

 

 View on GitHub View on Trac