Support

Account

Home Forums Front-end Issues Show posts between two dates

Solved

Show posts between two dates

  • Hi at all,

    I’ve got a small problem and I hope someone can help me. I created a page with ACF where I included two date picker – one for start and one for end.

    On the page template I display this from with <?php acf_form(); ?>. So the user can change and save the dates.

    After that I want to show all posts which are created and modified between these dates, so I did something like that on the same page template:

    $start = get_field('start', 124);
    $end = get_field('end', 124);
    
    $args = array(
        'date_query' => array(
            array(
                //'column' => 'post_modified_gmt',
                'after'     => $start,
                'before'    => $end,
                'inclusive' => true,
            ),
        ),
    );
    $query = new WP_Query( $args );

    But it doesn’t work. Can someone help me and say what’s the error? Thanks a lot. πŸ™‚

  • ACF does not store dates in the database in the correct format for using a date query. The format ACF uses for date fields is “YYYYMMDD”. You need to use a meta query, the value can be searching using “BETWEEN” as either “NUMERIC” or “CHAR” types.

  • Thanks for your answer. So I have to add a date field to my posts too to use the “key” in the meta query, am I right? Or is there are another solution without extra field for posts?

  • k, now that I’m looking back at this, you want to do a date query on post dates based on your values.

    Sorry, ignore my last comment. What you need to do is format the date returned by ACF to a full MySQL date. You return format from ACF should be “Y-m-d”.

    I’m not 100% sure that will work because post dates are stored as “Y-m-d H:i:s”, so you’ll need to test, but I can’t think of any reason that the query would not work without the time.

  • Yeah this solved my question. πŸ™‚ Thanks a lot!

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.