Home › Forums › Backend Issues (wp-admin) › Retrieve POST OBJECT values for admin column filter
Below code creates a filter option (from a select field) for the admin columns for custom post type ‘measurement’.
function yl_filter_measurement_status() {
$scr = get_current_screen();
if ( $scr->base !== 'edit' && $scr->post_type !== 'measurement') return;
$selected = filter_input(INPUT_GET, 'measurement_status', FILTER_SANITIZE_STRING );
$status_options = get_field_object('measurement_status');
$choices = $status_options['choices'];
echo '<select name="measurement_status">';
echo '<option value="all" '. (( $selected == 'all' ) ? 'selected="selected"' : "") . '>' . __( 'Filter by status', 'gasmetingonline' ) . '</option>';
foreach( $choices as $key => $value ) {
echo '<option value="' . $key . '" '. (( $selected == $key ) ? 'selected="selected"' : "") . '>' . $value . '</option>';
}
echo '</select>';
}
add_action('restrict_manage_posts', 'yl_filter_measurement_status');
function yl_filter_measurement_status_action($query) {
if ( is_admin() && $query->is_main_query() ) {
$scr = get_current_screen();
if ( $scr->base !== 'edit' && $scr->post_type !== 'measurement' ) return;
if (isset($_GET['measurement_status']) && $_GET['measurement_status'] != 'all') {
$query->set('meta_query', array( array(
'key' => 'measurement_status',
'value' => sanitize_text_field($_GET['measurement_status'])
) ) );
}
}
}
add_action('pre_get_posts','yl_filter_measurement_status_action');
Everything works great.
In this same group I have an POST OBJECT field to select the EXECUTIVE from an other custom post type (executive). When I use this above code to create a filter for executive, the filter stays empty.
This probably got to do with the values being in another group.
How can I retrieve the EXECUTIVE values to fill the filter?
You cannot filter posts by values associated with other posts. To do what you want you would need to copy all of the custom field values you want to filter on to the posts that you want to filter. The only thing that is stored for a post object field is the ID of the related post.
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!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.