Support

Account

Home Forums Backend Issues (wp-admin) Retrieve POST OBJECT values for admin column filter

Unread

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?

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

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.