Are select wordpress filters and column sort filters compatible?

0

I'm doing a plugin with CPT and meta_keys with their respective value (meta_value). There is a numeric meta_key for which I want the edit.php table to be organized and it works correctly for me. I have also created some filters to filter through some text fields, and they also work correctly.

The question and doubt arises when filter with the selects and the result that is seen in the table I try to organize it by the numeric meta_key , since the filter works but it does not order it by the number field .

Is it possible that it is because of the query that is made? Or can it be due to the fact that both filters are not compatible?

This is the code in case you understand it better:

/ * Select filter in the WP administration table * /

//Hacemos el select del string Month
function month_admin_posts_filter_restrict_manage_posts(){
    global $wpdb;
    $type = ( isset($_GET['post_type']) )? $_GET['post_type'] : 'post';

  if($type == 'custom-post'){
        $values = $wpdb->get_results( "SELECT DISTINCT meta_value FROM ".$wpdb->prefix."postmeta WHERE meta_key = '_month_field'");
    ?>
    <select name="ADMIN_FILTER_FIELD_VALUE">
            <option value=""><?php _e('Filter by: Month', 'mgm'); ?></option>
        <?php $current_v = isset($_GET['ADMIN_FILTER_FIELD_VALUE'])? $_GET['ADMIN_FILTER_FIELD_VALUE']:'';
                foreach ($values as $value) {
                    $s = ($value->meta_value == $current_v)? ' selected="selected"':'';
                    echo '<option value="'.$value->meta_value.'" '.$s.'>'.$value->meta_value.'</option>';
        }
        ?>
    </select>
<?php }
}
add_action( 'restrict_manage_posts', 'month_admin_posts_filter_restrict_manage_posts' );


//Filtro para mostrar por pantalla el listado resultante de la seleccion de select anterior creado
function month_posts_filter( $query ){
    global $pagenow;
    $type = ( isset($_GET['post_type']) )? $_GET['post_type'] : 'post';

    if( $type == 'custom-post' && is_admin() && $pagenow=='edit.php' && isset($_GET['ADMIN_FILTER_FIELD_VALUE']) && $_GET['ADMIN_FILTER_FIELD_VALUE'] != '') {
        $query->query_vars['meta_key'] = '_month_field';
        $query->query_vars['meta_value'] = $_GET['ADMIN_FILTER_FIELD_VALUE'];
  }
}
add_filter( 'parse_query', 'month_posts_filter' );

/ * Custom Columns * /

// Añadir las columnas personalizadas a la array de columnas del tipo post channel (Admin)
function channels_list_columns($columns){

    $columns['channel_month'] = esc_html__('Month', 'mgm');
    $columns['channel_ordering'] = esc_html__('Order Position', 'mgm');

    return $columns;
}
add_filter('manage_edit-channel_columns', 'channels_list_columns');


//Mostramos las nuevas columnas para el listado
function display_channels_list_columns( $column ){
    global $post;

    if($column == 'channel_ordering' ){
        $order = get_post_meta($post->ID,'_ordering_field');
        echo $order[0];
    }
}
add_action('manage_channel_posts_custom_column',  'display_channels_list_columns',10,2);


//Hacemos que sea organizable los siguientes campos del listado al hacer click sobre los nombres de los campos
function sortable_channel_columns_list( $array_ordering ){

    return wp_parse_args( array(
        'channel_ordering'      => esc_html__('Order Position', 'mgm');
    ), $array_ordering );
}
add_filter('manage_edit-channel_sortable_columns', 'sortable_channel_columns_list');



function load_channels() {
    add_filter( 'request', 'sortableby_numb_field' );
}
add_action( 'load-edit.php', 'load_channels' );



function sortableby_numb_field( $query_vars ) {

  if ( isset( $query_vars['post_type'] ) && 'channel' == $query_vars['post_type'] ) {

    if ( isset( $query_vars['orderby'] ) && $query_vars['orderby']== 'Order Position' ){
            $query_vars = array_merge(
                $query_vars,
                array(
                    'meta_key' => '_ordering_field',
                    'orderby' => 'meta_value_num'
                )
            );
    }
    return $query_vars;
    }
}

Thank you for your time, greetings.

    
asked by PiP 04.06.2018 в 13:46
source

0 answers