|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Services: Set the order in which entities are displayed on the archive |
|
4
|
|
|
* page of the event entity. |
|
5
|
|
|
* |
|
6
|
|
|
* Sorts the order of the entities being displayed by reverse event start time |
|
7
|
|
|
* |
|
8
|
|
|
* @since 3.12.0 |
|
9
|
|
|
* @package Wordlift |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Define the {@link Wordlift_Event_Entity_Page_Service} class. |
|
14
|
|
|
* |
|
15
|
|
|
* @since 3.12.0 |
|
16
|
|
|
* @package Wordlift |
|
17
|
|
|
*/ |
|
18
|
|
|
class Wordlift_Event_Entity_Page_Service { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Set the entity post types as one to be included in archive pages. |
|
22
|
|
|
* |
|
23
|
|
|
* In order to have entities show up in standard WP categories (Posts categories) |
|
24
|
|
|
* we configure the `entity` post type, but we also need to alter the main |
|
25
|
|
|
* WP query (which by default queries posts only) to include the `entities`. |
|
26
|
|
|
* |
|
27
|
|
|
* @since 3.12.0 |
|
28
|
|
|
* |
|
29
|
|
|
* @param WP_Query $query WP's {@link WP_Query} instance. |
|
30
|
|
|
*/ |
|
31
|
|
|
public function pre_get_posts( $query ) { |
|
32
|
|
|
|
|
33
|
|
|
// Only for the main query, avoid problems with widgets and what not. |
|
34
|
|
|
if ( ! $query->is_main_query() ) { |
|
35
|
|
|
return; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// We don't want to alter the query if we're in the admin UI, if this is |
|
39
|
|
|
// not a event achieve query, or if the `suppress_filters` is set. |
|
40
|
|
|
// |
|
41
|
|
|
// Note that it is unlikely for `suppress_filter` to be set on the front |
|
42
|
|
|
// end, but let's be safe if it is set the calling code assumes no |
|
43
|
|
|
// modifications of queries. |
|
44
|
|
|
// |
|
45
|
|
|
// is_admin is needed, otherwise category based post filters will show |
|
46
|
|
|
// both types and at the current release (4.7) it causes PHP errors. |
|
47
|
|
|
if ( is_admin() || |
|
48
|
|
|
! is_tax( Wordlift_Entity_Types_Taxonomy_Service::TAXONOMY_NAME, 'event' ) || |
|
49
|
|
|
! empty( $query->query_vars['suppress_filters'] ) |
|
50
|
|
|
) { |
|
51
|
|
|
return; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// Update the query to use the start time meta and desc order. |
|
55
|
|
|
$meta_query[] = array( |
|
|
|
|
|
|
56
|
|
|
'key' => Wordlift_Schema_Service::FIELD_DATE_START, |
|
57
|
|
|
); |
|
58
|
|
|
$query->set( 'meta_query', $meta_query ); |
|
59
|
|
|
$query->set( 'orderby', 'meta_value' ); |
|
60
|
|
|
$query->set( 'order', 'DESC' ); |
|
61
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.