1

I am trying to post saving using Post expirator and ACF

the code was :

// Schedule/Update Expiration
    $opts['expireType'] = 'draft';
    $opts['id'] = $id;

i am trying to change the $opts['expireType'] = 'draft'; to replace by an default selected category to $opts['expireType'] = 'category';

It works but when you select the category from admin panel, doesn't save the category...

Any help?

Here is my code in functions.php

  function acf_set_expiry($post_id, $post, $update){

        if ( empty( $_POST['fields'] ) )
            return;

        // You'll have to check to see what key your field uses in POST.
        // My field name is 'expiration-date', but in post it is referenced as 'field_583e5d591ee84'
        // We have to use the POST data because otherwise we'll be using the value from before the save/update
        if ( isset( $_POST['fields']['field_583e5d591ee84'] ) ) {
            // remove the default expiration action if this field exists
            remove_action( 'save_post', 'expirationdate_update_post_meta');
        }

        // Check if the post has a __expiration-date value
        if ( !empty( $_POST['fields']['field_583e5d591ee84'] ) ) {
            $date = $_POST['fields']['field_583e5d591ee84'];
            expirationdate_update_post_meta_acf($post_id, $date);
        }
        else {
            // Unschedule any existing event if the field is blank
            _unscheduleExpiratorEvent($post_id);
        }
    }

    function expirationdate_update_post_meta_acf($id, $date) {
        // don't run the echo if this is an auto save
        if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
            return;

        // don't run the echo if the function is called for saving revision.
        $posttype = get_post_type($id);
        if ( $posttype == 'revision' )
            return;

        // 'yymmdd' is the format my ACF date field is outputting, which is an accepted format for the DateTime constuctor.
        // Check how you're saving your date, and use the appropriate method to get it into a DateTime
        $formatted_date = new DateTime($date);

        $month   = intval($formatted_date->format('m'));
        $day     = intval($formatted_date->format('d'));
        $year    = intval($formatted_date->format('y'));

        // I am not using time in my ACF field, so I am setting it manually to the end of the day.
        $hour = 00;
        $minute = 00;

        $opts = array();
        $ts = get_gmt_from_date("$year-$month-$day $hour:$minute:0",'U');

        // Schedule/Update Expiration
        $opts['expireType'] = 'category';
        $opts['id'] = $id;


        _scheduleExpiratorEvent($id,$ts,$opts);
    }
    add_action( 'save_post', 'acf_set_expiry', 10, 3 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.