Support

Account

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

Solving

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.

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

You must be logged in to reply to this topic.