Ecommerce Shopify WordPress Discussion

Expire post after expiry date

I have been searching on how to expire posts after an expiry date - Ive tried a few plugins, but they only seem to work on articles posts in posts. Im using ACF and have a group called 'Event Group', within the group I have a fields called 'expiry_date' The date saves in the format of 'Ymd' I was trying to get the code to run as a daily cron, but its not working so the code now triggers when activating the plugin (when its working I can focus on the cron element) I have the below code: <?php /* Plugin Name: Auto Expire Events Description: Automatically sets events to 'draft' after the expiry date. Version: 1.0 Author: Dave */ // Exit if accessed directly. if (!defined('ABSPATH')) { exit; } // Function to check and update event status. function check_and_update_event_status() { $args = array( 'post_type' => 'event', // Change this to your custom post type. 'posts_per_page' => -1, 'post_status' => 'publish', 'meta_query' => array( array( 'key' => 'expiry_date', // Change this to the ACF field name. 'value' => date('Ymd'), // Adjust format according to 'Ymd'. 'compare' => '<', 'type' => 'DATE', // Change type to 'DATE'. ), ), ); $expired_events = new WP_Query($args); if ($expired_events->have_posts()) { while ($expired_events->have_posts()) { $expired_events->the_post(); $event_id = get_the_ID(); // Update event status to 'draft'. wp_update_post(array('ID' => $event_id, 'post_status' => 'draft')); } wp_reset_postdata(); } } // Schedule the function to run once, immediately after activation. function schedule_auto_expire_event_on_activation() { if (!wp_next_scheduled('auto_expire_event')) { wp_schedule_single_event(time(), 'auto_expire_event'); } } // Hook to run the function on scheduled event. add_action('auto_expire_event', 'check_and_update_event_status'); // Schedule the event on plugin activation. register_activation_hook(__FILE__, 'schedule_auto_expire_event_on_activation'); // Clear the scheduled event on plugin deactivation. register_deactivation_hook(__FILE__, 'wp_clear_scheduled_hook', 'auto_expire_event'); Anything blatently obvious thats incorrect within my code? Or a better way to test and identify an issue? Cheers
It seems like there's a syntax issue in your register_deactivation_hook. It should be something like: function clear_auto_expire_event_schedule() { wp_clear_scheduled_hook('auto_expire_event'); } register_deactivation_hook(__FILE__, 'clear_auto_expire_event_schedule');

January 23, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.