【问题标题】:Manually remove first image in wordpress database手动删除 wordpress 数据库中的第一张图片
【发布时间】:2015-09-08 14:14:27
【问题描述】:

我有 18,000 多个 wordpress 帖子,我使用一个函数来隐藏帖子中的第一张图片,但我不想再这样做了。我只想使用特色图片,仅此而已,而不必在帖子中插入特色图片来隐藏它(使用该功能)。

我正在考虑手动删除文本编辑器中的所有内容,但由于它存在 2 或 3 次,因此无法确定要删除哪个。示例:

 INSERT INTO `wp_posts` VALUES(16179, 88, '2009-05-28 08:32:47', '2009-05-28 12:32:47', '<img src="http://localhost/readjunk/wp-content/uploads/2009/05/news_0509_takingbacksundayalbum.jpg" alt="news_0509_takingbacksundayalbum" title="news_0509_takingbacksundayalbum" width="450" height="250" class="alignnone size-full wp-image-16180" />\r\nTaking Back Sunday will be streaming New Again in its entirety on their <a href="http://www.myspace.com/takingbacksunday">MySpace page</a> on Thursday May 28th and Friday May 29th.  New Again officially hits stores next Tuesday, June 2nd.\r\n<!--more-->\r\n\r\nTaking Back Sunday will <a href="http://localhost/readjunk/news/music/blink-182-reunite-and-announce-summer-tour/">be touring on select dates</a> with Blink 182. Check out their music video for Sink Into Me <a href="http://localhost/readjunk/media/music-video-taking-back-sunday-sink-into-me/">here</a>.\r\n\r\nFor more info visit:  <a href="http://www.takingbacksunday.com">www.takingbacksunday.com</a>\r\n', 'MySpace hosting Taking Back Sunday "New Again" listening party tonight and Friday', 0, '', 'publish', 'open', 'open', '', 'myspace-hosting-taking-back-sunday-new-again-listening-party-tonight-and-friday', '', '', '2009-05-28 08:32:47', '2009-05-28 12:32:47', '', 0, 'http://localhost/readjunk/?p=16179', 0, 'post', '', 0, NULL);
INSERT INTO `wp_posts` VALUES(16180, 88, '2009-05-28 08:28:27', '2009-05-28 12:28:27', '', 'news_0509_takingbacksundayalbum', 0, '', 'inherit', 'open', 'open', '', 'news_0509_takingbacksundayalbum', '', '', '2009-05-28 08:28:27', '2009-05-28 12:28:27', '', 16179, 'http://localhost/readjunk/wp-content/uploads/2009/05/news_0509_takingbacksundayalbum.jpg', 0, 'attachment', 'image/jpeg', 0, NULL);
INSERT INTO `wp_posts` VALUES(16181, 88, '2009-05-28 08:32:36', '2009-05-28 12:32:36', '<img src="http://localhost/readjunk/wp-content/uploads/2009/05/news_0509_takingbacksundayalbum.jpg" alt="news_0509_takingbacksundayalbum" title="news_0509_takingbacksundayalbum" width="450" height="250" class="alignnone size-full wp-image-16180" />\nTaking Back Sunday will be streaming New Again in its entirety on their <a href="http://www.myspace.com/takingbacksunday">MySpace page</a> on Thursday May 28th and Friday May 29th.  New Again officially hits stores next Tuesday, June 2nd.\n<!--more-->\n\nTaking Back Sunday will <a href="http://localhost/readjunk/news/music/blink-182-reunite-and-announce-summer-tour/">be touring on select dates</a> with Blink 182. Check out their music video for Sink Into Me <a href="http://localhost/readjunk/media/music-video-taking-back-sunday-sink-into-me/">here</a>.\n\nFor more info visit:  <a href="http://www.takingbacksunday.com">www.takingbacksunday.com</a>\n', 'MySpace hosting Taking Back Sunday "New Again" listening party tonight and Friday', 0, '', 'inherit', 'open', 'open', '', '16179-revision', '', '', '2009-05-28 08:32:36', '2009-05-28 12:32:36', '', 16179, 'http://localhost/readjunk/uncategorized/16179-revision/', 0, 'revision', '', 0, NULL);

只是两个 img 标签还是只是第一行?

【问题讨论】:

  • ID 看起来像 16179 及以上。您是在问如何根据 id 手动进行删除吗?是否有其他表格会禁止或搞砸?
  • 是的,不确定。我在 wp_posts 中,所以不确定为什么里面有 2 或 3 个相同的帖子。我正在使用一个特色图片插件,它使用帖子中的第一张图片,所以也许这就是创建额外 ID 的原因?不是程序员所以不确定。我想删除帖子中的图片,所以不确定我是否应该尝试删除第二个,因为第一个可能是附件?
  • 第一个是帖子,第二个是附件,第三个是修改。您可以在插入参数中看到post_type,它是倒数第四个参数'post''attachment''revision'。您不需要触摸附件或修订,可能您可以删除所有修订。

标签: mysql wordpress phpmyadmin


【解决方案1】:

在您的问题中,您发布了代表初始帖子的 3 个不同的 INSERT 语句(带有“发布”的 post_status),下一行是上传到媒体库的图像(带有 post_type 的 @ 987654324@),第三个代表对初始帖子的编辑(post_status 为“继承”)

我建议以编程方式执行此操作,而不是使用文本编辑器。您可以循环浏览所有帖子并使用 WordPress API 创建新的编辑,或者您可以使用新的 post_content 更新最新的帖子。

您应该能够使用正则表达式来删除内容中的第一张图片。比如:

$post->post_content = preg_replace( "~^[^>]*>~", "", $post->content );

在 Regex101.com 上查看正则表达式描述:https://regex101.com/r/jP9vZ6/1

【讨论】:

  • 谢谢,我该如何使用它?放入function.php?
  • 您可以在functions.php 中执行此操作,或者您可以使用开发人员/调试栏/调试栏控制台插件从浏览器运行任意PHP。安装后单击右上角的调试,然后单击左侧的控制台。绝对在非生产服务器上执行此操作:)
猜你喜欢
  • 2011-11-08
  • 1970-01-01
  • 2018-01-04
  • 1970-01-01
  • 2012-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-18
相关资源
最近更新 更多