df9c7deef1
・タクソノミーのソート順登録/未登録両方取得するようget_postsのパラメータを修正 ・タクソノミーのソート順登録/未登録両方取得に伴いテンプレートを修正 ・通常と検索のソート順登録/未登録両方取得するようget_postsのパラメータを修正 ・「表示設定に従う」メニューへposts_per_pageの設定件数を表示 ・バグ修正:検索設定のnema属性修正と検索ページのメソッド引数修正
174 lines
4.7 KiB
PHP
174 lines
4.7 KiB
PHP
<?php
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
} // Exit if accessed directly
|
|
|
|
if ( ! trait_exists( 'APOP_ORDER_SETTING' ) ) {
|
|
|
|
trait APOP_ORDER_SETTING {
|
|
|
|
public function set_query() {
|
|
add_action( 'pre_get_posts', array( $this, 'apop_posts_per_page' ), 1 );
|
|
}
|
|
|
|
public function apop_posts_per_page( $query ) {
|
|
|
|
if ( is_admin() || ! $query->is_main_query() ) {
|
|
return;
|
|
}
|
|
|
|
if ( is_home() ) {
|
|
$this->set_search_normal_orderby( $query, 'normal' );
|
|
}
|
|
|
|
if ( is_search() ) {
|
|
$this->set_search_normal_orderby( $query, 'search' );
|
|
}
|
|
|
|
if ( is_category() ) {
|
|
$cat = get_category_by_slug( $query->query_vars['category_name'] );
|
|
$this->set_orderby( $query, $cat->term_id, 'category', 'cat' );
|
|
}
|
|
|
|
if ( is_tag() ) {
|
|
$tag = get_term_by( 'slug', $query->query_vars['tag'], 'post_tag' );
|
|
$this->set_orderby( $query, $tag->term_id, 'post_tag', 'tag' );
|
|
}
|
|
|
|
if ( is_tax() ) {
|
|
$this->set_orderby( $query, get_queried_object_id(), 'tax', 'tax' );
|
|
}
|
|
}
|
|
|
|
private function set_per_page( $query, $target ) {
|
|
$per_page_option = get_option( '_apop_per_page' );
|
|
|
|
if ( ! isset( $per_page_option[ $target ] ) ) {
|
|
return;
|
|
}
|
|
if ( $per_page_option[ $target ] == 'default' ) {
|
|
return;
|
|
}
|
|
if ( $per_page_option[ $target ] == 'all' ) {
|
|
$target = 'search';
|
|
}
|
|
|
|
$query->set( 'posts_per_page', $per_page_option[ $target ] );
|
|
}
|
|
|
|
private function set_search_normal_orderby( $query, $type ) {
|
|
$apop_order = get_option( '_apop_' . $type . '_order' ) ?? 1;
|
|
if ( $apop_order == 2 ) {
|
|
//$query->set( 'meta_key', '_apop_post_' . $type );
|
|
$query->set( 'meta_query', self::get_all_post_args( '_apop_' . $type . '_order' ) );
|
|
$query->set( 'orderby', array( 'meta_value_num' => 'ASC' ) );
|
|
} else {
|
|
$apop_order_param = get_option( '_apop_' . $type . '_order_param' );
|
|
$sort_param = array( 1 => 'ASC', 2 => 'DESC' );
|
|
if ( ! $apop_order_param ) {
|
|
return;
|
|
}
|
|
foreach ( $apop_order_param as $order_col => $orders ) {
|
|
if ( $orders['use'] == 1 ) {
|
|
$orderby[ $order_col ] = $sort_param[ $orders['sort'] ];
|
|
}
|
|
}
|
|
if ( isset( $orderby ) ) {
|
|
$query->set(
|
|
'orderby', $orderby
|
|
);
|
|
}
|
|
}
|
|
$this->set_per_page( $query, 'search' );
|
|
}
|
|
|
|
private function set_orderby( $query, $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 ) {
|
|
$sort_meta_key = '_apop_post_' . $target . '_' . $id;
|
|
//$query->set( 'meta_key', $sort_meta_key );
|
|
$query->set( 'meta_query', self::get_all_post_args( $sort_meta_key ) );
|
|
$query->set( 'orderby', array( 'meta_value_num' => 'ASC' ) );
|
|
$this->set_per_page( $query, self::get_per_page_tag( $target ) );
|
|
} else {
|
|
self::set_search_normal_orderby( $query, 'normal' );
|
|
}
|
|
}
|
|
|
|
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 ): array {
|
|
return array(
|
|
'relation' => 'OR',
|
|
array(
|
|
'key' => $sort_meta_key,
|
|
'compare' => 'EXISTS',
|
|
),
|
|
array(
|
|
'key' => $sort_meta_key,
|
|
'compare' => 'NOT EXISTS',
|
|
),
|
|
);
|
|
}
|
|
|
|
public static function get_normal_orderby() {
|
|
$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' );
|
|
$sort_param = array( 1 => 'ASC', 2 => 'DESC' );
|
|
foreach ( $apop_order_param as $order_col => $orders ) {
|
|
if ( $orders['use'] == 1 ) {
|
|
$orderby[ $order_col ] = $sort_param[ $orders['sort'] ];
|
|
}
|
|
}
|
|
if ( isset( $orderby ) ) {
|
|
return array(
|
|
'orderby' => $orderby,
|
|
);
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
public static function get_tax_orderby( $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 ) {
|
|
$sort_meta_key = '_apop_post_' . $target . '_' . $id;
|
|
|
|
return array(
|
|
'meta_key' => $sort_meta_key,
|
|
'orderby' => array( 'meta_value_num' => 'ASC' )
|
|
);
|
|
} else {
|
|
return self::get_normal_orderby();
|
|
}
|
|
|
|
}
|
|
|
|
public static function get_per_page( $target ) {
|
|
$per_page_option = get_option( '_apop_per_page' );
|
|
if ( ! isset( $per_page_option ) ) {
|
|
return;
|
|
}
|
|
if ( $per_page_option == 'default' ) {
|
|
return;
|
|
}
|
|
|
|
return array( 'posts_per_page' => $per_page_option );
|
|
}
|
|
|
|
}
|
|
}
|