|
12 | 12 | */ |
13 | 13 | class G_a2z_Archive { |
14 | 14 |
|
15 | | - /** |
16 | | - * Initialize the plugin by setting localization and loading public scripts |
17 | | - * and styles. |
18 | | - * |
19 | | - * @since 1.0.0 |
20 | | - */ |
21 | | - public function __construct() { |
22 | | - add_filter( 'query_vars', array( $this, 'query_vars' ) ); |
23 | | - add_action( 'pre_get_posts', array( $this, 'check_qv' ) ); |
24 | | - } |
| 15 | + /** |
| 16 | + * Initialize the plugin by setting localization and loading public scripts |
| 17 | + * and styles. |
| 18 | + * |
| 19 | + * @since 1.0.0 |
| 20 | + */ |
| 21 | + public function __construct() { |
| 22 | + add_filter( 'query_vars', array( $this, 'query_vars' ) ); |
| 23 | + add_action( 'pre_get_posts', array( $this, 'check_qv' ) ); |
| 24 | + } |
25 | 25 |
|
26 | | - /** |
27 | | - * Add our value |
28 | | - * |
29 | | - * @param array $query_vars |
30 | | - * @return array |
31 | | - */ |
32 | | - public function query_vars( $query_vars ) { |
33 | | - array_push( $query_vars, 'az' ); |
34 | | - return $query_vars; |
35 | | - } |
| 26 | + /** |
| 27 | + * Add our value |
| 28 | + * |
| 29 | + * @param array $query_vars |
| 30 | + * @return array |
| 31 | + */ |
| 32 | + public function query_vars( $query_vars ) { |
| 33 | + array_push( $query_vars, 'az' ); |
| 34 | + return $query_vars; |
| 35 | + } |
36 | 36 |
|
37 | | - /** |
38 | | - * Check our value |
39 | | - * |
40 | | - * @global object $wp_query |
41 | | - * @param object $query |
42 | | - */ |
43 | | - public function check_qv( $query ) { |
44 | | - global $wp_query; |
45 | | - if ( $query->is_main_query() && isset( $wp_query->query_vars[ 'az' ] ) ) { |
46 | | - // if we are on the main query and the query var 'a2z' exists, modify the where/orderby statements |
47 | | - add_filter( 'posts_where', array( $this, 'modify_query_where' ) ); |
48 | | - add_filter( 'posts_orderby', array( $this, 'modify_query_orderby' ) ); |
49 | | - } |
| 37 | + /** |
| 38 | + * Check our value |
| 39 | + * |
| 40 | + * @global object $wp_query |
| 41 | + * @param object $query |
| 42 | + */ |
| 43 | + public function check_qv( $query ) { |
| 44 | + if ( $query->is_main_query() && $query->is_archive() && isset( $query->query_vars[ 'az' ] ) ) { |
| 45 | + // if we are on the main query and the query var 'a2z' exists, modify the where/orderby statements |
| 46 | + add_filter( 'posts_where', array( $this, 'modify_query_where' ) ); |
| 47 | + add_filter( 'posts_orderby', array( $this, 'modify_query_orderby' ) ); |
50 | 48 | } |
| 49 | + } |
51 | 50 |
|
52 | | - /** |
53 | | - * Alter the SQL |
54 | | - * |
55 | | - * @global object $wp_query |
56 | | - * @global object $wpdb |
57 | | - * @param string $where |
58 | | - * @return string |
59 | | - */ |
60 | | - public function modify_query_where( $where ) { |
61 | | - global $wp_query, $wpdb; |
62 | | - $where .= " AND substring( TRIM( LEADING 'A ' FROM TRIM( LEADING 'AN ' FROM TRIM( LEADING 'THE ' FROM UPPER( $wpdb->posts.post_title ) ) ) ), 1, 1) = '" . $wp_query->query_vars[ 'az' ] . "'"; |
63 | | - return $where; |
64 | | - } |
| 51 | + /** |
| 52 | + * Alter the SQL |
| 53 | + * |
| 54 | + * @global object $wp_query |
| 55 | + * @global object $wpdb |
| 56 | + * @param string $where |
| 57 | + * @return string |
| 58 | + */ |
| 59 | + public function modify_query_where( $where ) { |
| 60 | + global $wp_query, $wpdb; |
| 61 | + $where .= " AND substring( TRIM( LEADING 'A ' FROM TRIM( LEADING 'AN ' FROM TRIM( LEADING 'THE ' FROM UPPER( $wpdb->posts.post_title ) ) ) ), 1, 1) = '" . $wp_query->query_vars[ 'az' ] . "'"; |
| 62 | + remove_filter( 'posts_where', array( $this, 'modify_query_where' ) ); |
| 63 | + return $where; |
| 64 | + } |
65 | 65 |
|
66 | | - /** |
67 | | - * Alter the SQL |
68 | | - * |
69 | | - * @global object $wpdb |
70 | | - * @param string $orderby |
71 | | - * @return string |
72 | | - */ |
73 | | - public function modify_query_orderby( $orderby ) { |
74 | | - global $wpdb; |
75 | | - $orderby = "( TRIM( LEADING 'A ' FROM TRIM( LEADING 'AN ' FROM TRIM( LEADING 'THE ' FROM UPPER( $wpdb->posts.post_title ) ) ) ) )"; |
76 | | - return $orderby; |
77 | | - } |
| 66 | + /** |
| 67 | + * Alter the SQL |
| 68 | + * |
| 69 | + * @global object $wpdb |
| 70 | + * @param string $orderby |
| 71 | + * @return string |
| 72 | + */ |
| 73 | + public function modify_query_orderby( $orderby ) { |
| 74 | + global $wpdb; |
| 75 | + $orderby = "( TRIM( LEADING 'A ' FROM TRIM( LEADING 'AN ' FROM TRIM( LEADING 'THE ' FROM UPPER( $wpdb->posts.post_title ) ) ) ) )"; |
| 76 | + |
| 77 | + remove_filter( 'posts_orderby', array( $this, 'modify_query_orderby' ) ); |
| 78 | + return $orderby; |
| 79 | + } |
78 | 80 |
|
79 | 81 | } |
80 | 82 |
|
|
0 commit comments