Support » Fixing WordPress » category replace

  • I am trying to make the following code to work and from “draft” to change to specific id “id=144” category. Any help how its gonna make this work?

    // Schedule/Update Expiration
    $opts[‘expireType’] = ‘category’;
    $opts[‘id’] = $id;
    
    _scheduleExpiratorEvent($id,$ts,$opts);
    }
    add_action( ‘save_post’, ‘acf_set_expiry’, 10, 3 );

    until here it works but when i select category doesn’t save it…

    the defaults code was the folowing

    // Schedule/Update Expiration
    $opts[‘expireType’] = ‘draft’;
    $opts[‘id’] = $id;
    
    _scheduleExpiratorEvent($id,$ts,$opts);
    }
    add_action( ‘save_post’, ‘acf_set_expiry’, 10, 3 );

    Any solution??

Viewing 2 replies - 1 through 2 (of 2 total)
  • Please explain a bit more about what you are trying to attempt.

    Are you trying to restrict a user from using a category when creating a post?

    Are you trying to force a post to revert to using another when a user does use that cat?

    I am trying to change the following code and make the category work. Is with the ACF and Post Expirator plugins.

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

    The following code it works until the moment you select the post expire with a specific category. Then it doesn’t save it.

    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 );
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘category replace’ is closed to new replies.