0

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 );

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
    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
    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
    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
    Aug 11, 2022 at 22:50

1 Answer 1

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.

3
  • 1
    a reading of the generatepress code implies this is indeed the answer, but only assuming the $attributes conditions are reliable
    – Tom J Nowell
    Aug 11, 2022 at 22:57
  • @TomJNowell I actually focused straight on his meta query code and the this works comment, so I didn't notice that $attributes part.. (which sounds like the attributes of a dynamic block?) and your comment made me think that he might need to use another hook in the plugin which specifically used to modify the meta query, however I posted this answer mainly so that he knows the proper meta query formats. (edit: I forgot to tag you, so I reposted this comment..)
    – Sally CJ
    Aug 11, 2022 at 23:49
  • 1
    That did the trick Sally CJ, thank you very much, and for your help and sharing your knowledge too Tom (had to look up 'sophistry')
    – Chris
    Aug 12, 2022 at 0:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.