@@ -34,7 +34,10 @@ class Query {
34
34
*
35
35
* @return array|object|null Type corresponding to output type on success or null on failure.
36
36
*/
37
- public static function get_post ( $ id , $ args = array () ) {
37
+ public static function get_post ( $ id = null , $ args = array () ) {
38
+
39
+ global $ post ;
40
+
38
41
$ defaults = [
39
42
'meta_keys ' => 'null ' ,
40
43
'single ' => false ,
@@ -45,20 +48,23 @@ public static function get_post( $id, $args = array() ) {
45
48
46
49
extract ( $ options );
47
50
48
- $ post_data = get_post ( $ id , 'ARRAY_A ' );
51
+ // If id is not the same as global post get_post by id.
52
+ if ( $ post ->ID !== $ id ) {
53
+ $ post = get_post ( $ id );
54
+ }
49
55
50
- if ( is_array ( $ post_data ) ) {
51
- self ::get_post_meta ( $ post_data , $ post_data [ ' ID ' ] , $ meta_keys , $ single );
56
+ if ( is_object ( $ post ) ) {
57
+ self ::get_post_meta ( $ post , $ meta_keys , $ single );
52
58
} else {
53
59
// Return null.
54
- return $ post_data ;
60
+ return $ post ;
55
61
}
56
62
57
- $ post_data [ ' permalink ' ] = get_permalink ( $ id );
58
- $ post_data [ ' image_id ' ] = get_post_thumbnail_id ( $ id );
63
+ $ post -> permalink = get_permalink ( $ post -> ID );
64
+ $ post -> image_id = get_post_thumbnail_id ( $ post -> ID );
59
65
60
66
// Cast the post and return.
61
- return self ::cast_post_to_type ( $ post_data , $ output );
67
+ return self ::cast_post_to_type ( $ post , $ output );
62
68
}
63
69
64
70
/**
@@ -77,7 +83,9 @@ public static function get_post( $id, $args = array() ) {
77
83
*
78
84
* @return array|object|null Type corresponding to output type on success or null on failure.
79
85
*/
80
- public static function get_acf_post ( $ id , $ args = array () ) {
86
+ public static function get_acf_post ( $ id = null , $ args = array () ) {
87
+
88
+ global $ post ;
81
89
82
90
$ defaults = [
83
91
'meta_keys ' => null ,
@@ -92,39 +100,45 @@ public static function get_acf_post( $id, $args = array() ) {
92
100
93
101
extract ( $ options );
94
102
95
- $ acfpost = get_post ( $ id , 'ARRAY_A ' );
103
+ if ( $ post ->ID !== $ id ) {
104
+ $ acfpost = get_post ( $ id );
105
+ } else {
106
+ $ acfpost = $ post ;
107
+ }
96
108
97
- if ( is_array ( $ acfpost ) ) {
98
- $ acfpost[ ' fields ' ] = get_fields ( $ id );
109
+ if ( is_object ( $ acfpost ) ) {
110
+ $ acfpost-> fields = get_fields ( $ post -> ID );
99
111
100
112
// Get fields with relational post data as a whole acf object
101
113
if ( $ current_recursion_level < $ max_recursion_level ) {
102
114
103
115
// Let's avoid infinite loops by default by stopping recursion after one level. You may dig deeper in your view model.
104
116
$ options ['current_recursion_level ' ] = apply_filters ( 'dustpress/query/current_recursion_level ' , ++$ current_recursion_level );
105
117
106
- if ( is_array ( $ acfpost[ ' fields ' ] ) && count ( $ acfpost[ ' fields ' ] ) > 0 ) {
107
- foreach ( $ acfpost[ ' fields ' ] as &$ field ) {
118
+ if ( is_object ( $ acfpost-> fields ) && count ( $ acfpost-> fields ) > 0 ) {
119
+ foreach ( $ acfpost-> fields as &$ field ) {
108
120
$ field = self ::handle_field ( $ field , $ options );
109
121
}
110
122
}
111
123
} elseif ( true == $ whole_fields ) {
112
- if ( is_array ( $ acfpost[ ' fields ' ] ) && count ( $ acfpost[ ' fields ' ] ) > 0 ) {
113
- foreach ( $ acfpost[ ' fields ' ] as $ name => &$ field ) {
114
- $ field = get_field_object ($ name , $ id , true );
124
+ if ( is_object ( $ acfpost-> fields ) && count ( $ acfpost-> fields ) > 0 ) {
125
+ foreach ( $ acfpost-> fields as $ name => &$ field ) {
126
+ $ field = get_field_object ( $ name , $ post -> ID , true );
115
127
}
116
128
}
117
129
}
118
- self ::get_post_meta ( $ acfpost , $ id , $ meta_keys , $ single );
130
+
131
+ self ::get_post_meta ( $ acfpost , $ meta_keys , $ single );
119
132
}
120
133
121
- $ acfpost[ ' permalink ' ] = get_permalink ( $ id );
134
+ $ acfpost-> permalink = get_permalink ( $ post -> ID );
122
135
123
- if ( function_exists ("acf_get_attachment " ) ) {
124
- $ attachment_id = get_post_thumbnail_id ( $ id );
136
+ // Get ACF image object.
137
+ if ( function_exists ( 'acf_get_attachment ' ) ) {
138
+ $ attachment_id = get_post_thumbnail_id ( $ post ->ID );
125
139
126
140
if ( $ attachment_id ) {
127
- $ acfpost[ ' image ' ] = acf_get_attachment ( $ attachment_id );
141
+ $ acfpost-> image = acf_get_attachment ( $ attachment_id );
128
142
}
129
143
}
130
144
@@ -370,26 +384,26 @@ private static function parse_query_object( $query ) {
370
384
* @since 0.0.1
371
385
*
372
386
* @param array &$post The queried post.
373
- * @param int $id Id for the post.
374
387
* @param array/string $meta_keys Wanted meta keys or string 'ALL' to fetch all.
375
388
* @param boolean $single If true, return only the first value of the specified meta_key.
376
389
*
377
390
* @return array
378
391
*/
379
- private static function get_post_meta ( &$ post , $ id , $ meta_keys = NULL , $ single ) {
392
+ private static function get_post_meta ( &$ post , $ meta_keys = NULL , $ single = false ) {
393
+
380
394
$ meta = array ();
381
395
382
396
if ( $ meta_keys === 'all ' ) {
383
397
// Get all metadata.
384
- $ meta = get_metadata ( 'post ' , $ id , '' , $ single );
398
+ $ meta = get_metadata ( 'post ' , $ post -> ID , '' , $ single );
385
399
} elseif ( is_array ( $ meta_keys ) ) {
386
400
// Get the wanted metadata by defined keys.
387
401
foreach ( $ meta_keys as $ key ) {
388
- $ meta [$ key ] = get_metadata ( 'post ' , $ id , $ key , $ single );
402
+ $ meta [$ key ] = get_metadata ( 'post ' , $ post -> ID , $ key , $ single );
389
403
}
390
404
}
391
405
392
- $ post[ ' meta ' ] = $ meta ;
406
+ $ post-> meta = $ meta ;
393
407
}
394
408
395
409
/**
@@ -428,6 +442,7 @@ private static function get_meta_for_posts( &$posts = [], $meta_keys = NULL, $si
428
442
429
443
/**
430
444
* Used to cast posts to a desired type.
445
+ * Defaults to standard WordPress object.
431
446
*
432
447
* @type function
433
448
* @date 26/1/2016
@@ -440,13 +455,12 @@ private static function get_meta_for_posts( &$posts = [], $meta_keys = NULL, $si
440
455
*/
441
456
private static function cast_post_to_type ( $ post , $ type ) {
442
457
443
- if ( ' OBJECT ' === $ type ) {
444
- return ( object ) $ post ;
445
- } elseif ( ' ARRAY_N ' === $ type ) {
446
- return array_values ( $ post );
458
+ if ( $ type === ' ARRAY_A ' ) {
459
+ $ post = $ post-> to_array () ;
460
+ } elseif ( $ type === ' ARRAY_N ' ) {
461
+ $ post = array_values ( $ post-> to_array () );
447
462
}
448
463
449
- // Defaults to ARRAY_A
450
464
return $ post ;
451
465
}
452
466
0 commit comments