2
votes

I have 2 custom field: "start-date" and "end-date".

If the administrator don't set the "end-date" field, i want to set dynamicly the end-date with the start-date.

Is it possible ?

I use ACF for my field, but if necessary, I can export my fields in php: to add to the file functions.php

I use the meta_key for the query.

Initially I was using "start-date" to sort my event. But when an event is on a couple of days it dissparaissait after the 2nd day.

So now I use "end-date", but the events of a single day does not have a "end-date" defined.

For information, if I defined a "end-date" for my event on a single day: It Works. But I would simplify the publication of an event, if the event is one day not need to set the end date (since it's the same as the start date)

my code:

$args = array(
  'post_type'=>'evenement','meta_key'=>'end-date','orderby'=>'meta_value_num' , 'order'=>'ASC' , 'posts_per_page'=> 3,

'meta_query'=> array (array('key' => 'end-date', 'compare' => '>=', 'value' => $current_date, 'type' => 'numeric',))

); query_posts( $args );

I speak very little English sorry if I'm not understandable

Maybe the problem can be solved if the fields are filled at the time of publication of the post. Is it possible to set the default value of the "end-date" field with the "start-date" fields

2

2 Answers

3
votes

I've found your question, because I have exactly the same problem as you. My answer comes two years later, but maybe it could help someone that is having the same issue.

The way to get it is to use an ACF filter executed while saving the custom field value.

// Auto-populate end date if it is empty.
function update_end_date_cf( $value, $post_id, $field ) {

   //NOTE: don't use get_field() because it retrieves the value
   //in a preformatted way different as it is saved in database       
   $end_date = get_post_meta( $post_id, 'end_date_cf_name', true );
   $start_date = get_post_meta( $post_id, 'start_date_cf_name', true );

   if ($end_date == '' && $start_date != '') {
      $value = $start_date;
   }

   return $value;

}
add_filter('acf/update_value/name=end_date_cf_name', 'update_end_date_cf', 10, 3);

Hope it helps!

0
votes

Logic:

if end-date is empty
set end-date equal to start-date

it will look something like this

<?php if( get_field('end-date') ): ?>
    <?php $end-date = $start-date ?>    
     //Your code here                
<?php endif; ?>