WP_Query::get_posts() – Retrieves an array of posts based on query variables.

You appear to be a bot. Output may be restricted

Description

Retrieves an array of posts based on query variables.

There are a few filters and actions that can be used to modify the post database query.

Usage

$WP_Post[]|int[] = WP_Query::get_posts();

Parameters

Returns

WP_Post[]|int[] Array of post objects or post IDs.

Source

File name: wordpress/wp-includes/class-wp-query.php


Lines:

1 to 100 of 1741
  public function get_posts() {
    global $wpdb;

    $this->WP_Query::parse_query();

    
/**
 * Fires after the query variable object is created, but before the actual query is run.
 *
 * Note: If using conditional tags, use the method versions within the passed instance
 * (e.g. $this->is_main_query() instead of is_main_query()). This is because the functions
 * like is_main_query() test against the global $wp_query instance, not the passed one.
 *
 * @since 2.0.0
 *
 * @param WP_Query $query The WP_Query instance (passed by reference).
 */
    do_action_ref_array( 'pre_get_posts', array( &$this ) );

    // Shorthand.
    $q = &$this->query_vars;

    // Fill again in case 'pre_get_posts' unset some vars.
    $q = $this->WP_Query::fill_query_vars( $q );

    
/**
 * Filters whether an attachment query should include filenames or not.
 *
 * @since 6.0.3
 *
 * @param bool $allow_query_attachment_by_filename Whether or not to include filenames.
 */
    $this->allow_query_attachment_by_filename = apply_filters( 'wp_allow_query_attachment_by_filename', false );
    remove_all_filters( 'wp_allow_query_attachment_by_filename' );

    // Parse meta query.
    $this->meta_query = new WP_Meta_Query();
    $this->meta_query->WP_Query::parse_query_vars( $q );

    // Set a flag if a 'pre_get_posts' hook changed the query vars.
    $hash = md5( serialize( $this->query_vars ) );
    if ( $hash != $this->query_vars_hash ) {
      $this->query_vars_changed = true;
      $this->query_vars_hash    = $hash;
    }
    unset( $hash );

    // First let's clear some variables.
    $distinct         = '';
    $whichauthor      = '';
    $whichmimetype    = '';
    $where            = '';
    $limits           = '';
    $join             = '';
    $search           = '';
    $groupby          = '';
    $post_status_join = false;
    $page             = 1;

    if ( isset( $q['caller_get_posts'] ) ) {
      _deprecated_argument(
        'WP_Query',
        '3.1.0',
        sprintf(
          /* translators: 1: caller_get_posts, 2: ignore_sticky_posts */
          __( '%1$s is deprecated. Use %2$s instead.' ),
          '<code>caller_get_posts</code>',
          '<code>ignore_sticky_posts</code>'
        )
      );

      if ( ! isset( $q['ignore_sticky_posts'] ) ) {
        $q['ignore_sticky_posts'] = $q['caller_get_posts'];
      }
    }

    if ( ! isset( $q['ignore_sticky_posts'] ) ) {
      $q['ignore_sticky_posts'] = false;
    }

    if ( ! isset( $q['suppress_filters'] ) ) {
      $q['suppress_filters'] = false;
    }

    if ( ! isset( $q['cache_results'] ) ) {
      $q['cache_results'] = true;
    }

    if ( ! isset( $q['update_post_term_cache'] ) ) {
      $q['update_post_term_cache'] = true;
    }

    if ( ! isset( $q['update_menu_item_cache'] ) ) {
      $q['update_menu_item_cache'] = false;
    }

    if ( ! isset( $q['lazy_load_term_meta'] ) ) {
      $q['lazy_load_term_meta'] = $q['update_post_term_cache'];
    } elseif ( $q['lazy_load_term_meta'] ) { // Lazy loading term meta only works if term caches are primed.

 View on GitHub View on Trac