Tribe__Repository::build_query( $use_query_builder = true )

{@inheritdoc}


Source

File: src/Tribe/Repository.php

	public function build_query() {
		/**
		 * Allow classes extending or decorating the repository to act before
		 * the query is built or replace its building completely.
		 */
		if ( null !== $this->query_builder ) {
			$built = $this->query_builder->build_query();

			if ( null !== $built ) {
				return $built;
			}
		}

		$query = new WP_Query();

		$this->filter_query->set_query( $query );

		/**
		 * Here we merge, not recursively, to allow user-set query arguments
		 * to override the default ones.
		 */
		$query_args = array_merge( $this->default_args, $this->query_args );

		$default_post_status       = current_user_can( 'read_private_posts' ) ? 'any' : '';
		$query_args['post_status'] = Tribe__Utils__Array::get( $query_args, 'post_status', $default_post_status );

		/**
		 * Filters the query arguments that will be used to fetch the posts.
		 *
		 * @param array    $query_args An array of the query arguments the query will be
		 *                             initialized with.
		 * @param WP_Query $query      The query object, the query arguments have not been parsed yet.
		 * @param          $this       $this This repository instance
		 */
		$query_args = apply_filters( "{$this->filter_name}_query_args", $query_args, $query, $this );

		if ( isset( $query_args['offset'] ) ) {
			$offset   = absint( $query_args['offset'] );
			$per_page = (int) Tribe__Utils__Array::get( $query_args, 'posts_per_page', get_option( 'posts_per_page' ) );
			$page     = (int) Tribe__Utils__Array::get( $query_args, 'paged', 1 );

			$real_offset                  = $per_page === - 1 ? $offset : ( $per_page * ( $page - 1 ) ) + $offset;
			$query_args['offset']         = $real_offset;
			$query_args['posts_per_page'] = $per_page === - 1 ? 99999999999 : $per_page;

			/**
			 * Unset the `offset` query argument to avoid applying it multiple times when this method
			 * is used, on the same repository, more than once.
			 */
			unset( $this->query_args['offset'] );
		}

		foreach ( $query_args as $key => $value ) {
			$query->set( $key, $value );
		}

		/**
		 * Here process the previously set query modifiers passing them the
		 * query object before it executes.
		 * The query modifiers should modify the query by reference.
		 */
		foreach ( $this->query_modifiers as $arg ) {
			if ( is_object( $arg ) ) {
				// __invoke, assume changes are made by reference
				$arg( $query );
			} elseif ( is_callable( $arg ) ) {
				// assume changes are made by reference
				$arg( $query );
			}
		}

		return $query;
	}