admin管理员组文章数量:1130349
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.
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
-
I fixed the indentation but the code says is that you want all posts that have an expire date that is newer than the current date (
>=higher or equal to/ present and future ).meta_queryis 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:53 - Thank you for fixing the formatting Tom, I did not realize it was messed up when I posted. I don't think this is specific to generatepress/generateblock though - the query args are wp, correct? I could be wrong. I did try = and != as comparison operators, just to test. I still got everything unfiltered. It's like the query args aren't being applied at all... – Chris Commented Aug 11, 2022 at 19:47
-
1
Not sure this is your issue but saw this note in the docs "The 'meta_key', 'meta_value', 'meta_type' and 'meta_compare' arguments will only work if you use the second method described below." developer.wordpress.org/reference/classes/wp_meta_query If you are using that method should
typealso bemeta_type? – zac Commented Aug 11, 2022 at 21:55 -
1
possible, but that doesn't mean they are, or that generatepress/generateblock doesn't do things to them, or has special semantics. You would need to ask someone familiar with it to know, hence the concern that it is off-topic. We could pretend it's the
pre_get_postsfilter, 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
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.
本文标签: wp querywpquery loop with compare operator simply not workingwhy
版权声明:本文标题:wp query - wp_query loop with compare operator simply not working, why? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1738466293a1574549.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


>=higher or equal to/ present and future ).meta_queryis 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:53typealso bemeta_type? – zac Commented Aug 11, 2022 at 21:55pre_get_postsfilter, 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