• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
WooCommerce a2z

WooCommerce a2z

WooCommerce

  • Home
  • Plugins
  • Blocks
  • Shortcodes
  • APIs
  • Classes
  • Files
  • Hooks
  • Sitemap
  • Blog
Home / APIs / WC_Query::product_query() – Query the products, applying sorting/ordering etc.

You appear to be a bot. Output may be restricted

Description

Query the products, applying sorting/ordering etc.

This applies to the main WordPress loop.

Usage

WC_Query::product_query( $q );

Parameters

$q
( WP_Query ) required – Query instance.

Returns

void

Source

File name: woocommerce/includes/class-wc-query.php


Lines:

1 to 30 of 30
  public function product_query( $q ) {
    if ( ! is_feed() ) {
      $ordering = $this->get_catalog_ordering_args();
      $q->set( 'orderby', $ordering['orderby'] );
      $q->set( 'order', $ordering['order'] );

      if ( isset( $ordering['meta_key'] ) ) {
        $q->set( 'meta_key', $ordering['meta_key'] );
      }
    }

    // Query vars that affect posts shown.
    $q->set( 'meta_query', $this->get_meta_query( $q->get( 'meta_query' ), true ) );
    $q->set( 'tax_query', $this->get_tax_query( $q->get( 'tax_query' ), true ) );
    $q->set( 'wc_query', 'product_query' );
    $q->set( 'post__in', array_unique( (array) apply_filters( 'loop_shop_post_in', array() ) ) );

    // Work out how many products to query.
    $q->set( 'posts_per_page', $q->get( 'posts_per_page' ) ? $q->get( 'posts_per_page' ) : apply_filters( 'loop_shop_per_page', wc_get_default_products_per_row() * wc_get_default_product_rows_per_page() ) );

    // Store reference to this query.
    self::$product_query = $q;

    // Additonal hooks to change WP Query.
    add_filter( 'posts_clauses', array( $this, 'price_filter_post_clauses' ), 10, 2 );
    add_filter( 'the_posts', array( $this, 'handle_get_posts' ), 10, 2 );

    do_action( 'woocommerce_product_query', $q, $this );
  }
 

 View on GitHub View on Trac

Published: 26th November 2019 | Last updated: 10th December 2020

Primary Sidebar

Information

Function name: WC_Query::product_query
Class ref: WC_Query
Plugin ref: WooCommerce
Version: 4.8.0
Sourcefile: includes/class-wc-query.php
File ref: includes/class-wc-query.php
Deprecated?: No
API Letters: P,Q,W

Footer

WooCommerce a2z
WooCommerce a2z
WooCommerce
WordPress 5.6
WordPress a2z
WordPress core a2z
Genesis Theme Framework a2z
Jetpack a2z
WordPress develop tests
Easy Digital Downloads a2z
WooCommerce a2z
Yoast SEO a2z
WordPress Blocks

Site:  woocommerce.wp-a2z.org
© Copyright WooCommerce a2z 2014-2021. All rights reserved.


Website designed and developed by Herb Miller
Proudly powered by WordPress and oik plugins

  • Home
  • Blog
  • Sitemap
  • Sites