Inventory_Presser_Admin_Posts_List::enable_order_by_attachment_count( array $pieces, WP_Query $query )

Handles the ORDER BY on the vehicle list (edit.php) when sorting by photo count.


Parameters Parameters

$pieces

(array) (Required)

$query

(WP_Query) (Required)


Top ↑

Return Return

(array)


Top ↑

Source Source

File: includes/admin/class-admin-posts-list.php

	public function enable_order_by_attachment_count( $pieces, $query ) {
		if ( ! is_admin() ) {
			return $pieces;
		}

		/**
		 * We only want our code to run in the main WP query
		 * AND if an orderby query variable is designated.
		 */
		if ( $query->is_main_query() ) {
			// Get the order query variable - ASC or DESC.
			$order = strtoupper( $query->get( 'order', '' ) );

			// Make sure the order setting qualifies. If not, set default as ASC.
			if ( ! in_array( $order, array( 'ASC', 'DESC' ), true ) ) {
				$order = 'ASC';
			}

			$orderby = $query->get( 'orderby', '' );
			if ( apply_filters( 'invp_prefix_meta_key', 'photo_count' ) === $orderby
				|| apply_filters( 'invp_prefix_meta_key', 'thumbnail' ) === $orderby
			) {
				global $wpdb;
				$pieces['orderby'] = "( SELECT COUNT( ID ) FROM {$wpdb->posts} forget WHERE post_parent = {$wpdb->posts}.ID ) $order, " . $pieces['orderby'];
			}
		}
		return $pieces;
	}