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

・ファイル名、クラス名の変更
・タクソノミーの有効化で、カテゴリー、タグ、カスタム分類のメタキーを分割
・表示用クラスの追加
・readmeの追加等
This commit is contained in:
2021-05-04 15:57:30 +09:00
parent 2ed2b25194
commit 7722f53f10
12 changed files with 409 additions and 211 deletions
+42
View File
@@ -0,0 +1,42 @@
<?php
class apop_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_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 );
}
}
}