PluginProbe
Auto Listings – Car Listings & Car Dealership Plugin for WordPress / trunk
Auto Listings – Car Listings & Car Dealership Plugin for WordPress vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.1.0 2.1.1 2.1.2 All 81 releases
auto-listings / src / Query.php

Query.php in Auto Listings – Car Listings & Car Dealership Plugin for WordPress trunk, at src/Query.php

98 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Build query for archive listings page.
4 *
5 * @package Auto Listings.
6 */
7
8 namespace AutoListings;
9
10 /**
11 * Class Query
12 */
13 class Query {
14
15 /**
16 * Add hooks when module is loaded.
17 */
18 public function __construct() {
19 if ( is_admin() ) {
20 return;
21 }
22 add_action( 'pre_get_posts', [ $this, 'pre_get_posts' ] );
23 }
24
25 /**
26 * Hook into pre_get_posts to do the main listing query.
27 *
28 * @param mixed $query Query object.
29 */
30 public function pre_get_posts( $query ) {
31 $condition = apply_filters( 'auto_listings_ordering_condtion', ( $query->is_post_type_archive( 'auto-listing' ) || $query->is_tax( 'body-type' ) ) && $query->is_main_query() );
32 if ( ! $condition ) {
33 return;
34 }
35
36 $this->listings_query( $query );
37 }
38
39 /**
40 * Query the listings, applying sorting/ordering etc. This applies to the main WordPress loop.
41 *
42 * @param mixed $query Query object.
43 */
44 public function listings_query( $query ) {
45 $query->set( 'post_status', 'publish' );
46
47 // Ordering query vars.
48 $ordering = $this->get_ordering_args();
49 $query->set( 'orderby', $ordering['orderby'] );
50 $query->set( 'order', $ordering['order'] );
51 if ( isset( $ordering['meta_key'] ) ) {
52 $query->set( 'meta_key', $ordering['meta_key'] );
53 }
54 }
55
56 /**
57 * Query the listings, applying sorting/ordering etc. This applies to the main WordPress loop.
58 *
59 * @param string $orderby Sort listings by parameter.
60 * @param string $order Ascending or descending order.
61 */
62 public function get_ordering_args( $orderby = '', $order = '' ) {
63 // Get ordering from query string unless defined.
64 if ( ! $orderby ) {
65 $orderby_value = filter_input( INPUT_GET, 'orderby' );
66 $orderby_value = isset( $orderby_value ) ? esc_html( $orderby_value ) : 'date';
67
68 // Get order + orderby args from string.
69 $orderby_value = explode( '-', $orderby_value );
70 $orderby = esc_attr( $orderby_value[0] );
71 $order = ! empty( $orderby_value[1] ) ? $orderby_value[1] : $order;
72 }
73
74 $orderby = strtolower( $orderby );
75 $order = strtoupper( $order );
76 $args = [];
77
78 // Default - menu_order.
79 $args['orderby'] = 'date ID';
80 $args['order'] = 'OLD' === $order ? 'ASC' : 'DESC';
81 $args['meta_key'] = '';
82
83 switch ( $orderby ) {
84 case 'date':
85 $args['orderby'] = 'date ID';
86 $args['order'] = 'OLD' === $order ? 'ASC' : 'DESC';
87 break;
88 case 'price':
89 $args['orderby'] = 'meta_value_num ID';
90 $args['order'] = 'HIGH' === $order ? 'DESC' : 'ASC';
91 $args['meta_key'] = '_al_listing_price';
92 break;
93 }
94
95 return apply_filters( 'auto_listings_get_ordering_args', $args );
96 }
97 }
98