Support

Account

Home Forums Backend Issues (wp-admin) Filter by Radio Button Value in Admin

Helping

Filter by Radio Button Value in Admin

  • Hello! I have a custom post type of People for which I’ve given a ACF radio button field of Status. I’ve been able to add Status as a filterable column using the method outlined here (http://www.elliotcondon.com/advanced-custom-fields-admin-custom-columns/), but am running into issues while attempting to add a working dropdown filter for Status utilizing the method shown here (https://wordpress.stackexchange.com/questions/45436/add-filter-menu-to-admin-list-of-posts-of-custom-type-to-filter-posts-by-custo). I was able to get as far as a filter that seemed to work, but replaced the values in the “Status” column with radio button values (such as sp_people_member) rather than the labels that are initially there (such as Lab Member). You can see my attempt at getting the code to work below.

    Any insight into how to properly create a backend filter from a radio button ACF value would be greatly appreciated! Thanks so much.

    Jessica

    add_action( 'restrict_manage_posts', 'wpse45436_admin_posts_filter_restrict_manage_posts' );
    /**
     * First create the dropdown
     * make sure to change POST_TYPE to the name of your custom post type
     * 
     * @author Ohad Raz
     * 
     * @return void
     */
    function wpse45436_admin_posts_filter_restrict_manage_posts(){
        $type = 'post';
        if (isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
        }
    
        //only add filter to post type you want
        if ('sp_people' == $type){
            //change this to the list of values you want to show
            //in 'label' => 'value' format
            $values = array(
                'Group Leader' => 'sp_people_pi', 
                'Lab Member' => 'sp_people_member',
                'Alumnus' => 'sp_people_alumnus',
                'Collaborator' => 'sp_people_collaborator'
            );
            ?>
            <select name="ADMIN_FILTER_FIELD_VALUE">
            <option value=""><?php _e('Select Status ', 'sp'); ?></option>
            <?php
                $current_v = isset($_GET['ADMIN_FILTER_FIELD_VALUE'])? $_GET['ADMIN_FILTER_FIELD_VALUE']:'';
                foreach ($values as $label => $value) {
                    printf
                        (
                            '<option value="%s"%s>%s</option>',
                            $value,
                            $value == $current_v? ' selected="selected"':'',
                            $label
                        );
                    }
            ?>
            </select>
            <?php
        }
    }
    
    add_filter( 'parse_query', 'wpse45436_posts_filter' );
    /**
     * if submitted filter by post meta
     * 
     * make sure to change META_KEY to the actual meta key
     * and POST_TYPE to the name of your custom post type
     * @author Ohad Raz
     * @param  (wp_query object) $query
     * 
     * @return Void
     */
    function wpse45436_posts_filter( $query ){
        global $pagenow;
        $type = 'post';
        if (isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
        }
        if ( 'sp_people' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '') {
            $query->query_vars['meta_key'] = 'sp_people_status';
            $query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE'];
        }
    }
  • I’ve been working with this same code, but to filter “Coupons” by “Service Type”, where service_type is a custom post type that is part of each coupon in the form of a relationship field. The one thing that i did that made this work is to add:

    $query->query_vars['meta_compare'] = 'LIKE';

    in the wpse45436_posts_filter function so it should read like this:

    function wpse45436_posts_filter( $query ){
        global $pagenow;
        $type = 'post';
        if (isset($_GET['post_type'])) {
            $type = $_GET['post_type'];
        }
        if ( 'sp_people' == $type && is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '') {
            $query->query_vars['meta_key']     = 'sp_people_status';
            $query->query_vars['meta_value']   = $_GET['ADMIN_FILTER_FIELD_VALUE'];
            $query->query_vars['meta_compare'] = 'LIKE';
        }
    }

    Hope this works for you.

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Filter by Radio Button Value in Admin’ is closed to new replies.