English 中文(简体)
wordpress - 检查附件存在
原标题:wordpress - check attachment existence

我有一个函数可以在 wordpress 中创建 aachment (zip 文件) 。

流出代码如下:

我的问题是:如果在创建新附件之前已经存在相同的附件名称( 或 ID 或 FILE TYPE)? (意指在将附件设置为 DB 之前)

在我目前的状态下, 每当此函数运行时, 就会创建附件 。 意思是它可以创建每条新附件的 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_ title = $ post-gt; post_ title 用于任何您需要的检查 。

参考文献:https://wordpress.stackschange.com/q/54258/12615>





相关问题
Wrap stray text in <p> tags

Wordpress issue.. how do I wrap stray text in P tags? Example: Before- <div class = "content"> <img src = "hello.jpg"/> <h1>Introduction</h1> Hello! this is ...

Using jQuery Plugins with Wordpress

Having a bit of trouble using jQuery plugins (Superfish, jQuery UI, etc) using Wordpress. Everything works fine in my plain non-Wordpress site, but Wordpress seems to conflict with JQuery. There must ...

WordPress Data Storage Efficiency

I ve been asked to review a WordPress plugin of sorts and try to find ways of making it faster. The premise of this plugin is basically to store a bunch of users and shifts and appointments and ...

Why can t I properly style a blockquote in Wordpress?

On the design I just created for my website, I have a blockquote styled with two quote images using the span technique in css: blockquote { background-image: url(images/openquote.jpg); background-...

How does the WordPress <!--nextpage--> tag actually work?

What happens? I m guessing that somehow the post or page is parsed before displaying, and then just split into two methods? I can t seem to find any documentation on how the underlying <?php ...

Wordpress Plug-ins: How-to add custom URL Handles

I m trying to write a Wordpress Plug-in but can t seem to figure out how you would modify how a URL gets handled, so for example: any requests made for: <url>/?myplugin=<pageID> will ...

热门标签