Home › Forums › Backend Issues (wp-admin) › 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.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🚨 The 2023 ACF Annual Survey closes tomorrow! This is your last chance to complete the survey and help guide the evolution of ACF. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 18, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.