WC_Query::pre_get_posts PHP Method

pre_get_posts() public method

Hook into pre_get_posts to do the main product query.
public pre_get_posts ( mixed $q )
$q mixed query object
    public function pre_get_posts($q)
    {
        // We only want to affect the main query
        if (!$q->is_main_query()) {
            return;
        }
        // Fix for endpoints on the homepage
        if ($this->is_showing_page_on_front($q) && !$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->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. Get around that here.
        if ($this->is_showing_page_on_front($q) && $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->is_page = true;
                $q->is_home = false;
                $q->set('page_id', (int) get_option('page_on_front'));
                $q->set('post_type', 'product');
            }
        }
        // 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 archive on front
        if ($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
            define('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'));
            }
            // Only apply to product categories, the product post archive, the shop page, product tags, and product attribute taxonomies
        } elseif (!$q->is_post_type_archive('product') && !$q->is_tax(get_object_taxonomies('product'))) {
            return;
        }
        $this->product_query($q);
        if (is_search()) {
            add_filter('posts_where', array($this, 'search_post_excerpt'));
            add_filter('wp', array($this, 'remove_posts_where'));
        }
        // And remove the pre_get_posts hook
        $this->remove_product_query();
    }