Support

Account

Home Forums ACF PRO Show 4 page titles based on next dates

Solving

Show 4 page titles based on next dates

  • Hi,

    I’m using a date picker to display a date for an event on pages. I want to be able to show the next 4 page titles on the homepage that have a date in the present or future. When one of the date is past, it is removed and replace with a new future page title.

    I’m an “intermediate” PHP developer and I’m not sure how to do this with the documentation available for the date picker field. Can you help me with a few hints?

    Thanks

  • For doing custom queries in WP you will want to learn WP_Query https://codex.wordpress.org/Class_Reference/WP_Query.

    ACF saves dates as YYYYMMDD

    Then you do a WP_Query using the field name of your ACF field in a meta_query

    The query below uses a new feature in WP that’s not in the codex yet and is explained here https://make.wordpress.org/core/2015/03/30/query-improvements-in-wp-4-2-orderby-and-meta_query/

    
    // if this isn't in a template that already have the global $post
    // available you may need to do
    // global $post;
    $today = date('Ymd');
    $args = array(
        'post_type' => 'page', // or your post type
        'posts_per_page' => 4, // the number of posts you want to show
        'meta_query' => array(
            'date_clause' => array(
                'key' => 'name_of_date_field',
                'value' => $today,
                'compare' => '>='
            ),
        ),
        'order_by' => array(
            'date_clause' => 'ASC',
            'title' => 'ASC',
        ),
    );
    $query = new WP_Query($args);
    if ($query->have_posts()) {
        $query->the_post();
        // loop to show what was returned in query
    }
    wp_reset_postdata();
    

    For more information on queries and loops see the codex page I linked to above.

  • I have edited my previous post to change the date format. ACF stores dates in the format YYYYMMDD

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

The topic ‘Show 4 page titles based on next dates’ is closed to new replies.