• Resolved turbodb

    (@turbodb)


    I have a meta field (defined by the ACF plugin), with key event_date that stores a “date” in the post_meta table in the form: 20240515 (YYYYMMdd).

    I’d like to use a shortcode such as the following to pull all posts with event_date between two dates:

    [display-posts meta_key="event_date" meta_date_value_after="2024-01-01" meta_date_value_before="2024-06-01"]

    To do this, I think I need to do something similar to the various event calendar filter examples, so I’ve got a skeleton filter that looks like this:

    function atp_dps_event_date_between ( $args, $atts ) {
        
        // Ensure that all three parameters are in use and that meta_key is 'event_date'; 
        // else return the $args (no modifications necessary)
        if( ! ( isset( $atts['meta_key'] ) && $atts['meta_key'] == 'event_date'
                && isset( $atts['meta_date_value_after'] ) 
                && isset( $atts['meta_date_value_before'] ) ) )
            return $args;
    
        // what goes here?
    
    
        return $args;
    }
    add_filter( 'display_posts_shortcode_args', 'atp_dps_event_date_between', 10, 2 );

    My problem, obviously, is that I don’t know how to build up the meta query itself, as I’m not quite sure how to do the date/string comparison, as well as the “double” query to handle before/after. It would seem to me to be something along these lines for a “after,” but then I don’t know how to add the “before.”

    $meta_query = array(
        array(
          'key' => 'event_date',
          'value' => $atts['meta_date_value_after'],
          'compare' => '>=',
        )
      );

    I’m going to work on this a bit more on my end, hopefully finding a solution that I can post here for others to use. But, if anyone can hop in whit a solution, that’d be awesome.

Viewing 1 replies (of 1 total)
  • Thread Starter turbodb

    (@turbodb)

    I’ve worked on this a bit more, and here’s my solution. Due to my lack of experience with constructing meta queries (or WP_Query in general), I was assisted by Copilot, which worked out nicely to get me on the right track.

    I like this solution as it still allows me to use the “built-in” meta_key attribute for display-posts, but based on its value and the presence of my custom attributes, just “does the right thing.”

    Thanks again @billerickson for a great – and very versatile – plugin!

    function atp_dps_event_date_between ( $args, $atts ) {
    
        // Ensure that all three parameters are in use and that meta_key == 'event_date';
        // else return the $args (no modifications necessary)
    
        if( ! ( isset( $atts['meta_key'] ) && $atts['meta_key'] == 'event_date'
                && isset( $atts['meta_value_date_after'] )
                && isset( $atts['meta_value_date_before'] ) ) )
            return $args;
    
        $args['orderby'] = 'meta_value';
        $args['meta_key'] = $atts['meta_key'];
        $args['order'] = 'ASC';
    
        $meta_query = array(
            'relation' => 'AND',
            array(
                'key' => $atts['meta_key'],
                'value' => $atts['meta_value_date_before'],
                'compare' => '<=',
                'type' => 'DATE',
            ),
            array(
                'key' => $atts['meta_key'],
                'value' => $atts['meta_value_date_after'],
                'compare' => '>=',
                'type' => 'DATE',
            ),
        );
    
        $args['meta_query'] = $meta_query;
    
        return $args;
    }
    add_filter( 'display_posts_shortcode_args', 'atp_dps_event_date_between', 10, 2 );
Viewing 1 replies (of 1 total)

The topic ‘Meta query (with filter) to list posts *between* two dates’ is closed to new replies.