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 );
}
}
}
+125
View File
@@ -0,0 +1,125 @@
<?php
/**
* TODO:設定対象にカスタムタクソノミー、カスタム投稿を含める
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
} // Exit if accessed directly
if ( ! class_exists( 'post_sort_cat_order' ) ) {
class post_sort_cat_order {
const TEMPLATE_DIR = __DIR__ . '/../template/';
public function __construct() {
add_action( 'admin_menu', array( $this, 'add_pages' ) );
// add_action( 'transition_post_status', array( $this, 'add_cat_post_order' ), 10, 3 );
}
public function add_pages() {
add_menu_page(
'PSBC',
'PSBC',
'level_8',
'nb_post_sort',
array( $this, 'show_option_page' ),
'',
50
);
add_submenu_page(
'nb_post_sort', // parent_slug
'Archive Setting', // page_title
'カテゴリー選択', // menu_title
'administrator', // capability
'nb_post_sort_setting', // menu_slug
array( $this, 'display_setting_page' ) // function
);
}
public function display_setting_page() {
if ( isset( $_POST['post_cat_order'] ) ) {
check_admin_referer( 'sh_options' );
$opt = $_POST['post_cat_order'];
//ソート対象カテゴリーの登録
update_option( 'post_cat_order', $opt );
require_once self::TEMPLATE_DIR . 'success.php';
}
require_once self::TEMPLATE_DIR . 'setting.php';
}
public function show_option_page() {
if ( isset( $_POST['nb_post_cat'] ) ) {
check_admin_referer( 'sh_options' );
//投稿表示順の設定
$this->update_post_sort();
require_once self::TEMPLATE_DIR . 'success.php';
}
require_once self::TEMPLATE_DIR . 'sort.php';
}
//投稿表示順の設定
private function update_post_sort() {
$posts_sort = $_POST['nb_post_cat']['post_sort'];
foreach ( $posts_sort as $cat_id => $posts ) {
$sort_key = 'nb_post_sort_' . $cat_id;
foreach ( $posts as $post_id => $sort ) {
update_post_meta( $post_id, $sort_key, $sort );
}
}
}
/**
* 対象カテゴリー
*
* @param $opt
*
* @return array
*/
public static function get_category_list( $opt ): array {
if ( isset( $opt['target_cat'] ) ) {
$include_cat = implode( ',', $opt['target_cat'] );
if ( ! empty( $include_cat ) ) {
$category_args = array(
'hide_empty' => 0,
'include' => $include_cat,
);
return get_categories( $category_args );
}
}
return array();
}
// public function add_cat_post_order( $new_status, $old_status, $post ) {
// //リビジョンはパスする
// if ( wp_is_post_revision( $post->ID ) ) {
// return;
// }
//
// }
public static function get_sort_post_list( $cat_id ) {
$product_args = [
'post_type' => 'post',
'post_status' => array( 'publish', 'draft' ),
'posts_per_page' => - 1,
'category' => $cat_id,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_key' => 'nb_post_sort_' . $cat_id,
];
return get_posts( $product_args );
}
}
}