如何删除所有没有分配特色图片的帖子?

Howto remove all posts that do not have a Featured images assigned to them?

这会找到所有这些:

$args = array(
  'meta_query' => array(
     array(
       'key' => '_thumbnail_id',
       'value' => '?',
       'compare' => 'NOT EXISTS'
     )
  ),
);
$new_query = new WP_Query( $args );

如何制作一个迷你插件,当我激活它时,它会删除所有没有分配特色图片的帖子?

我正在尝试:

add_action( 'init', 'process_posts' );

function process_posts() {

    $args = array(
      'meta_query' => array(
         array(
           'key' => '_thumbnail_id',
           'value' => '?',
           'compare' => 'NOT EXISTS'
         )
      ),
    );

    $new_query = new WP_Query( $args );
     if (empty($_thumbnail_id)) {
       wp_delete_post($_POST['post_id'], true);
}
}

有人可以给我看看吗?谢谢

这是您可以使用的钩子的一些示例代码,您需要编写自己的循环代码来删除帖子。

add_action( 'init', 'process_posts' );

function process_posts() {

    $args = array(
      'meta_query' => array(
         array(
           'key' => '_thumbnail_id',
           'value' => '?',
           'compare' => 'NOT EXISTS'
         )
      ),
    );

    $new_query = new WP_Query( $args );

    // Delete your posts here with a loop

}

您可以使用 Wordpress 函数 wp_delete_post() 删除 post。为每个循环创建一个获取 post id 并将它们传递给 wp_delete_post() 的循环。我将此代码添加到我的 functions.php 文件中,它按预期工作。因为你有很多 posts 可能需要一些时间来执行。如果花费的时间太长,您可能需要在 php.ini 文件中调整 setTimeout。希望对您有所帮助!

$args = array(
  'meta_query' => array(
     array(
       'key' => '_thumbnail_id',
       'value' => '?',
       'compare' => 'NOT EXISTS'
     )
  ),
);


$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        $post_id = get_the_ID();
        wp_delete_post($post_id);
    }
    wp_reset_postdata();
}