@@ -48,6 +48,10 @@ private function find_post( $post_name ) {
48
48
* @return array
49
49
*/
50
50
private function get_posts ( $ short_url , $ no_cache = false ) {
51
+ if ( empty ( $ short_url ) ) {
52
+ return [];
53
+ }
54
+
51
55
$ posts = wp_cache_get ( sprintf ( '%s:%s ' , $ this ->cache_key , $ short_url ) );
52
56
53
57
if ( empty ( $ posts ) || $ no_cache ) {
@@ -58,6 +62,11 @@ private function get_posts( $short_url, $no_cache = false ) {
58
62
'update_post_meta_cache ' => false ,
59
63
'update_post_term_cache ' => false ,
60
64
'meta_query ' => [
65
+ 'relation ' => 'AND ' ,
66
+ [
67
+ 'key ' => $ meta_key ,
68
+ 'compare ' => 'EXISTS '
69
+ ],
61
70
[
62
71
'key ' => $ meta_key ,
63
72
'value ' => $ short_url ,
@@ -66,9 +75,7 @@ private function get_posts( $short_url, $no_cache = false ) {
66
75
]
67
76
];
68
77
69
- $ query = new WP_Query ( $ args );
70
- $ posts = $ query ->get_posts ();
71
-
78
+ $ posts = ( new WP_Query ( $ args ) )->posts ;
72
79
$ posts = array_filter ( $ posts , function ( $ post ) use ( $ meta_key , $ short_url ) {
73
80
return get_post_meta ( $ post ->ID , $ meta_key , true ) === $ short_url ;
74
81
} );
@@ -380,7 +387,7 @@ public function save_post( $post_id ) {
380
387
381
388
if ( is_null ( $ meta_value ) ) {
382
389
add_post_meta ( $ post_id , $ this ->meta_key , $ value , true );
383
- } else if ( ! is_null ( $ meta_value ) && ! is_null ( $ value ) ) {
390
+ } else if ( ! is_null ( $ meta_value ) && ! empty ( $ value ) ) {
384
391
update_post_meta ( $ post_id , $ this ->meta_key , $ value );
385
392
} else {
386
393
delete_post_meta ( $ post_id , $ this ->meta_key );
0 commit comments