php - 我需要从我的 wordpress 媒体库中删除重复的图像

标签 php mysql wordpress

我正在尝试从我的 wordpress 媒体库中删除重复的图像。

帖子本身并不重复,但每个帖子的每个附件图像出现两次。

我环顾四周,但没有人给出明确的答案。有人说使用这样的东西:

  <?php 
    $p = get_posts(array('numberposts'=> -1));
    foreach($p as $t) {
      $s = get_children(array('post_type' => 'attachment', 'numberposts' => -1 )); 
      foreach ($s as $u) {
        var_dump($u);
      }
    }
  ?>

但是好像还是少了点,像这样给我带来了一个附件列表,但是我还是不知道怎么比较。

在我看来,我需要使用一些 sql 查询并直接从数据库中删除媒体文件。不过,我不太确定该怎么做。

理论上我需要尝试类似查找帖子、foreach 帖子获取附件的操作,如果附件文件名 == 文件名则删除文件名。

感谢帮助。

最佳答案

在您的主题文件夹中创建一个文件,并在其中粘贴第一种或第二种方式的解决方案:

第一种方式(与他们的标题进行比较 - 推荐):

<?php

require('../../../wp-blog-header.php');
global $wpdb;

$querys = $wpdb->get_results(
    "
    SELECT a.ID, a.post_title, a.post_type
    FROM $wpdb->posts AS a
       INNER JOIN (
          SELECT post_title, MIN( id ) AS min_id
          FROM $wpdb->posts
          WHERE post_type = 'attachment'
          GROUP BY post_title
          HAVING COUNT( * ) > 1
       ) AS b ON b.post_title = a.post_title
    AND b.min_id <> a.id
    AND a.post_type = 'attachment'
    ");

echo "<style>td {padding:0 0 10px;}</style>";
echo "<h2>DUPLICATES</h2>\n";
echo "<table>\n";
echo "<tr><th></th><th>Title</th><th>URL</th></tr>\n";

foreach ( $querys as $query )
{
    $attachment_url = wp_get_attachment_url($query->ID);
    $delete_url = get_delete_post_link($query->ID);
    $delete_url = get_delete_post_link($query->ID);
    echo "<tr>
        <td><a style=\"color: #FFF;background-color: #E74C3C;text-decoration: none;padding: 5px;\" target=\"_blank\" href=\"".$delete_url."\">DELETE</a></td>
        <td>".get_the_title($query->ID)."</td>
        <td><a href=\"".$attachment_url."\">".$attachment_url."</a></td>
        </tr>\n";
}

echo "</table>";

?>

第二种方式(与他们的网址进行比较) 它的工作方式是,它会相互检查每个文件。因此,如果您有 file.jpg 和 file1.jpg 之类的文件,那么它会将它们捕获为重复项。此外,如果您有 file1.jpg 和 file11.jpg 之类的文件,那么它会将它们捕获为重复项,即使它们可能是完全不同的文件。 :

<?php

require('../../../wp-blog-header.php');

$args = array(
    'post_type' => 'attachment',
    'numberposts' => -1,
    'orderby' => 'name',
    'order' => 'ASC',
    'post_status' => null,
    'post_parent' => null, // any parent
);

$newArray = array();

$attachments = get_posts($args);
$attachments2 = $attachments;


if ($attachments){

    foreach($attachments as $post){
        $attachment_url = wp_get_attachment_url($post->ID);
        $delete_url = get_delete_post_link($post->ID);
        $newArray[] = array("att_url" => $attachment_url, "del_url" => $delete_url);
    }

    echo "<table>";
        $newArray2 = $newArray;
        echo "<tr><td><h2>DUPLICATES</h2></td></tr>";
        foreach($newArray as $url1){
            $url_del = $url1['del_url'];
            $url1 = $url1['att_url'];
            $url11 = substr($url1,0,strrpos($url1,"."));
            foreach($newArray2 as $url2){
                $url2 = $url2['att_url'];
                $url2 = substr($url2,0,strrpos($url2,".") - 1);

                if($url2 == $url11)
                {
                    echo "<tr><td><a href=\"".$url_del."\">DELETE</a></td><td><a href=\"".$url1."\">".$url1."</a></td></tr>";
                }
            }
        }
    echo "</table>";
    echo "<table>";
        echo "<tr><td><h2>ALL ATTACHMENTS</h2></td></tr>";

        foreach($attachments as $tst1){
            echo "<tr><td>".$tst1->guid."</td></tr>";
        }
    echo "</table>";
}
?>

然后尝试在浏览器中访问该文件,它会显示所有重复项的列表。

关于php - 我需要从我的 wordpress 媒体库中删除重复的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22274062/

相关文章:

php - laravel复选框插入mysql数据库 'Integrity constraint violation'

php - 我需要获取用户 ID 并将其添加到列表中

jquery - Wordpress 站点错误,文本框突然向上移动

php - MySQL 查询代码对 Web 服务的两个不同字段使用 OR

php - Codeigniter:如何将文件路径上传到数据库?

php - PHP 中的嵌套循环 - 布局问题

php - 对齐 php 变量的字段

mysql - 如何使用curdate实现查询?

WordPress 页面编辑器中的 javascript 不起作用

php - 具有不同字段的 Symfony CollectionType