Support

Account

Home Forums Backend Issues (wp-admin) get custom post by specified date

Helping

get custom post by specified date

  • On my site i need to offer match schedules for the next week (if there is a schedule). So i added a custom post type and ad the fields for a file (for downloading the scedule) and a date field so the admin can select the matchday.

    After that I wrote this:

    <?php
        $nextsat = strtotime("next Saturday");
        date_default_timezone_set("UTC+1");
        $saturday = date(strtotime('+0 hours', $nextsat) * 1000);                    
                        
        $args = array(
            'posts_per_page' => -1,  // Get all posts
            'post_type' => 'schedule',
            'post_status' => 'publish',
            'meta_key' => 'schedule',
            'orderby' => 'meta_value_num',
            'order' => 'ASC',
            'meta_query' => array(
    	    'relation'	=> 'AND',
    	    array(
                    'key' => 'schedule',
                    'value' => $saturday,
                    'type' => 'NUMERIC',
                    'compare' => '>='
                )
            ),
        );
    
        $posts = get_posts( $args );
    
        foreach ( $posts as $post ){
            echo $post->post_title;
        }
    ?>

    After this a addes two schedules whit a date in the future but it displays only one.

    when i delete the “meta_query” i do get two but i really need to get only the next one (for the next matchday).

    Is there anybody who has any idea what’s wrong?

  • What version of ACF are you using?

    in ACF4, you set the format how the date is stored when you create the field. The value in your meta query needs to match this format.

    In ACF5 dates are always stored in the format ‘YYYYMMDD’ and the value for your meta query needs to match this format. Something like this I think: $saturday = date('Ymd', $nextsat);

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

You must be logged in to reply to this topic.