Inventory_Presser_Shortcode_Slider::get_vehicle_IDs( $shortcode_atts )

On This Page


Source Source

File: includes/shortcode/class-shortcode-inventory-slider.php

	public static function get_vehicle_IDs( $shortcode_atts ) {
		// Check if any filters are specified.
		$has_filters = ! empty( $shortcode_atts['make'] )
			|| ! empty( $shortcode_atts['model'] )
			|| ! empty( $shortcode_atts['type'] )
			|| ! empty( $shortcode_atts['condition'] );

		// Base query arguments.
		$gpargs = array(
			'posts_per_page' => 10,
			'post_type'      => INVP::POST_TYPE,
			'meta_query'     => array(
				'relation' => 'AND',
				array(
					'key'     => '_thumbnail_id',
					'compare' => 'EXISTS',
				),
			),
			'fields'         => 'ids',
			'orderby'        => $shortcode_atts['orderby'],
			'order'          => $shortcode_atts['order'],
		);

		// Only require featured vehicles if no filters are specified.
		if ( ! $has_filters ) {
			$gpargs['meta_query'][] = array(
				'key'     => apply_filters( 'invp_prefix_meta_key', 'featured' ),
				'value'   => 1,
				'compare' => '=',
			);
		}

		// Add filter arguments (make, model, type, condition).
		$gpargs = self::add_make_and_model_query_args( $gpargs, $shortcode_atts );

		$inventory_ids = get_posts( $gpargs );

		// If we found less than 10 vehicles and no filters were specified, try non-featured vehicles.
		if ( count( $inventory_ids ) < 10 && ! $has_filters ) {
			$gpargs = array(
				'posts_per_page' => 10 - ( count( $inventory_ids ) ),
				'post_type'      => INVP::POST_TYPE,
				'meta_query'     => array(
					'relation' => 'AND',
					array(
						'relation' => 'OR',
						array(
							'key'     => apply_filters( 'invp_prefix_meta_key', 'featured' ),
							'value'   => array( '', '0' ),
							'compare' => 'IN',
						),
						array(
							'key'     => apply_filters( 'invp_prefix_meta_key', 'featured' ),
							'compare' => 'NOT EXISTS',
						),
					),
					array(
						'key'     => '_thumbnail_id',
						'compare' => 'EXISTS',
					),
				),
				'fields'         => 'ids',
				'orderby'        => $shortcode_atts['orderby'],
				'order'          => $shortcode_atts['order'],
			);

			$inventory_ids += get_posts( self::add_make_and_model_query_args( $gpargs, $shortcode_atts ) );
		}

		if ( ! $inventory_ids ) {
			return array();
		}

		shuffle( $inventory_ids );

		return $inventory_ids;
	}