term_id ); foreach ( $posts as $post ) { self::create_sort_field( $post->ID, $category->term_id ); } } } //カテゴリーの投稿を取得する public static function get_category_posts( $cat_id ) { $args = array( 'post_status' => array( 'publish', 'draft' ), 'posts_per_page' => - 1, 'category' => $cat_id, ); return get_posts( $args ); } /** * ソート用のカスタムフィールド作成 * * @param $post_id * @param $cat_id */ public static function create_sort_field( $post_id, $cat_id ) { $sort_key = '_apop_post_sort_' . $cat_id; //カスタムフィールドが存在しなければ追加する $sort_filed = get_post_meta( $post_id, $sort_key, true ); if ( empty( $sort_filed ) ) { update_post_meta( $post_id, $sort_key, 0 ); } } }