WordPressの記事データを変換しつつCSV形式でエクスポートする

構築・開発WordPress

エクスポートプラグイン「WP CSV Exporter

※基本無料プラグインですが、カスタム投稿タイプの記事を扱うときには、有料アドオンが必要になります。

プラグイン制作者様のサイト
WordPressの記事をCSVでダウンロードできるWP CSV Exporterプラグインを作ってみた

スポンサーリンク

エクスポートする記事のステータスを強制的にすべて「下書き」に変更する

<?php
    add_filter( 'wp_csv_exporter_post_status', 'wp_csv_exporter_post_status_filter', 10, 3 );
    function wp_csv_exporter_post_status_filter( $post_status, $post_id  ) {
        $post_status = 'draft';
        
        return $post_status;
    }
?>

記事本文内のデータをカスタムする。 下記は画像のパスをルートからフルパスに変換

<?php
    add_filter( 'wp_csv_exporter_post_content', 'wp_csv_exporter_post_content_filter', 10, 3 );
    function wp_csv_exporter_post_content_filter( $post_content, $post_id  ) {
        $content = $post_content;
        $content = str_replace('src="/', 'src="http://blog.cror.net/', $content);
        $content = str_replace('href="/', 'href="http://blog.cror.net/', $content);
        
        return $content;
    }
?>

カスタムフィールドの値をカスタムする。 下記は、画像のURLをルートからフルパスに変換

<?php
    add_filter( 'wp_csv_exporter_thumb_img', 'wp_csv_exporter_thumb_img_filter', 10, 3 );
    function wp_csv_exporter_thumb_img_filter( $field, $post_id ) {
        $thumb  = get_field('thumb_img', $post_id);
        
        return 'http://blog.cror.net'.$thumb;
    }
?>

必要なカテゴリだけエクスポートに含める。

<?php
    add_filter( 'wp_csv_exporter_post_category', 'wp_csv_exporter_post_category_filter', 10, 3 );
    function wp_csv_exporter_post_category_filter( $category, $post_id ) {
        $_category = array();
        
        $necessary = array('catA','catC');
        //catBがあってもエクスポートされない。
        
        foreach ( $category as $key => $value ) {
            if(in_array($value, $necessary)){
                $_category[] = $value;
            }
        }
        return $_category;
    }
?>

 

そのほか、エクスポート時にフィルターを使って色々と整形することが出来ます。
詳しくは、下記ページを参照してください。
https://wordpress.org/plugins/wp-csv-exporter/other_notes/

コメント

スポンサーリンク
タイトルとURLをコピーしました