Support

Account

Home Forums General Issues ACF Date Picker To Change Category

Solved

ACF Date Picker To Change Category

  • Hello all. I have been having a good play with ACF whilst off work with covid, and absolutely love it! The more you read the better it gets.
    What i am trying to do is change the category of a custom post when the expiry date from an ACF date picker field is reached. I have managed to get it to change the post to draft on expiry, but that defeats the object of our site, as even expired equipment needs to be available to view.
    The code i used to change status to draft:
    your_tooling and test_rigs are the names of the custom posts i need to check.
    tools_calibration_expiry is the name of my datepicker ACF.

    // Expire equipment on ACF expiry date field.
    if (!wp_next_scheduled('expire_posts')){
      wp_schedule_event(time(), 'daily', 'expire_posts'); // this can be hourly, twicedaily, or daily
    }
    
    add_action('expire_posts', 'expire_posts_function');
    
    function expire_posts_function() {
    	$today = date('Ymd');
    	$args = array(
    		'post_type' => array('your_tooling','test_rigs'), // post types you want to check
    		'posts_per_page' => -1 
    	);
    	$posts = get_posts($args);
    	foreach($posts as $p){
    		$expiredate = get_field('tools_calibration_expiry', $p->ID, false, false); // get the raw date from the db MUST CHANGE ACF FIELD VALUE
    		if ($expiredate) {
    			if($expiredate < $today){
    				$postdata = array(
    					'ID' => $p->ID,
    					'post_status' => 'draft'
    				);
    				wp_update_post($postdata);
    			}
    		}
    	}
    }

    Can anyone expand this call to change the post to a category i have set up as expired?
    It would also be helpful if two calls were made like below:
    your_tooling to use tools_calibration_expiry field and move to expiry category
    test_rigs to use rigs_calibration_expiry field to move to expiry category

    Any help on this would be fantastic!!

    Whilst also on the date picker field theme, i am just in the process of reading up on how to trigger an email notification to admin or someone else one month or so before the date picker expiry date is reached.
    If anyone has any links or tutorials on how to achieve something like that it would be so much appreciated.
    Thanks for reading.

  • https://developer.wordpress.org/reference/functions/wp_set_post_term

    
    if($expiredate < $today){
    	wp_set_post_terms($p->ID, $term_id, $taxonomy, true);
    }
    
  • Hi there,
    I am very interested in implementing the same functionality on my website – basically to manage posts that I treat as events.
    I haven’t played with code yet within WordPress – except HTML and CSS.
    Can this be achieved with a simple WPCode snippet ? Would you be kind enough to give me a hint on the process ?
    Thank you very much,
    Adrien

  • No, it needs to be done using a cron as the OP posted, otherwise it would need to happen on every page load on your site and that would not be a good practice as it could impact performance significantly.

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

You must be logged in to reply to this topic.