Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Post expire by acf date

  • birdhousedigital

    Hi,
    I’m using your php snippet (with updates for GB 2.0) to sort my posts by acf date.
    I have another acf date field, which is the end or expiry date. Do you have a snippet or any way to expire the post, ie no longer show it in the query loop, based on an acf date field?

  • Hi there,

    Try replacing the snippet with this one:

    add_filter('generateblocks_query_wp_query_args', function ($query_args, $attributes) {
        // apply filter if loop has class: order-by-date
        if (! is_admin() && ! empty($attributes['className']) && strpos($attributes['className'], 'order-by-date') !== false) {
    
            // Get today's date in the format that matches your end_date field
            $today = current_time('Y-m-d');
    
            // Set up the meta query to exclude posts where end_date matches today
            $meta_query = array(
                'relation' => 'AND',
                array(
                    'key' => 'event_end_date',
                    'value' => $today,
                    'compare' => '!=',
                    'type' => 'DATE'
                )
            );
    
            // Add or merge with existing meta_query if any
            if (isset($query_args['meta_query'])) {
                $query_args['meta_query']['relation'] = 'AND';
                $query_args['meta_query'][] = $meta_query[0];
            } else {
                $query_args['meta_query'] = $meta_query;
            }
    
            // Keep the original sorting by event-start_date
            $query_args = array_merge($query_args, array(
                'meta_key' => 'event-start_date',
                'meta_type' => 'DATE',
                'orderby' => 'meta_value',
                'order' => 'ASC',
            ));
        }
        return $query_args;
    }, 10, 2);
  • birdhousedigital

    Thanks Alvind,
    I changed compare: ‘!=’ ➝ compare: ‘>=’ and it works perfectly!

  • Ah yes, that was my mistake. Glad to know it’s all sorted!

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.