WordPress 自动删除自定义元字段

发布于 2024-12-18 22:53:59 字数 92 浏览 5 评论 0原文

假设我有一个名为“debut”的自定义元字段,我希望在 x 天(例如 30 天)后将其从帖子/数据库中删除,但帖子仍然保持完整,只是没有“debut”的值。我该怎么做呢?

Lets say I have a custom meta field called "debut" and I want after x amount of days(Ex 30 days) it to be deleted from the post/db but the post is still to remain intact just without the value of "debut". How would I go about doing that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遗失的美好 2024-12-25 22:53:59

我会创建一个简单的插件,挂钩到 WordPress 的加载状态,然后删除符合您标准的帖子元...

add_action('wp_loaded', 'cleanup_expired_meta');

function cleanup_expired_meta(){
    global $wpdb;
    $wpdb->execute('DELETE FROM '.$wpdb->prefix.'posts_meta WHERE [conditions]');
}

未经测试,只是从我的脑海中浮现出来,小心您用它做什么,可能很危险并删除所有帖子元...:)


这将是完整的请求,但请注意,没有添加元的日期。所以我采用帖子的最后修改日期。但如果用户修改了帖子,则元数据在 30 天之内不会被删除。

DELETE FROM `wp_posts` 
LEFT JOIN wp_postmeta ON wp_postmeta.post_id = wp_posts.id 
LEFT JOIN wp_postmeta AS releasedate ON releasedate.post_id = wp_posts.id AND releasedate.meta_key = "release-date"
WHERE wp_postmeta.meta_key = "Debut" 
AND DATE_ADD(releasedate.meta_value, INTERVAL 30 DAY) < NOW()

注意,我不确定这是否有效,因为 meta_value 的数据类型是 varchar,但如果转换有效,也许它不会抛出错误。

I would create a simple plugin that hooks to the loaded state of wordpress and just delete the post meta that meet your criterias...

add_action('wp_loaded', 'cleanup_expired_meta');

function cleanup_expired_meta(){
    global $wpdb;
    $wpdb->execute('DELETE FROM '.$wpdb->prefix.'posts_meta WHERE [conditions]');
}

Not tested, just got that off the top of my head, becareful what you do with that, can be dangerous and delete all your post metas... :)


That would be the full request, note tho, there are no dates when a META was added. So i'm taking the last modified date of the post. But if a user modifies the post then the meta won't be deleted before another 30 days.

DELETE FROM `wp_posts` 
LEFT JOIN wp_postmeta ON wp_postmeta.post_id = wp_posts.id 
LEFT JOIN wp_postmeta AS releasedate ON releasedate.post_id = wp_posts.id AND releasedate.meta_key = "release-date"
WHERE wp_postmeta.meta_key = "Debut" 
AND DATE_ADD(releasedate.meta_value, INTERVAL 30 DAY) < NOW()

Note, i'm not sure this will work since the data type of meta_value is a varchar, but if the conversion works, maybe it will not throw off errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文