我如何使用Cron在达到某个限制后删除某个帖子类型的帖子,比如最多保留50篇帖子?
原因是这些帖子会定期自动导入,所以我想防止DB变得太大,除了不需要旧帖子之外。
我如何使用Cron在达到某个限制后删除某个帖子类型的帖子,比如最多保留50篇帖子?
原因是这些帖子会定期自动导入,所以我想防止DB变得太大,除了不需要旧帖子之外。
虽然我不理解截断帖子的动机,但我认为这个练习对于您了解如何使用cron+WordPress很有价值。
function foobar_truncate_posts(){
global $wpdb;
# Set your threshold of max posts and post_type name
$threshold = 50;
$post_type = \'foobar\';
# Query post type
$query = "
SELECT ID FROM $wpdb->posts
WHERE post_type = \'$post_type\'
AND post_status = \'publish\'
ORDER BY post_modified DESC
";
$results = $wpdb->get_results($query);
# Check if there are any results
if(count($results)){
foreach($result as $post){
$i++;
# Skip any posts within our threshold
if($i <= $threshold)
continue;
# Let the WordPress API do the heavy lifting for cleaning up entire post trails
$purge = wp_delete_post($post->ID);
}
}
}
以下是WordPress中安排事件的两种基本方法。if(!wp_next_scheduled( \'foobar_truncate_posts_schedule\')){
wp_schedule_event(time(), \'daily\', \'foobar_truncate_posts_schedule\');
}
加入你的计划行动add_action(\'foobar_truncate_posts_schedule\', \'foobar_truncate_posts\');
如果您发现WP Cron缺少您的计划、发布计划帖子等。。。,您可以使用UNIX cron进一步自动化它。这里有一个great article 演示如何ping wp cron。php在指定的时间间隔。下面是他们建议使用UNIX cron保持wp cron准时运行的方法。wget http://www.server.com/wp-cron.php > /dev/null 2>&1
方法2:使用UNIX cron,您可以将真正的UNIX cron与本机管理ajax一起使用。php功能。sudo apt-get install php5-curl
然后sudo /etc/init.d/apache2 restart
.add_action(\'wp_ajax_nopriv_truncate_posts\', \'foobar_truncate_posts_cron\');
function foobar_truncate_posts_cron(){
# We use the user-agent as a shared key
$shared_user_agent = \'FooBar TruncatePostsCron/1.0\';
# Block unwanted IP addresses
$whitelisted_ips = array( //IPs allowed to run this operation
\'192.168.1.1\',
\'127.0.0.1\'
);
# Retrive Request Information
$request_user_agent = $_SERVER[\'HTTP_USER_AGENT\'];
$request_ip = $_SERVER[\'REMOTE_ADDR\'];
# Authenticate
if($request_user_agent === $shared_user_agent && in_array($request_ip, $whitelisted_ips))
echo foobar_truncate_posts(); // Reusable function
else
echo \'Authentication failed for post trucation cron.\';
exit;
}
添加Crontab此配置将每天持续运行一次。-A
设置共享用户代理机密-o
指定输出文件,action=truncate_posts
与ajax挂钩操作相关。验证/user/bin/curl
是执行cURL命令的正确路径。你可以使用curl
相反0 0 * * * /usr/bin/curl -A \'FooBar TruncatePostsCron/1.0\' -o ~/truncate_posts.log http://yourdomain.com/wp-admin/admin-ajax.php?action=truncate_posts
最后,始终确保register_globals=off
在php中。ini以防止任何类型的欺骗。foobar_truncate_posts()
. 我相信你可以从这里调整它。希望这对你有帮助!最可靠的方法是使用WP-Cron本身。但是,如果您的要求很低,您可以将其保持原样,这样就不会出现诸如多次访问多次触发wp cron和事件重叠之类的问题。
应参考Is it safe to run wp-cron.php twice if the first instance takes too long?
第三段中有这样一句话(实际上,这个答案值得更多的投票)很清楚:“因为wp cron会在运行作业之前重新安排和取消作业计划。在运行作业之前,它会取消作业计划。这会阻止作业运行两次”,是的,有人担心可能会出现一种种族状况,一个陷入困境的博客可能会两次踢出同一个事件。
现在wordpress团队有了一个解决方案,那就是在wp配置中禁用cron。php。这并没有禁用cron,但实际上禁用了在访问发生时触发cron。在此之后,可以使用wget或curl设置web cron,但如果我们不能确定服务将在30秒内完成,这恰好是php脚本的默认超时,那么应该使用php cli在服务器上设置cron。有关如何执行此操作的更深入的文档,请访问https://rtcamp.com/tutorials/wordpress/wp-cron-crontab/
如何从get\\u posts()返回的列表中选择第一个/最后一个对象?如何选择与get\\u posts返回的对象列表中的任意对象相关的前/后对象?如何从get\\u posts()返回的列表中提取子列表?