简体   繁体   中英

Sort WP_Query by ACF Datepicker

I have a “Upcoming Events” page, and a “Past Events” page. Each event has a custom field called “event_date”.

I want to create a loop that displays all of the events greater than today. I've taken a look through these articles, but couldn't get it to work: http://support.advancedcustomfields.com/forums/topic/how-do-i-filter-and-sort-event-posts-with-start-and-end-date/

https://wordpress.org/support/topic/plugin-advanced-custom-fields-sorting-by-date-picker

wordpress advanced custom fields order posts by date-picker

From what I've gathered in the three links above, I would put this in my functions.php file:

    // CREATE UNIX TIME STAMP FROM DATE PICKER
function custom_unixtimesamp ( $post_id ) {
    if ( get_post_type( $post_id ) == 'event_type' ) {
    $event_date = get_post_meta($post_id, 'event_date', true);

        if($event_date) {
            $dateparts = explode('/', $event_date);
            $newdate1 = strtotime(date('d.m.Y H:i:s', strtotime($dateparts[1].'/'.$dateparts[0].'/'.$dateparts[2])));
            update_post_meta($post_id, 'unixstartdate', $newdate1  );
        }
    }
}
add_action( 'save_post', 'custom_unixtimesamp', 100, 2);

Then I would add something like this to my page template:

<?php 
$today = time();
$args = array(
        'post_type' => 'event_type',
        'posts_per_page' => 5,
        'meta_query' => array(
            array(
            'key' => 'unixstartdate',
            'compare' => '>=',
            'value' => $today,
            )
        ),
        'meta_key' => 'event_date',
        'orderby' => 'meta_value',
        'order' => 'ASC',
    );

$query = new WP_Query( $args );
$event_type = $query->posts;
?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

Right now that's not turning up any results. My post-type is called “event_type”, and the key is “event_date”.

Any thoughts on where I'm going wrong?

I found a solution here thanks to svsdnb.

https://wordpress.org/support/topic/query-date-array-to-display-future-events-only

Instead of having to convert the timestamp in functions.php, there is a way to do this specifically with ACF where you use

current_time('Ymd')

instead of

 $today = date ('Ymd')

Here's what I've ended up with (and it seems to be working, and includes events that happen today):

<?php 
$today = current_time('Ymd');
$args = array(
    'post_type' => 'event_type',
    'post_status' => 'publish',
    'posts_per_page' => '0',
    'meta_query' => array(
        array(
            'key' => 'event_date',
            'compare' => '>=', // Upcoming Events - Greater than or equal to today
            'value' => $today,
        )
    ),
    'meta_key' => 'event_date',
    'orderby' => 'meta_value',
    'order' => 'ASC',
    );

$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
?>

It looks like you have a few issue with your code. Your date parsing also seems to be incorrect, though it's hard to say without more info. Using strtotime in both the function to set unixstarttime and your query code would be good. Your code uses time() which will include the seconds and leave out events for "today". You have no time zone so everything will be treated as GMT, which is fine as long as you keep that in mind.

Not really a big deal, but you are specifying two parameters for your callback in the add_action but the function only takes one argument.

The next problem is the have_posts() loop - you need to specify the custom query $query to loop over it.

<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

I also implemented it a slightly different way using the updated_{type}_meta action to only fire on the proper meta key, and then check the post type. It should only run when the meta value is updated rather than every time the post is saved. I would also recommend sorting by the unixstarttime meta value since it is numeric.

functions.php

// create/update unixstartdate based on event_type.event_date update
add_action( 'updated_post_meta', 'my_updated_post_meta', 20, 4 );
function my_updated_post_meta( $meta_id, $object_id, $meta_key, $_meta_value ){
    if ( $meta_key == 'event_date' && 'event_type' == get_post_type( $object_id ) ){
        $unixstartdate = strtotime( $_meta_value );
        update_post_meta( $object_id, 'unixstartdate', $unixstartdate );
    }
}

template page

$args = array(
    'post_type' => 'event_type',
    'posts_per_page' => 5,
    'meta_query' => array(
        array(
            'key' => 'unixstartdate',
            'compare' => '>=',
            'value' => strtotime('m/d/Y', time()),
        )
    ),
    'meta_key' => 'unixstartdate',
    'orderby' => 'meta_value',
    'order' => 'ASC',
);

$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
    the_title(); // whatever you want to use from $post
endwhile; endif;
<div class="row">

<?php 
$today = current_time('Ymd');
$args = array(
    'post_type' => 'your-custom-post-type',
    'post_status' => 'publish',
    'posts_per_page' => '-1',
    'meta_query' => array(
        array(
            'key' => 'event_date',//your date picker field name
            'compare' => '>=', // Upcoming Events - Greater than or equal to today
            'value' => $today,
        )
    ),
    'meta_key' => 'event_date',
    'orderby' => 'meta_value',
    'order' => 'Desc',
    );

$query = new WP_Query( $args );
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
?>
<?php  $dateformatstring = "F d, Y";

$unixtimestamp = strtotime(get_field('event_date')); ?>

<div class="post">

    <a href="<?php the_permalink; ?>"><?php the_title(); ?></a>
 <?php echo date_i18n($dateformatstring, $unixtimestamp); ?>

</div>

 <?php
  endwhile;
endif;
wp_reset_query();  // Restore global post data stomped by the_post().
?>

</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM