I'm using wordpress for real estate project, for that, I am using a theme together with a plugin (realia real estate) to real estate. So I believe that this not is beside the point ... My question is simple, I have several custom meta_key such as property_baths (bathrooms), property_beds (rooms) and etc ... What I want to do is create a filter, an advanced search in the wordpress management, in the "Posts" tab. I need also that this search be cumulative, for example, if the "bathrooms" and the "rooms" are both filled, should filter posts according to what has been filled.
After much searching I found a way to do this by creating a plugin, but the code that I have here, it is passing only only 1 meta_key with your meta_value, I need it to pass several meta_key with their values ...
What I have so far is this:
add_action( 'restrict_manage_posts', 'wpse45436_admin_posts_filter_restrict_manage_posts' );
function wpse45436_admin_posts_filter_restrict_manage_posts(){
$type = 'property';
if (isset($_GET['post_type'])) {
$type = $_GET['post_type'];
}
//only add filter to post type you want
if ('property' == $type){
//change this to the list of values you want to show
//in 'label' => 'value' format
$values = array(
'Banheiros' => 'property_baths',
'Quartos' => 'property_beds'
);
$current_v = isset($_GET['ADMIN_FILTER_FIELD_VALUE'])? $_GET['ADMIN_FILTER_FIELD_VALUE']:'';
foreach ($values as $label => $value) {
printf
(
'<input type="text" name="%s"%s placeholder="%s"></option>',
$value,
$value == $current_v? ' value="'.$current_v.'"':'',
$label
);
}
}
}
add_filter('parse_query', 'wpse45436_posts_filter');
function wpse45436_posts_filter( $query ){
global $pagenow;
$type = 'property';
if (isset($_GET['post_type'])) {
$type = $_GET['post_type'];
}
if ( 'property' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['property_baths']) && $_GET['property_baths'] != '') {
$query->query_vars['meta_key'] = 'property_baths';
$query->query_vars['meta_value'] = $_GET['property_baths'];
}
}
With this code (plugin) I can filter only 1 meta_key, I need to do in a way that I can use several "meta_keys" with their values.
I've tried loop, I tried array, but nothing worked. I hope someone can help me ... Thanks in advance ...