• 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::pre_get_posts() – Hook into pre_get_posts to do the main product query.

You appear to be a bot. Output may be restricted

Description

Hook into pre_get_posts to do the main product query.

Usage

WC_Query::pre_get_posts( $q );

Parameters

$q
( WP_Query ) required – Query instance.

Returns

void

Source

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

1 to 95 of 95
  public function pre_get_posts( $q ) {
    // We only want to affect the main query.
    if ( ! $q->is_main_query() ) {
      return;
    }

    // Fixes for queries on static homepages.
    if ( $this->is_showing_page_on_front( $q ) ) {

      // Fix for endpoints on the homepage.
      if ( ! $this->page_on_front_is( $q->get( 'page_id' ) ) ) {
        $_query = wp_parse_args( $q->query );
        if ( ! empty( $_query ) && array_intersect( array_keys( $_query ), array_keys( $this->get_query_vars() ) ) ) {
          $q->is_page     = true;
          $q->is_home     = false;
          $q->is_singular = true;
          $q->set( 'page_id', (int) get_option( 'page_on_front' ) );
          add_filter( 'redirect_canonical', '__return_false' )  <;
        }
      }

      // When orderby is set, WordPress shows posts on the front-page. Get around that here.
      if ( $this->page_on_front_is( wc_get_page_id( 'shop' ) ) ) {
        $_query = wp_parse_args( $q->query );
        if ( empty( $_query ) || ! array_diff( array_keys( $_query ), array( 'preview', 'page', 'paged', 'cpage', 'orderby' ) ) ) {
          $q->set( 'page_id', (int) get_option( 'page_on_front' ) );
          $q->is_page = true;
          $q->is_home = false;

          // WP supporting themes show post type archive.
          if ( current_theme_supports( 'woocommerce' ) ) {
            $q->set( 'post_type', 'product' );
          } else {
            $q->is_singular = true;
          }
        }
      } elseif ( ! empty( $_GET['orderby'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
        $q->set( 'page_id', (int) get_option( 'page_on_front' ) );
        $q->is_page     = true;
        $q->is_home     = false;
        $q->is_singular = true;
      }
    }

    // Fix product feeds.
    if ( $q->is_feed() && $q->is_post_type_archive( 'product' ) ) {
      $q->is_comment_feed = false;
    }

    // Special check for shops with the PRODUCT POST TYPE ARCHIVE on front.
    if ( current_theme_supports( 'woocommerce' ) && $q->is_page() && 'page' === get_option( 'show_on_front' ) && absint( $q->get( 'page_id' ) ) === wc_get_page_id( 'shop' ) ) {
      // This is a front-page shop.
      $q->set( 'post_type', 'product' );
      $q->set( 'page_id', '' );

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

      // Define a variable so we know this is the front page shop later on.
      wc_maybe_define_constant( 'SHOP_IS_ON_FRONT', true );

      // Get the actual WP page to avoid errors and let us use is_front_page().
      // This is hacky but works. Awaiting https://core.trac.wordpress.org/ticket/21096.
      global $wp_post_types;

      $shop_page = get_post( wc_get_page_id( 'shop' ) );

      $wp_post_types['product']->ID         = $shop_page->ID;
      $wp_post_types['product']->post_title = $shop_page->post_title;
      $wp_post_types['product']->post_name  = $shop_page->post_name;
      $wp_post_types['product']->post_type  = $shop_page->post_type;
      $wp_post_types['product']->ancestors  = get_ancestors( $shop_page->ID, $shop_page->post_type );

      // Fix conditional Functions like is_front_page.
      $q->is_singular          = false;
      $q->is_post_type_archive = true;
      $q->is_archive           = true;
      $q->is_page              = true;

      // Remove post type archive name from front page title tag.
      add_filter( 'post_type_archive_title', '__return_empty_string',  < 5 );

      // Fix WP SEO.
      if ( class_exists( 'WPSEO_Meta' ) ) {
        add_filter( 'wpseo_metadesc', array( $this, 'wpseo_metadesc' ) );
        add_filter( 'wpseo_metakey', array( $this, 'wpseo_metakey' ) );
      }
    } elseif ( ! $q->is_post_type_archive( 'product' ) && ! $q->is_tax( get_object_taxonomies( 'product' ) ) ) {
      // Only apply to product categories, the product post archive, the shop page, product tags, and product attribute taxonomies.
      return;
    }

    $this->product_query( $q );
  }
 

 View on GitHub View on Trac

Published: 26th November 2019 | Last updated: 6th April 2020

Primary Sidebar

Information

Function name: WC_Query::pre_get_posts
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: G,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