2ed2b25194
・ベース部分(カテゴリー、投稿)の表示順用カスタムフィールド設定登録の作成
43 lines
1.0 KiB
PHP
43 lines
1.0 KiB
PHP
<?php
|
|
|
|
class activate {
|
|
|
|
public static function add_sort_posts() {
|
|
|
|
$categories = get_categories();
|
|
foreach ( $categories as $category ) {
|
|
$posts = self::get_category_posts( $category->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_type' => 'post',
|
|
'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 = 'nb_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 );
|
|
}
|
|
}
|
|
|
|
} |