自动删除WordPress图像/缩略图(所有大小),并在X天/小时后显示,或类似?

时间:2014-03-05 作者:drabello

我 A.M LooK我NG FoR soLUT我oN To A.UTo DELETE 我MA.GEs oF oLD PosTs 我N WoRDPREss s我TE. 我 WA.NT To HoLD 我MA.GEs oF CURRENT 5.0 PosTs, oTHERs sHoULD BE DELETED A.UToMA.T我CA.LLY. 我T CA.N BE FUNCT我oN To DELETE oN T我ME 我NTER五、A.L, oR FUNCT我oN To HoLD oNLY 我MA.GEs oF LA.sT 5.0 PosTs. DoEs A.NYoNE KNoW FUNCT我oN oR soME PLUG我N WH我CH CA.N Do TH我s oR s我M我LA.R TH我NGs, To A.UToMA.T我CA.LLY DELETE oLDER 我MA.GEs? 我 BEL我E五、E THA.T sETT我NG A. CRoN JoB W我LL BE NEEDED.

&#十、A.;&#十、A.;

soMEoNE PosTED TH我s FUNCT我oN 我N sTA.CKo五、ERFLoW BUT 我 DoN\'T sEE A.NY MENT我oN oF T我ME.

&#十、A.;&#十、A.;
&#十、A.;FUNCT我oN DELETE_PosT_MED我A.( $PosT_我D ) {&#十、A.;&#十、A.;    $A.TTA.CHMENTs = GET_PosTs( A.RRA.Y(&#十、A.;        \'PosT_TYPE\'      => \'A.TTA.CHMENT\',&#十、A.;        \'PosTs_PER_PA.GE\' => -1.,&#十、A.;        \'PosT_sTA.TUs\'    => \'A.NY\',&#十、A.;        \'PosT_PA.RENT\'    => $PosT_我D&#十、A.;    ) );&#十、A.;&#十、A.;    FoREA.CH ( $A.TTA.CHMENTs A.s $A.TTA.CHMENT ) {&#十、A.;        我F ( FA.LsE === WP_DELETE_A.TTA.CHMENT( $A.TTA.CHMENT->我D ) ) {&#十、A.;            // LoG FA.我LURE To DELETE A.TTA.CHMENT.&#十、A.;        }&#十、A.;    }&#十、A.;}&#十、A.;&#十、A.;
&#十、A.;

1 个回复
SO网友:gmazzap

如果你只需要为最近50篇文章保留图像,我认为cron作业或WP cron不是你能做的最好的事情,在WordPress中,你可以知道一篇文章何时发布,你可以每次都运行一个例程,删除50篇之前发布的文章的图像。

这很简单,性能更好(如果无事可做,则什么也不做,无论是否有要删除的内容,cron作业都会运行)。

工作流程非常简单:

每次发布帖子时,获取第51个帖子id(按发布日期排序)

  • 删除所有以该id为帖子父项的图像
    1. 代码:

      add_action( \'save_post\', \'cleanup_old_post_images\', 10, 3 );
      
      function cleanup_old_post_images( $post_ID, $post, $update ) {
        if ( $update ) return; // do nothing on update
        $postid51th = get51th_postid(); // see below
        if ( ! empty( $postid51th ) && is_numeric( $postid51th ) ) {
          delete_post_media( $postid51th ); // see below, function in OP
        }
      }
      
      function get51th_postid() {
        return $GLOBALS[\'wpdb\']->get_var(
          "SELECT ID FROM " . $GLOBALS[\'wpdb\']->posts .
          " WHERE post_type = \'post\' AND post_status = \'publish\' ORDER BY post_date DESC
          LIMIT 50, 1"
        ); 
      }
      
      function delete_post_media( $post_id ) {
        $attachments = get_posts( array(
          \'post_type\'      => \'attachment\',
          \'nopaging\'       => TRUE,
          \'post_parent\'    => $post_id
        ) );
        if ( empty( $attachments ) ) return; // added this line to prevent errors
        foreach ( $attachments as $attachment ) {
          if ( false === wp_delete_attachment( $attachment->ID ) ) {
            // Log failure to delete attachment.
          }
        }
      }
      
      这适用于将代码放入站点后发布的帖子,但您可以编写run once函数来删除旧帖子的图像

      add_action( \'shutdown\', \'delete_older_attachments\' );
      
      function delete_older_attachments() {
      
        // run only on admin and use a transient to run once
        if ( ! is_admin() || get_transient(\'get_older_postids\') ) return;     
      
        // the query to exclude last 50 posts
        $latest_query = "SELECT ID FROM " . $GLOBALS[\'wpdb\']->posts .
          " WHERE post_type = \'post\' AND post_status = \'publish\' ORDER BY post_date DESC
          LIMIT 50"
        );
      
        // get older posts ids
        $older_ids = $GLOBALS[\'wpdb\']->get_row(
          "SELECT ID FROM " . $GLOBALS[\'wpdb\']->posts . 
          " WHERE post_type = \'post\' AND post_status = \'publish\'
          AND ID NOT IN (" . $latest_query . ")"
        );
      
        // get attachments for older posts
        if ( ! empty( $older_ids ) && is_array( $older_ids )  ) {
          $attachments = $GLOBALS[\'wpdb\']->get_row(
            "SELECT ID FROM " . $GLOBALS[\'wpdb\']->posts . 
            " WHERE post_type = \'attachments\'
            AND post_parent IN (" . implode( \',\', $older_ids ) . ")"
          );
        }
      
        if ( isset($attachments) && ! empty( $attachments ) && is_array( $attachments )  ) {
          foreach ( $attachments as $attachment ) {
            if ( false === wp_delete_attachment( $attachment ) ) {
              // Log failure to delete attachment.
            }
          }
          // set the transient to assure run once
          set_transient( \'get_older_postids\', 1 );
        }
      }
      

    结束

    相关推荐

    If is_single in functions.php

    我希望删除wpautop筛选器仅对我博客中的帖子起作用。因为在某些页面,我需要autop,而在某些页面,我需要它不在那里。我使用以下规则,这是在我的主题函数中。php:remove_filter( \'the_content\', \'wpautop\' ); 因为我只想在博客上看到它,所以我在考虑if语句,我在Wordpress中搜索了条件标记页面,发现我可以使用is\\u single。但它不起作用。这是我现在使用的代码。if(is_single() ){ remove_f