The article discusses how to add a filter function by Meta Fields in the WordPress dashboard. It provides step-by-step instructions on adding Meta Field columns to the post list display section and creating a filter box to display and filter values based on Meta Fields. The code snippets provided need to be added to the functions.php file in the theme in use. The article also includes notes on customizing the code for different post types and Meta Field values. The reader is encouraged to try filtering with a value to ensure the correct results.
Today, I’ll be talking about how to Add filter function by Meta Fields in the WordPress Dashboard. Many of you have messaged me about this topic recently, and I appreciate your patience as I get to addressing it.
I. Add Meta Field column to the post list display section
To make this happen, you’ll need to add a code snippet to the functions.php file in the current Theme being used.
function vnkings_add_column_field($defaults) {
global $typenow;
if ( $typenow == 'san-pham' ) {
$defaults['metafield'] = 'Mã Field';
return $defaults;
}
}
function vnkings_get_field_value($columnName, $postID) {
if ($columnName == 'metafield') {
$mafield = get_post_meta($postID, 'vnkings_post', true);
echo $mafield;
}
}
add_filter('manage_posts_columns', 'vnkings_add_column_field');
add_action('manage_posts_custom_column', 'vnkings_get_field_value', 10, 2);
Note:
- You need to adjust product to match your Post type.
- vnkings_post represents the Meta Field that needs to be displayed.
Result I: Check image here
II. Add Meta Fields filter box and display the values to be filtered
In this step, you’ll include another code snippet in the functions.php file of the current Theme.
// Thêm ô lọc Meta Fields
function vnkings_add_filter_dropdown() {
global $typenow;
global $wp_query;
if ( $typenow == 'san-pham' ) {
$field_value = array( 'giam_gia','khong_giam_gia' );
$current_field_value="";
if( isset( $_GET['ma_field'] ) ) {
$current_field_value = $_GET['ma_field'];
} ?>
<select name="ma_field" ><option value="all" selected>>Lọc Meta Fields</option><?php foreach( $field_value as $key=>$value ) { ?>
<option value="<?php echo $value; ?>" selected>><?php echo esc_attr( $value ); ?></option><?php } ?></select><?php }
}
add_action( 'restrict_manage_posts', 'vnkings_add_filter_dropdown' );
// Kết quả tìm kiếm Meta field
function vnkings_search_metafield( $query ) {
global $pagenow;
$post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : '';
if ( is_admin() && $pagenow=='edit.php' && $post_type == 'san-pham' && isset( $_GET['ma_field'] ) && $_GET['ma_field'] != 'all' ) {
$query->query_vars['meta_key'] = 'vnkings_post';
$query->query_vars['meta_value'] = $_GET['ma_field'];
$query->query_vars['meta_compare'] = '=';
}
}
add_filter( 'parse_query', 'vnkings_search_metafield' );
Note:
- discount and no discount represent the values of the vnkings_post field for filtering.
Result II: Check image here
Feel free to experiment with the filtering values to see the results on your WordPress Dashboard. Thank you for always supporting Vnkings, and best of luck with implementing these functions!