WP PLUGIN アーカイブ毎に投稿表示順を設定する

・ベース部分(カテゴリー、投稿)の表示順用カスタムフィールド設定登録の作成
This commit is contained in:
2021-05-03 18:51:02 +09:00
commit 2ed2b25194
15 changed files with 486 additions and 0 deletions
+43
View File
@@ -0,0 +1,43 @@
<?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 );
}
}
}