Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
wordpress-检查附件是否存在_Wordpress_Attachment - Fatal编程技术网

wordpress-检查附件是否存在

wordpress-检查附件是否存在,wordpress,attachment,Wordpress,Attachment,我有一个在wordpress中创建atachment(zip文件)的函数 相关代码如下所示: 我的问题是:在创建新附件之前,是否有方法检查是否已经存在相同的附件名称(或ID或文件类型)?(在将附件插入数据库之前的含义) 在我的当前状态下-每次运行此函数时都会创建附件。这意味着它可以在每个tiem中创建一个新附件的DB记录-即使实际文件只有一个文件 $attachment = array( 'guid' => $wp_upload_dir['baseurl'] . _wp_rela

我有一个在wordpress中创建atachment(zip文件)的函数

相关代码如下所示:

我的问题是:在创建新附件之前,是否有方法检查是否已经存在相同的附件名称(或ID或文件类型)?(在将附件插入数据库之前的含义)

在我的当前状态下-每次运行此函数时都会创建附件。这意味着它可以在每个tiem中创建一个新附件的DB记录-即使实际文件只有一个文件

$attachment = array(
     'guid' => $wp_upload_dir['baseurl'] . _wp_relative_upload_path( $path  ), 
     'post_mime_type' => $wp_filetype['type'],
     'post_title' => preg_replace('/\.[^.]+$/', '', basename($path )),
     'post_content' => '',
     'post_status' => 'inherit'
  );


$attach_id = wp_insert_attachment( $attachment, $path , $post_id); // perform the magic


  require_once(ABSPATH . 'wp-admin/includes/image.php');


  wp_update_attachment_metadata( $attach_id, $attach_data ); // perform the magic II

更多关于代码发生位置的上下文将非常有用

尝试以下操作以检查是否存在附件:

global $wpdb;
$title_exists = $wpdb->get_results( 
    $wpdb->prepare( 
        "SELECT ID FROM $wpdb->posts 
        WHERE post_title = '$post->post_title' 
        AND post_type = 'attachment'"
    ) 
);
更改
post\u title='$post->post\u title'
,查看您需要的任何检查

参考: