skip to Main Content

I am calling woocommerce_product_query but is not working in Divi theme.
I checked and it works with other built-in wordpress themes.

I am using Query Monitor and I can see that its fired correctly but still I get all products and not the one I hardcoded in post__in.

Here is my code:

add_action( 'woocommerce_product_query', 'my_pre_get_posts_query', 9999 );
function my_pre_get_posts_query( $query ) {
    
    $meta_query = $query->get( 'meta_query' );

    
    $query->set( 'post__in', ['245374','245372'] );
}

Is there any known interference with Divi? What can possible go wrong? I checked both with parent and child Divi theme.

Divi Theme code:

/**
     * Filter the products query arguments.
     *
     * @since 4.0.5
     *
     * @param array $query_args Query array.
     *
     * @return array
     */
    public function filter_products_query( $query_args ) {
        if ( is_search() ) {
            $query_args['s'] = get_search_query();
        }

        if ( function_exists( 'WC' ) ) {
            $query_args['meta_query'] = WC()->query->get_meta_query( et_()->array_get( $query_args, 'meta_query', array() ), true );
            $query_args['tax_query']  = WC()->query->get_tax_query( et_()->array_get( $query_args, 'tax_query', array() ), true );

            // Add fake cache-busting argument as the filtering is actually done in self::apply_woo_widget_filters().
            $query_args['nocache'] = microtime( true );
        }

        return $query_args;
    }

Or this part in the get_shop public function of the theme:

global $wp_the_query;

        $query_backup = $wp_the_query;

        $is_offset_valid = absint( $offset_number ) > 0;
        if ( $is_offset_valid ) {
            self::$offset = $offset_number;

            add_filter(
                'woocommerce_shortcode_products_query',
                // phpcs:ignore WordPress.Arrays.CommaAfterArrayItem.NoComma -- This is a function call.
                array( 'ET_Builder_Module_Shop', 'append_offset' )
            );

            add_filter(
                'woocommerce_shortcode_products_query_results',
                array( 'ET_Builder_Module_Shop', 'adjust_offset_pagination' )
            );
        }

        if ( 'product_category' === $type || $use_current_loop ) {
            add_filter( 'woocommerce_shortcode_products_query', array( $this, 'filter_products_query' ) );
            add_action( 'pre_get_posts', array( $this, 'apply_woo_widget_filters' ), 10 );
        }

        if ( $use_current_loop ) {
            add_filter( 'woocommerce_shortcode_products_query', array( $this, 'filter_vendors_products_query' ) );
        }

        $shop = do_shortcode( $shortcode );

        if ( $is_offset_valid ) {
            remove_filter(
                'woocommerce_shortcode_products_query',
                array( 'ET_Builder_Module_Shop', 'append_offset' )
            );

            remove_filter(
                'woocommerce_shortcode_products_query_results',
                array( 'ET_Builder_Module_Shop', 'adjust_offset_pagination' )
            );

            self::$offset = 0;
        }

        remove_filter( 'woocommerce_default_catalog_orderby', array( $this, 'set_default_orderby' ) );

        if ( $use_current_loop ) {
            remove_filter( 'woocommerce_shortcode_products_query', array( $this, 'filter_vendors_products_query' ) );
        }

        if ( 'product_category' === $type || $use_current_loop ) {
            remove_action( 'pre_get_posts', array( $this, 'apply_woo_widget_filters' ), 10 );
            remove_filter( 'woocommerce_shortcode_products_query', array( $this, 'filter_products_query' ) );
        }

        $wp_the_query = $query_backup;

2

Answers


  1. I dont have Divi to test it but i know that theme have own settings for queries.

    Try this solution that i found

    add_action( 'woocommerce_product_query', 'my_pre_get_posts_query' );
    function my_pre_get_posts_query( $q ) {
        if ( $q->is_main_query() && ( $q->get( 'wc_query' ) === 'product_query' ) ) {
             $query->set( 'post__in', ['245374','245372'] );
        }
    }
    
    Login or Signup to reply.
  2. Try this , it may help you.

    <?php 
    add_action( 'woocommerce_product_query', 'my_pre_get_posts_query' );
    function my_pre_get_posts_query( $query ) {
        
        $query->set( 'meta_query', array( array(
           'post__in' => ['245374','245372']     
        )));
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search