WC_Query::get_catalog_ordering_args()publicWC 1.0

Returns an array of arguments for ordering products based on the selected values.

Method of the class: WC_Query{}

Return

Array.

Usage

$WC_Query = new WC_Query();
$WC_Query->get_catalog_ordering_args( $orderby, $order );
$orderby(string)
Order by param.
Default: ''
$order(string)
Order param.
Default: ''

WC_Query::get_catalog_ordering_args() code WC 8.6.1

public function get_catalog_ordering_args( $orderby = '', $order = '' ) {
	// Get ordering from query string unless defined.
	if ( ! $orderby ) {
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( (string) wp_unslash( $_GET['orderby'] ) ) : wc_clean( get_query_var( 'orderby' ) );

		if ( ! $orderby_value ) {
			if ( is_search() ) {
				$orderby_value = 'relevance';
			} else {
				$orderby_value = apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby', 'menu_order' ) );
			}
		}

		// Get order + orderby args from string.
		$orderby_value = is_array( $orderby_value ) ? $orderby_value : explode( '-', $orderby_value );
		$orderby       = esc_attr( $orderby_value[0] );
		$order         = ! empty( $orderby_value[1] ) ? $orderby_value[1] : $order;
	}

	// Convert to correct format.
	$orderby = strtolower( is_array( $orderby ) ? (string) current( $orderby ) : (string) $orderby );
	$order   = strtoupper( is_array( $order ) ? (string) current( $order ) : (string) $order );
	$args    = array(
		'orderby'  => $orderby,
		'order'    => ( 'DESC' === $order ) ? 'DESC' : 'ASC',
		'meta_key' => '', // @codingStandardsIgnoreLine
	);

	switch ( $orderby ) {
		case 'id':
			$args['orderby'] = 'ID';
			break;
		case 'menu_order':
			$args['orderby'] = 'menu_order title';
			break;
		case 'title':
			$args['orderby'] = 'title';
			$args['order']   = ( 'DESC' === $order ) ? 'DESC' : 'ASC';
			break;
		case 'relevance':
			$args['orderby'] = 'relevance';
			$args['order']   = 'DESC';
			break;
		case 'rand':
			$args['orderby'] = 'rand'; // @codingStandardsIgnoreLine
			break;
		case 'date':
			$args['orderby'] = 'date ID';
			$args['order']   = ( 'ASC' === $order ) ? 'ASC' : 'DESC';
			break;
		case 'price':
			$callback = 'DESC' === $order ? 'order_by_price_desc_post_clauses' : 'order_by_price_asc_post_clauses';
			add_filter( 'posts_clauses', array( $this, $callback ) );
			break;
		case 'popularity':
			add_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );
			break;
		case 'rating':
			add_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) );
			break;
	}

	return apply_filters( 'woocommerce_get_catalog_ordering_args', $args, $orderby, $order );
}