I have created a custom post and I have added two new fields (year, month) and a filter for each of those fields. The thing is that if I filter by one (year or month) and I give the filter button it filters me well and if I then add the second filter and filter it, it passes me. And if I put the two filters from the beginning and give it to filter it does not work either.
This is the code of one of the filters, I do not put the two because they are the same except for the name of the select.
//crea el filtro select encima de la tabla
function year_admin_posts_filter_restrict_manage_posts(){
global $wpdb;
$type = 'post';
if(isset($_GET['post_type'])) {
$type = $_GET['post_type'];
}
if( $type == 'custom-post' ){
$values = $wpdb->get_results( "SELECT DISTINCT meta_value FROM ".$wpdb->prefix."postmeta WHERE meta_key = '_year_field'");
?>
<select name="admin_filter_year">
<option value=""><?php _e('Filter by: Year', 'mgm'); ?></option>
<?php $current_v = isset($_GET['admin_filter_year'])? $_GET['admin_filter_year']:'';
foreach ($values as $value) {
$s = ($value->meta_value == $current_v)? 'selected="selected"':'';
echo '<option value="'.$value->meta_value.'" '.$s.'>'.$value->meta_value.'</option>';
}
?>
</select>
<?php }
}
add_action( 'restrict_manage_posts', 'year_admin_posts_filter_restrict_manage_posts' );
//Filtro para mostrar por pantalla el listado resultante de la seleccion de select anterior creada
function audience_year_posts_filter( $query ){
global $pagenow;
$type = 'post';
if(isset($_GET['post_type'])) {
$type = $_GET['post_type'];
}
if( $type == 'channel' && is_admin() && $pagenow=='edit.php' && isset($_GET['admin_filter_year']) && $_GET['admin_filter_year'] != '') {
$query->query_vars['meta_key'] = '_year_field';
$query->query_vars['meta_value'] = $_GET['admin_filter_year'];
}
}
add_filter( 'parse_query', 'audience_year_posts_filter' );