ARCHIVE POST ORDER PLUS:カスタム投稿アーカイブの追加
カスタム投稿アーカイブ対応 全体設定で「全件」指定時に「全体設定に従う」の設定が反映されないバグの修正
This commit is contained in:
+89
-203
@@ -12,6 +12,11 @@ if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) {
|
||||
add_action( 'pre_get_posts', array( $this, 'apop_posts_per_page' ), 1 );
|
||||
}
|
||||
|
||||
/**
|
||||
* ソートのクエリを生成
|
||||
*
|
||||
* @param $query
|
||||
*/
|
||||
public function apop_posts_per_page( $query ) {
|
||||
|
||||
if ( is_admin() || ! $query->is_main_query() ) {
|
||||
@@ -26,6 +31,13 @@ if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) {
|
||||
$this->set_search_normal_orderby( $query, 'search' );
|
||||
}
|
||||
|
||||
if ( is_post_type_archive() ) {
|
||||
$post_type = get_query_var( 'post_type' );
|
||||
if ( $post_type ) {
|
||||
$this->set_custompost_orderby( $query, $post_type );
|
||||
}
|
||||
}
|
||||
|
||||
if ( is_category() ) {
|
||||
$cat = get_category_by_slug( $query->query_vars['category_name'] );
|
||||
if ( isset( $cat->term_id ) ) {
|
||||
@@ -47,33 +59,9 @@ if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) {
|
||||
}
|
||||
}
|
||||
|
||||
private static function set_per_page( $target, $id = null ) {
|
||||
$per_page_option = get_option( '_apop_per_page' );
|
||||
|
||||
if(!$per_page_option){
|
||||
return;
|
||||
}
|
||||
|
||||
if ( is_null( $id ) ) {
|
||||
$per_page_option_data = $per_page_option[ $target ];
|
||||
} else {
|
||||
$per_page_target = $target == 'tax' ? 'taxonomy' : $target;
|
||||
$per_page_option_data = $per_page_option[ $per_page_target ][ $id ];
|
||||
}
|
||||
|
||||
if ( ! isset( $per_page_option_data ) ) {
|
||||
return;
|
||||
}
|
||||
if ( $per_page_option_data == 'default' ) {
|
||||
return;
|
||||
}
|
||||
if ( $per_page_option_data == 'all' ) {
|
||||
$per_page_option_data = $per_page_option['search'];;
|
||||
}
|
||||
|
||||
return $per_page_option_data;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------
|
||||
// 最近の投稿と検索のソートのクエリ生成
|
||||
//-----------------------------------------------------------
|
||||
private function set_search_normal_orderby( $query, $type ) {
|
||||
$apop_order = get_option( '_apop_' . $type . '_order' ) ?? 1;
|
||||
if ( $apop_order == 2 ) {
|
||||
@@ -93,98 +81,9 @@ if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) {
|
||||
$query->set( 'posts_per_page', $per_page_option_data );
|
||||
}
|
||||
|
||||
private static function set_custom_filed_sort( &$orderby, $order_col, $orders, $sort_param ): array {
|
||||
if ( strpos( $order_col, 'custom_field' ) !== false ) {
|
||||
if($orders['field']['custom_field_type'] == 2){
|
||||
$meta_key = APOP_CUSTOM_FIELD_PREFIX . $orders['field']['meta_key'];
|
||||
}else{
|
||||
$meta_key = $orders['field']['meta_key'];
|
||||
}
|
||||
$type = 'CHAR';
|
||||
if ( $orders['field']['value_type'] == 'meta_value_num' ) {
|
||||
$type = 'NUMERIC';
|
||||
} elseif ( $orders['field']['value_type'] == 'meta_value' ) {
|
||||
$type = 'CHAR';
|
||||
} elseif ( $orders['field']['value_type'] == 'meta_date' ) {
|
||||
$type = 'DATE';
|
||||
}
|
||||
if ( isset( $orders['sort'] ) ) {
|
||||
$orderby[ $meta_key ] = $sort_param[ $orders['sort'] ];
|
||||
}
|
||||
|
||||
return array( $meta_key, $type );
|
||||
}
|
||||
}
|
||||
|
||||
private function set_orderby( $query, $id, $target, $order_key ) {
|
||||
$sort_type = self::get_tax_sort_type( $id );
|
||||
if ( $sort_type == 1 ) {
|
||||
$sort_meta_key = self::set_tax_sort_meta_key( $id, $target, $order_key );
|
||||
if ( is_null( $sort_meta_key ) ) {
|
||||
$this->set_search_normal_orderby( $query, 'normal' );
|
||||
} else {
|
||||
$query->set( 'meta_query', self::get_all_post_args( $sort_meta_key ) );
|
||||
$query->set( 'orderby', array( 'meta_value' => 'ASC', ) );
|
||||
}
|
||||
} else {
|
||||
$meta_orderby = self::set_tax_custom_field_orderby( $id );
|
||||
if ( isset( $meta_orderby['meta_query'] ) ) {
|
||||
$query->set( 'meta_query', $meta_orderby['meta_query'] );
|
||||
}
|
||||
if ( isset( $meta_orderby['orderby'] ) ) {
|
||||
$query->set( 'orderby', $meta_orderby['orderby'] );
|
||||
}
|
||||
}
|
||||
$per_page_option_data = self::set_per_page( self::get_per_page_tag( $target ), $id );
|
||||
$query->set( 'posts_per_page', $per_page_option_data );
|
||||
}
|
||||
|
||||
private static function get_tax_sort_type( $id ) {
|
||||
$sort_type = get_option( '_apop_tax_sort_type' );
|
||||
|
||||
return $sort_type[ $id ] ?? 1;
|
||||
}
|
||||
|
||||
private static function set_tax_sort_meta_key( $id, $target, $order_key ) {
|
||||
$order_settings = get_option( '_apop_' . $order_key . '_order' );
|
||||
if ( isset( $order_settings['target_cat'][ $id ] ) && $order_settings['target_cat'][ $id ] == 1 ) {
|
||||
return '_apop_post_' . $target . '_' . $id;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static function set_tax_custom_field_orderby( $id ): array {
|
||||
$tax_order_param = get_option( '_apop_tax_order_param' );
|
||||
if ( isset( $tax_order_param[ $id ] ) ) {
|
||||
return self::set_custom_field_orderby( $tax_order_param[ $id ] );
|
||||
}
|
||||
}
|
||||
|
||||
private static function get_per_page_tag( $target ) {
|
||||
if ( $target == 'post_tag' ) {
|
||||
return 'tag';
|
||||
}
|
||||
|
||||
return $target;
|
||||
}
|
||||
|
||||
private static function get_all_post_args( $sort_meta_key, $type = 'numeric' ): array {
|
||||
return array(
|
||||
'relation' => 'OR',
|
||||
array(
|
||||
'key' => $sort_meta_key,
|
||||
'compare' => 'EXISTS',
|
||||
'type' => $type,
|
||||
),
|
||||
array(
|
||||
'key' => $sort_meta_key,
|
||||
'compare' => 'NOT EXISTS',
|
||||
'type' => $type,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------
|
||||
//標準ソート+カスタムフィールドソートのカスタムフィールドデータ取得
|
||||
//-----------------------------------------------------------
|
||||
private static function set_custom_field_orderby( $apop_order_param ): array {
|
||||
if ( ! $apop_order_param ) {
|
||||
return array();
|
||||
@@ -211,94 +110,81 @@ if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) {
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* 1ページ表示件数を取得する
|
||||
* APOP::per_page(ID, [TARGET]);
|
||||
* [TARGET]
|
||||
* cat
|
||||
* tag
|
||||
* tax
|
||||
*
|
||||
* @param string $target
|
||||
* @param null $id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function per_page( $id = null, string $target = 'search' ) {
|
||||
if ( $target == 'cat' ) {
|
||||
$target = 'category';
|
||||
}
|
||||
|
||||
return array( 'posts_per_page' => self::set_per_page( $target, $id ) );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 最新の投稿ページのソートを取得する
|
||||
* APOP::orderby_normal()
|
||||
* @return array|null
|
||||
*/
|
||||
public static function orderby_normal() {
|
||||
$apop_order = get_option( '_apop_normal_order' ) ?? 1;
|
||||
if ( $apop_order == 2 ) {
|
||||
return array(
|
||||
'meta_query' => self::get_all_post_args( '_apop_normal_order' ),
|
||||
'orderby' => array( 'meta_value_num' => 'ASC' ),
|
||||
);
|
||||
} else {
|
||||
$apop_order_param = get_option( '_apop_normal_order_param' );
|
||||
$meta_orderby = self::set_custom_field_orderby( $apop_order_param );
|
||||
if ( is_null( $meta_orderby['meta_query'] ) ) {
|
||||
unset( $meta_orderby['meta_query'] );
|
||||
}
|
||||
if ( is_null( $meta_orderby['orderby'] ) ) {
|
||||
unset( $meta_orderby['orderby'] );
|
||||
}
|
||||
|
||||
return $meta_orderby;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* カテゴリー、タグ、カスタム分類のソートを取得する
|
||||
* APOP::orderby_tax([ID, TARGET], );
|
||||
* [TARGET]
|
||||
* cat
|
||||
* tag
|
||||
* tax
|
||||
*
|
||||
* @param $id
|
||||
* @param $target
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function orderby_tax( $id, $target ) {
|
||||
$sort_type = self::get_tax_sort_type( $id );
|
||||
if ( $sort_type == 1 ) {
|
||||
$order_key = $target;
|
||||
if ( $target == 'tag' ) {
|
||||
$target == 'post_tag';
|
||||
} elseif ( $target = 'cat' ) {
|
||||
$target == 'category';
|
||||
}
|
||||
$sort_meta_key = self::set_tax_sort_meta_key( $id, $target, $order_key );
|
||||
if ( is_null( $sort_meta_key ) ) {
|
||||
return self::orderby_normal();
|
||||
private static function set_custom_filed_sort( &$orderby, $order_col, $orders, $sort_param ): array {
|
||||
if ( strpos( $order_col, 'custom_field' ) !== false ) {
|
||||
if ( $orders['field']['custom_field_type'] == 2 ) {
|
||||
$meta_key = APOP_CUSTOM_FIELD_PREFIX . $orders['field']['meta_key'];
|
||||
} else {
|
||||
return array(
|
||||
'meta_query' => self::get_all_post_args( $sort_meta_key ),
|
||||
'orderby' => array( 'meta_value_num' => 'ASC' ),
|
||||
);
|
||||
$meta_key = $orders['field']['meta_key'];
|
||||
}
|
||||
} else {
|
||||
return self::set_tax_custom_field_orderby( $id );
|
||||
$type = 'CHAR';
|
||||
if ( $orders['field']['value_type'] == 'meta_value_num' ) {
|
||||
$type = 'NUMERIC';
|
||||
} elseif ( $orders['field']['value_type'] == 'meta_value' ) {
|
||||
$type = 'CHAR';
|
||||
} elseif ( $orders['field']['value_type'] == 'meta_date' ) {
|
||||
$type = 'DATE';
|
||||
}
|
||||
if ( isset( $orders['sort'] ) ) {
|
||||
$orderby[ $meta_key ] = $sort_param[ $orders['sort'] ];
|
||||
}
|
||||
|
||||
return array( $meta_key, $type );
|
||||
}
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------
|
||||
// ソートキーが無くともデータを抽出するようEXISTSとNOT EXISTSをリレーションする
|
||||
//-----------------------------------------------------------
|
||||
private static function get_all_post_args( $sort_meta_key, $type = 'numeric' ): array {
|
||||
return array(
|
||||
'relation' => 'OR',
|
||||
array(
|
||||
'key' => $sort_meta_key,
|
||||
'compare' => 'EXISTS',
|
||||
'type' => $type,
|
||||
),
|
||||
array(
|
||||
'key' => $sort_meta_key,
|
||||
'compare' => 'NOT EXISTS',
|
||||
'type' => $type,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------
|
||||
// 1ページ表示件数の取得
|
||||
//-----------------------------------------------------------
|
||||
private static function set_per_page( $target, $id = null ) {
|
||||
$per_page_option = get_option( '_apop_per_page' );
|
||||
|
||||
if ( ! $per_page_option ) {
|
||||
return;
|
||||
}
|
||||
|
||||
return;
|
||||
if ( is_null( $id ) ) {
|
||||
$per_page_option_data = $per_page_option[ $target ];
|
||||
} else {
|
||||
$per_page_target = $target == 'tax' ? 'taxonomy' : $target;
|
||||
$per_page_option_data = $per_page_option[ $per_page_target ][ $id ];
|
||||
}
|
||||
|
||||
if ( ! isset( $per_page_option_data ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( $per_page_option_data == 'default' ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( $per_page_option_data == 'all' ) {
|
||||
if ( $per_page_option['search'] == 'default' ) {
|
||||
return;
|
||||
}
|
||||
$per_page_option_data = $per_page_option['search'];
|
||||
}
|
||||
|
||||
return $per_page_option_data;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user