A solução está nesta resposta do WordPress Developers:
How to filter post listing (in WP dashboard posts listing) using a custom field (search functionality)?
Primeiro, é preciso adicionar o dropdown e o input de texto, ajustando o Post Type na comparação com $typenow
:
/**
* Adicionar filtro de Custom Fields na tela /wp-admin/edit.php
*/
add_action( 'restrict_manage_posts', 'ba_admin_posts_filter_restrict_manage_posts' );
function ba_admin_posts_filter_restrict_manage_posts()
{
global $wpdb, $typenow;
if( $typenow !== 'post' ) // AJUSTE SEU POST TYPE
return;
$sql = 'SELECT DISTINCT meta_key FROM '.$wpdb->postmeta.' ORDER BY 1';
$fields = $wpdb->get_results( $sql, ARRAY_N );
?>
<select name="ADMIN_FILTER_FIELD_NAME">
<option value=""><?php _e('Filter By Custom Fields', 'baapf'); ?></option>
<?php
$current = isset( $_GET['ADMIN_FILTER_FIELD_NAME'] ) ? $_GET['ADMIN_FILTER_FIELD_NAME'] : '';
$current_v = isset( $_GET['ADMIN_FILTER_FIELD_VALUE'] ) ? $_GET['ADMIN_FILTER_FIELD_VALUE'] : '';
foreach ( $fields as $field )
{
# Hidden fields
if ( substr( $field[0], 0, 1 ) != "_" )
{
printf(
'<option value="%s"%s>%s</option>',
$field[0],
$field[0] == $current ? ' selected="selected"' : '',
$field[0]
);
}
}
?>
</select>
<?php _e('Value:', 'baapf'); ?>
<input type="TEXT" name="ADMIN_FILTER_FIELD_VALUE" value="<?php echo $current_v; ?>" />
<?php
}
Depois, adicionar o filtro quando a pesquisa acontecer dentro da página /wp-admin/edit.php
e o $_GET
correto estiver setado:
/**
* Fazer a filtragem conforme o $_GET
*/
add_filter( 'parse_query', 'ba_admin_posts_filter' );
function ba_admin_posts_filter( $query )
{
global $pagenow;
$get_url = isset($_GET['ADMIN_FILTER_FIELD_NAME']) && $_GET['ADMIN_FILTER_FIELD_NAME'] != '';
if ( is_admin() && $pagenow=='edit.php' && $get_url )
{
$query->query_vars['meta_key'] = $_GET['ADMIN_FILTER_FIELD_NAME'];
if (isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '')
$query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE'];
}
}
Note que a busca é literal pelo "Value" e que os fields com arrays serializadas não são considerados.
Testei esta outra solução para fazer a filtragem e parece que funciona com fields serializados (testes mais profundos são necessários):
add_filter( 'posts_clauses', 'wpse143477_parse_clauses', 10, 2 );
function wpse143477_parse_clauses($clauses, $query) {
global $pagenow, $wpdb;
$get_url = isset($_GET['ADMIN_FILTER_FIELD_NAME']) && $_GET['ADMIN_FILTER_FIELD_NAME'] != '';
if ( is_admin() && $pagenow=='edit.php' && $get_url )
{
$keyword = $_GET['ADMIN_FILTER_FIELD_VALUE'];
$keyword = '%' . like_escape( $keyword ) . '%';
$post_ids = $wpdb->get_col( $wpdb->prepare( "
SELECT DISTINCT post_id FROM {$wpdb->postmeta}
WHERE meta_key = '%s' AND meta_value LIKE '%s'
", $_GET['ADMIN_FILTER_FIELD_NAME'], $keyword ) );
if( !empty( $post_ids )) {
$clauses['where'] .= " AND {$wpdb->posts}.ID IN (" . implode( ', ', $post_ids ) . ')';
}
}
return $clauses;
}
PS: Esse tipo de código pode ir functions.php
, mas é mais adequado como um plugin.