$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - Creating search filter through plugin|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - Creating search filter through plugin

matteradmin11PV0评论

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

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

Share Improve this question edited Jun 13, 2016 at 9:27 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Jun 13, 2016 at 7:33 Vinícius ÓliverVinícius Óliver 213 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far