【问题标题】:Mass associate posts with media elements in wordpress?大量将帖子与 wordpress 中的媒体元素相关联?
【发布时间】:2013-06-07 22:53:34
【问题描述】:

我正在从事一个 wordpress 项目,该项目涉及从电子表格中获取大约 990 个条目到系统中。我已经能够创建 CSV 文件来创建帖子,以及它们与少数自定义分类法的关联。但是现在我必须弄清楚这个媒体的事情。

990 个帖子,几乎所有帖子都需要有与之关联的图像和视频文件。

我尝试了一个用于将 csv 数据导入数据库的插件,并在 wp_postmeta 中创建具有关联的 Post ID、“_wp_attached_file”的元键和文件名的条目。我还在帖子表中添加了每个图像的条目,包括应该是正确的 guid、post_parent 和 post_mime_type。

我的主题可以根据需要提取数据并按应有的方式显示,但问题出在幕后。我去编辑一个帖子,除非它是我通过编辑帖子本身手动添加媒体的帖子,否则不会显示任何图像。我必须在某处的数据库中缺少一些关联,但我还无法追踪它。必须有一种方法通过 SQL 来处理这个问题,而不是手动编辑每个条目。

我正在查看 _wp_attachment_metadata,我想知道这是否是关键? wp_postmeta 和 wp_posts 中的条目是否真的毫无意义,而且都在 _wp_attachment_metadata 上?我不想开始一起破解 990(视频也是,所以 1980)序列化条目,只是为了发现这仍然不是缺失的链接。

我在 wp_posts 中有正确的父级的图像,在 postmeta 中有正确的信息。不过还是少了点什么……

【问题讨论】:

    标签: sql wordpress


    【解决方案1】:

    wp_insert_attachment

    $attach_id = wp_insert_attachment($attachment, $filename, $post->ID);
    $attach_data = wp_generate_attachment_metadata($attach_id, $path . $filename);
    wp_update_attachment_metadata($attach_id, $attach_data);
    

    【讨论】:

      【解决方案2】:

      以下是我从我编写的另一个插件中获取的方法。这是使用上传的文件 - $file 是一个包含结果的 post 数组 - 但是,如果您在磁盘上的某个地方有文件,那么您可以将 $filename 设置为该文件(删除 move_uploaded_file)。

          $uploads = wp_upload_dir();
      
          if ($uploads['error'] != '')
              wp_die($uploads['error']);
      
          $target_dir = $uploads['path'];
      
          if (!file_exists ($target_dir))
              mkdir ($target_dir);
      
          $filename = $file['name'];
      
          // If you get script file, it's a danger. Make it TXT file.
          if ( preg_match( '/\.(php|pl|py|rb|cgi)\d?$/', $filename ) )
                  $filename .= '.txt';
      
          $new_filename = wp_unique_filename( $target_dir, $filename );
          $new_url = trailingslashit( $uploads['url'] ).$new_filename;
          $new_filename = trailingslashit( $target_dir ) . $new_filename;
      
          $upload_result[$filename]['new_filename']=$new_filename;
      
          $upload_result[$filename]['valid'] = false;
          $upload_result[$filename]['reason'] = '';
          $upload_result[$filename]['url'] = $new_url;
          // Detect whether the uploaded file is an image
      
          $is_image = preg_match ('/(jpeg|png|gif)/i', $file['type']);
          $type = ($is_image) ? "img" : "file";
      
          if (!$is_image) {
              $upload_result[$filename]['reason'] =  "Sorry, you can only upload images.";
          } else if (move_uploaded_file ($file['tmp_name'], $new_filename)) {
              $upload_result[$filename]['valid'] = true;
              $upload_result[$filename]['reason'] = '';
      
              // Make sure the uploaded file is only readable for the owner process
              @chmod( $new_file, 0400 );
              //
              // now attach this to the original post - not really required except that it will ensure
              // that the image shows up on the right place in the media library and when viewing the post.
              $wp_filetype = wp_check_filetype(basename($new_filename), null );
              $attachment = array(
                  'post_mime_type' => $wp_filetype['type'],
                  'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
                  'post_content' => '',
                  'post_status' => 'inherit'
                  );
              $attach_id = wp_insert_attachment( $attachment, $new_filename, $commentData['comment_post_ID'] );
      
              // need to include the image.php file so  function wp_generate_attachment_metadata() works
              require_once(ABSPATH . 'wp-admin/includes/image.php');
      
              $attach_data = wp_generate_attachment_metadata( $attach_id, $filename );
              wp_update_attachment_metadata( $attach_id, $attach_data );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-10-04
        • 1970-01-01
        • 2019-03-31
        • 2022-07-07
        • 2013-01-10
        • 1970-01-01
        • 1970-01-01
        • 2010-11-13
        相关资源
        最近更新 更多