LoginSignup
3
3

More than 5 years have passed since last update.

WordpressプラグインのBroken Link Checkerで検知したリンク切れ記事を削除する

Posted at

リンク切れチェックのBroken Link Checker、リンク切れチェックした後に極端な例として削除しちゃっていいやって時のRubyスクリプト

require 'mysql2'
require 'rubypress'

wp = Rubypress::Client.new(
  host: ENV['WP_HOST'],
  username: ENV['WP_USERNAME'],
  password: ENV['WP_PASSWORD'],
  path: ENV['WP_PATH'],
  retry_timeouts: true,
)

client = Mysql2::Client.new(
  host: ENV['WP_DB_HOST'],
  username: ENV['WP_DB_USERNAME'],
  password: ENV['WP_DB_PASSWORD'],
  database: ENV['WP_DB_DATABASE']
)

query = %q{
select instances.container_id from wp_blc_links AS links
LEFT JOIN wp_blc_instances AS instances
ON (links.link_id = instances.link_id)
where links.http_code='404'
}
results = client.query(query)
post_ids = results.map{|row| row.values }.flatten.uniq

puts "delete post_ids #{post_ids}"

p post_ids.map{|post_id| wp.deletePost(post_id: post_id) }

対象の記事idが取得出来ればあとは良い感じに編集とかしちゃえば良いのだと思う

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3