¿Cómo filtrar la lista de publicaciones (en la lista de publicaciones del tablero de WP) usando un campo personalizado (funcionalidad de búsqueda)?
1 respuesta
- votos
-
- 2011-05-07
Codifiqué un complemento soloparaeso ynuncapudepublicarlo:
Uso:
Enelmenú desplegabletiene una lista detodos los campospersonalizados,así que seleccioneel campoporel que deseafiltrar y haga clicen filtrar. si deseafiltrar a un valorespecífico de un campopersonalizado,seleccioneelnombre del campo,ingreseel valor que desee y haga clicen filtrar.
<?php /* Plugin Name: Admin Filter BY Custom Fields Plugin URI: http://en.bainternet.info Description: Filter posts or pages in admin by custom fields (post meta) Version: 1.0 Author: Bainternet Author URI: http://en.bainternet.info */ add_filter( 'parse_query', 'ba_admin_posts_filter' ); add_action( 'restrict_manage_posts', 'ba_admin_posts_filter_restrict_manage_posts' ); function ba_admin_posts_filter( $query ) { global $pagenow; if ( is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_NAME']) && $_GET['ADMIN_FILTER_FIELD_NAME'] != '') { $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']; } } function ba_admin_posts_filter_restrict_manage_posts() { global $wpdb; $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) { 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 }
I coded a plugin just for that and never got around to publish it :
Usage:
In the dropdown you have a list of all custom fields, so just select the field you want to filter by and click filter. if you want to filter to a specific value of a custom field then select the field's name , enter the value you want and click filter.
<?php /* Plugin Name: Admin Filter BY Custom Fields Plugin URI: http://en.bainternet.info Description: Filter posts or pages in admin by custom fields (post meta) Version: 1.0 Author: Bainternet Author URI: http://en.bainternet.info */ add_filter( 'parse_query', 'ba_admin_posts_filter' ); add_action( 'restrict_manage_posts', 'ba_admin_posts_filter_restrict_manage_posts' ); function ba_admin_posts_filter( $query ) { global $pagenow; if ( is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_NAME']) && $_GET['ADMIN_FILTER_FIELD_NAME'] != '') { $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']; } } function ba_admin_posts_filter_restrict_manage_posts() { global $wpdb; $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) { 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 }
-
¿Por quéno utilizar lafunción auxiliarpara su cuadro de selección?es decir.reemplazar `$ campo [0]==$ actual?'selected="selected"': '', `con` seleccionado ($ campo [0]==$ actual,verdadero,falso),`... :)Why not use the helper function for your selection box? ie. replace `$field[0] == $current? ' selected="selected"':'', ` with `selected( $field[0] == $current, true, false ),` ... :)
- 7
- 2011-07-18
- t31os
-
esta respuesta yanofunciona.this answer doesn't work anymore.
- 0
- 2018-07-09
- Kir Mazur
-
¿Hay algunaforma de hacer quefuncioneen las versionesmás recientes de WordPress?Is there a way to make it work in newest versions of WordPress?
- 0
- 2018-11-27
- nakkeru
Apesar de quebusquémuchoen Google,noencontré la respuesta a unapreguntamuy simple:
Tengo algunaspublicaciones con un campopersonalizado (es decir,nombre_proveedor).Megustaríapoderbuscar yfiltrarmispublicaciones de acuerdo coneste campopersonalizado.En otraspalabras,en la lista depublicaciones de administración,me gustaríatener un cuadro debúsqueda (llamado "Nombre delproveedor") dondepuedoescribir un valor (porejemplo,"IBM") y luego hacer clicen unbotón debúsqueda que daráDevuelvatodas laspublicaciones quetienen un campopersonalizado llamado "nombre_proveedor" y,sies así,el valor del campopersonalizado será "IBM".
¿Cómopuedo haceresto?