Frustrated and would love some help with this.
I have a custom post field titled 'expire_date'. When the expire date is reached, I want that post to to drop off of the list of posts that are returned.
I use Generate Blocks, and support at Generate Press were kind enough to give me some code that they say should work for what I want.
However, it doesn't. None of the posts I want filtered out are filtered out.
I've tested the 'if' statement and that query_args do work by using a different query_arg. It worked fine (and is included in the script below).
But the actual script that I need to work doesn't. I've tested every way that I can think of, double checked the custom field and it's working and titled correctly. I've changed the comparison operator to = and even tried != and it doesn't make any difference. Nothing is filtered out. Why?
Thanks for any help!
Chris
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
if (
! is_admin()
&& ! empty( $attributes['className'] )
&& strpos( $attributes['className'], 'my-query' ) !== false
) {
// pass meta_query parameter
$query_args[ 'meta_query' ] = array(
'meta_key' => 'expire_date', //example 2022-06-20
'meta_value' => date("Y-m-d"),
'type' => 'DATE', //have also tried DATETIME and TIME
'meta_compare' => '>=',
);
////in testing, this works/// $query_args['date_query'] = array( 'after' => '2022-06-01' );
}
return $query_args;
}, 10, 2 );
Frustrated and would love some help with this.
I have a custom post field titled 'expire_date'. When the expire date is reached, I want that post to to drop off of the list of posts that are returned.
I use Generate Blocks, and support at Generate Press were kind enough to give me some code that they say should work for what I want.
However, it doesn't. None of the posts I want filtered out are filtered out.
I've tested the 'if' statement and that query_args do work by using a different query_arg. It worked fine (and is included in the script below).
But the actual script that I need to work doesn't. I've tested every way that I can think of, double checked the custom field and it's working and titled correctly. I've changed the comparison operator to = and even tried != and it doesn't make any difference. Nothing is filtered out. Why?
Thanks for any help!
Chris
add_filter( 'generateblocks_query_loop_args', function( $query_args, $attributes ) {
if (
! is_admin()
&& ! empty( $attributes['className'] )
&& strpos( $attributes['className'], 'my-query' ) !== false
) {
// pass meta_query parameter
$query_args[ 'meta_query' ] = array(
'meta_key' => 'expire_date', //example 2022-06-20
'meta_value' => date("Y-m-d"),
'type' => 'DATE', //have also tried DATETIME and TIME
'meta_compare' => '>=',
);
////in testing, this works/// $query_args['date_query'] = array( 'after' => '2022-06-01' );
}
return $query_args;
}, 10, 2 );
Share
Improve this question
edited Aug 11, 2022 at 17:52
Tom J Nowell♦
60.8k7 gold badges77 silver badges147 bronze badges
asked Aug 11, 2022 at 17:47
ChrisChris
1053 bronze badges
4
|
1 Answer
Reset to default 2As stated in the documentation: (bold formatting was added by me)
Note that meta_query expects nested arrays, even if you only have one query.
So your meta query should actually look like the following, and note that in a meta_query
, we don't use the meta_
prefix, e.g. we use just key
and not meta_key
:
$query_args['meta_query'] = array(
array( // clause/query 1
'key' => 'expire_date',
'value' => date("Y-m-d"),
'type' => 'DATE',
'compare' => '>=',
)
);
Or use the root meta_xxx
arguments instead, and note that I used meta_type
instead of just type
:
$query_args['meta_key'] = 'expire_date';
$query_args['meta_value'] = date("Y-m-d");
$query_args['meta_type'] = 'DATE';
$query_args['meta_compare'] = '>=';
Or did you actually mean to use $query_args['meta_query'][]
(note the []
), i.e. to add another clause to the existing meta_query
array? But even so, make sure you use the correct array keys.
>=
higher or equal to/ present and future ).meta_query
is normally meant to indicate what you want, not what you don't want. I do see though that this is a generateblocks question, you need to go back to their support route, or ask other generatepress/generateblock users in their communities. 3rd party plugin/theme dev support questions are offtopic here – Tom J Nowell ♦ Commented Aug 11, 2022 at 17:53type
also bemeta_type
? – zac Commented Aug 11, 2022 at 21:55pre_get_posts
filter, but it is not, and you have no guarantee that it would work if we did. That's ignoring$attributes
. Eitherway it's sophistry – Tom J Nowell ♦ Commented Aug 11, 2022 at 22:50