2

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 ...

1 Answer 1

2

You can add more than one filter call, so each of your custom meta_key's can be handled in separate code and when executed will cumulatively adjust the query.

Perhaps look at the pre_get_posts filter rather than parse_query (although the solution I'm suggesting could possibly work in either case). pre_get_posts gives you access to the current query before it is executed by WordPress, and you will limit the posts that the query will fetch from the database.

I haven't tested this code: it's just an edit here against your code to show you how you could approach this (so please check the syntax for errors).

You'd end up with something like this:

add_filter('pre_get_posts', 'wpse45436_property_baths_posts_filter');

function wpse45436_property_baths_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'];
    }
}

add_filter('pre_get_posts', 'wpse45436_property_beds_posts_filter');

function wpse45436_property_beds_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_beds']) && $_GET['property_beds'] != '') {

        $query->query_vars['meta_key'] = 'property_beds';
        $query->query_vars['meta_value'] = $_GET['property_beds'];
    }
}

Check the Codex for further details on the pre_get_posts filter

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.