$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - wp_query loop with compare operator simply not working, why?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - wp_query loop with compare operator simply not working, why?

matteradmin13PV0评论

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_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: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 type also be meta_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_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
Add a comment  | 

1 Answer 1

Reset to default 2

As 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.

Post a comment

comment list (0)

  1. No comments so far