WC_Product_Data_Store_CPT::get_related_products_query()publicWC 3.0.0

Builds the related posts query.

Method of the class: WC_Product_Data_Store_CPT{}

No Hooks.

Return

Array.

Usage

$WC_Product_Data_Store_CPT = new WC_Product_Data_Store_CPT();
$WC_Product_Data_Store_CPT->get_related_products_query( $cats_array, $tags_array, $exclude_ids, $limit );
$cats_array(array) (required)
List of categories IDs.
$tags_array(array) (required)
List of tags IDs.
$exclude_ids(array) (required)
Excluded IDs.
$limit(int) (required)
Limit of results.

Changelog

Since 3.0.0 Introduced.

WC_Product_Data_Store_CPT::get_related_products_query() code WC 9.4.2

public function get_related_products_query( $cats_array, $tags_array, $exclude_ids, $limit ) {
	global $wpdb;

	$include_term_ids            = array_merge( $cats_array, $tags_array );
	$exclude_term_ids            = array();
	$product_visibility_term_ids = wc_get_product_visibility_term_ids();

	if ( $product_visibility_term_ids['exclude-from-catalog'] ) {
		$exclude_term_ids[] = $product_visibility_term_ids['exclude-from-catalog'];
	}

	if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && $product_visibility_term_ids['outofstock'] ) {
		$exclude_term_ids[] = $product_visibility_term_ids['outofstock'];
	}

	$query = array(
		'fields' => "
			SELECT DISTINCT ID FROM {$wpdb->posts} p
		",
		'join'   => '',
		'where'  => "
			WHERE 1=1
			AND p.post_status = 'publish'
			AND p.post_type = 'product'

		",
		'limits' => '
			LIMIT ' . absint( $limit ) . '
		',
	);

	if ( count( $exclude_term_ids ) ) {
		$query['join']  .= " LEFT JOIN ( SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN ( " . implode( ',', array_map( 'absint', $exclude_term_ids ) ) . ' ) ) AS exclude_join ON exclude_join.object_id = p.ID';
		$query['where'] .= ' AND exclude_join.object_id IS NULL';
	}

	if ( count( $include_term_ids ) ) {
		$query['join'] .= " INNER JOIN ( SELECT object_id FROM {$wpdb->term_relationships} INNER JOIN {$wpdb->term_taxonomy} using( term_taxonomy_id ) WHERE term_id IN ( " . implode( ',', array_map( 'absint', $include_term_ids ) ) . ' ) ) AS include_join ON include_join.object_id = p.ID';
	}

	if ( count( $exclude_ids ) ) {
		$query['where'] .= ' AND p.ID NOT IN ( ' . implode( ',', array_map( 'absint', $exclude_ids ) ) . ' )';
	}

	return $query;
}