Last active
July 26, 2018 08:43
-
-
Save Tenderfeel/8edb8ce07286d22d42413ecb7017edab to your computer and use it in GitHub Desktop.
投稿一覧でカスタムフィールドの値を使ってフィルタリングや並び替えする
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* カラムの追加と削除 | |
*/ | |
add_filter('manage_cat_story_posts_columns' , function ($columns) { | |
//削除 | |
unset( $columns['author'] ); | |
unset( $columns['comments'] ); | |
$new_columns = array( | |
'cat_type' => '猫タイプ', | |
'adsence' => '広告の有無', | |
'recommend_point' => 'おすすめ度', | |
'custom_order' => '並び順' | |
); | |
//dateの前に追加する | |
$pos = array_search('date', array_keys($columns)); | |
$columns = array_merge( array_slice($columns, 0, $pos), $new_columns, array_slice($columns, $pos) ); | |
return $columns; | |
}); | |
/** | |
* 値の表示 | |
*/ | |
add_action( 'manage_cat_story_posts_custom_column' , function ( $column, $post_id ) { | |
switch ( $column ) { | |
case 'cat_type' : | |
$cat_type = get_field_object('field_5b5964e901fa4')['choices']; | |
$cat_type_val = get_field('cat_type', $post_id); | |
echo !empty($cat_type_val) ? $cat_type[$cat_type_val] : '—'; | |
break; | |
case 'adsence': | |
echo (int) get_field('adsence', $post_id) === 1 ? '○' : '—'; | |
break; | |
case 'recommend_point': | |
echo (int) get_field('recommend_point', $post_id); | |
break; | |
case 'custom_order': | |
echo (int) get_field('custom_order', $post_id) ; | |
break; | |
} | |
}, 10, 2 ); | |
/** | |
* 並び替えカラムの指定 | |
*/ | |
add_filter("manage_edit-news_sortable_columns", function ( $columns ) { | |
$columns['recommend_point'] = 'recommend_point'; | |
$columns['custom_order'] = 'custom_order'; | |
return $columns; | |
}); | |
/** | |
* クエリーの追加 | |
*/ | |
add_action( 'parse_query', function ($query) { | |
global $typenow, $pagenow; | |
if ( 'cat_story' == $typenow && is_admin() && $pagenow == 'edit.php') { | |
//recommend_point順の並び替え | |
if( isset($_GET['orderby']) && $_GET['orderby'] === 'recommend_point' ) { | |
$query->query_vars['meta_key'] = 'recommend_point'; | |
$query->query_vars['orderby'] = 'meta_value_num'; | |
} | |
//recommend_point順の並び替え | |
if( isset($_GET['orderby']) && $_GET['orderby'] === 'custom_order' ) { | |
$query->query_vars['meta_key'] = 'custom_order'; | |
$query->query_vars['orderby'] = 'meta_value_num'; | |
} | |
//カスタムフィールドによるフィルタ | |
$meta_query = array(); | |
if( array_key_exists('cat_type', $_GET) && $_GET['cat_type'] !== '') { | |
$meta_query[] = array( | |
'key' => 'cat_type', | |
'value' => $_GET['cat_type'], | |
'compare' => '==' | |
); | |
} | |
if( array_key_exists('adsence', $_GET) && $_GET['adsence'] !== '') { | |
$meta_query[] = array( | |
'key' => 'adsence', | |
'value' => $_GET['adsence'], | |
'compare' => '==' | |
); | |
} | |
if(count($meta_query) > 1) { | |
$meta_query['relation'] = 'AND'; | |
} | |
if(count($meta_query) > 0) { | |
$query->set('meta_query', $meta_query); | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment