WC_Shortcode_Products::get_query_results()protectedWC 3.3.0

Run the query and return an array of data, including queried ids and pagination information.

Method of the class: WC_Shortcode_Products{}

Return

Object. Object with the following props; ids, per_page, found_posts, max_num_pages, current_page

Usage

// protected - for code of main (parent) or child class
$result = $this->get_query_results();

Changelog

Since 3.3.0 Introduced.

WC_Shortcode_Products::get_query_results() code WC 9.4.2

protected function get_query_results() {
	$transient_name    = $this->get_transient_name();
	$transient_version = WC_Cache_Helper::get_transient_version( 'product_query' );
	$cache             = wc_string_to_bool( $this->attributes['cache'] ) === true;
	$transient_value   = $cache ? get_transient( $transient_name ) : false;

	if ( isset( $transient_value['value'], $transient_value['version'] ) && $transient_value['version'] === $transient_version ) {
		$results = $transient_value['value'];
	} else {
		$query = new WP_Query( $this->query_args );

		$paginated = ! $query->get( 'no_found_rows' );

		$results = (object) array(
			'ids'          => wp_parse_id_list( $query->posts ),
			'total'        => $paginated ? (int) $query->found_posts : count( $query->posts ),
			'total_pages'  => $paginated ? (int) $query->max_num_pages : 1,
			'per_page'     => (int) $query->get( 'posts_per_page' ),
			'current_page' => $paginated ? (int) max( 1, $query->get( 'paged', 1 ) ) : 1,
		);

		if ( $cache ) {
			$transient_value = array(
				'version' => $transient_version,
				'value'   => $results,
			);
			set_transient( $transient_name, $transient_value, DAY_IN_SECONDS * 30 );
		}
	}

	// Remove ordering query arguments which may have been added by get_catalog_ordering_args.
	WC()->query->remove_ordering_args();

	/**
	 * Filter shortcode products query results.
	 *
	 * @since 4.0.0
	 * @param stdClass $results Query results.
	 * @param WC_Shortcode_Products $this WC_Shortcode_Products instance.
	 */
	return apply_filters( 'woocommerce_shortcode_products_query_results', $results, $this );
}