Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
mccahan committed Apr 22, 2019
0 parents commit e9e349c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# LGND Expire Sticky Posts
This plugin automatically expires sticky posts after a set number of days (by default, 14). To use:

- Edit the `lgnd-expire-stickies.php` file to set the appropriate value of `LGND_EXPIRE_STICKIES_DAYS`
- Install the plugin by uploading it to the Plugins page in your WordPress backend
- Activate the plugin

Once installed and activated, the plugin will run using wp-cron **hourly**, selecting all sticky posts older than `LGND_EXPIRE_STICKIES_DAYS` and unstickying them.
54 changes: 54 additions & 0 deletions lgnd-expire-stickies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
/**
* Plugin Name: Expire Stickies
* Description: Automatically expire sticky posts after a set period of days
* Version: 1.0
* Author: LGND
* Author URI: https://lgnd.com
* Contributors: mccahan
*/

define('LGND_EXPIRE_STICKIES_DAYS', 14);

function lgndCronActivation()
{
if (!wp_next_scheduled('lgnd_expire_stickies'))
wp_schedule_event(time()+30, 'hourly', 'lgnd_expire_stickies');
}
register_activation_hook(__FILE__, 'lgndCronActivation');

// Delete cronjob on deactivation
function lgndCronDeactivation()
{
$time = wp_next_scheduled('lgnd_expire_stickies');
wp_unschedule_event($time, 'lgnd_expire_stickies');
}
register_deactivation_hook(__FILE__, 'lgndCronDeactivation');

// Cronjob function
function lgndExpireStickiesCron()
{
$before = time() - LGND_EXPIRE_STICKIES_DAYS*86400;
$args = [
'posts_per_page' => -1,
'post__in' => get_option('sticky_posts'),
'date_query' => [
[
'before' => [
'year' => date('Y', $before),
'month' => date('m', $before),
'day' => date('d', $before)
]
]
]
];
$query = new WP_Query($args);

while ($query->have_posts())
{
$query->the_post();
unstick_post(get_the_ID());
}
wp_reset_postdata();
}
add_action('lgnd_expire_stickies', 'lgndExpireStickiesCron');

0 comments on commit e9e349c

Please sign in to comment.