2016-12-29 50 views
1

我有一個自定義postmeta字段,它以序列化方式存儲post/product_id。示例_related_ids =>a:4:{i:0;i:2462;i:1;i:2466;i:2;i:2469;i:3;i:2472;}WooCommerce自定義產品列可排序支持不工作

我在WooCommerce產品清單屏幕(支持)中顯示_related_ids的product_id計數,該工作正常。現在我想讓這個列可排序。所以我寫了一個函數related_product_col_sort,這個函數連接到manage_edit-product_sortable_columns。列短路不起作用。 (不工作意味着它沒有正確訂購product_id計數)

這裏是我完整的代碼

//_related_ids => a:4:{i:0;i:2462;i:1;i:2466;i:2;i:2469;i:3;i:2472;} 
//manage_product_posts_custom_column 
add_filter('manage_edit-product_columns', 'related_product_col'); 

function related_product_col($columns) { 
    $new_columns = (is_array($columns)) ? $columns : array(); 
    $new_columns['RELATED'] = 'Related Product'; 
    return $new_columns; 
} 

add_action('manage_product_posts_custom_column', 'related_product_col_data', 2); 

function related_product_col_data($column) { 
    global $post; 
    $related_product_ids = get_post_meta($post->ID, '_related_ids', true); 
    if ($column == 'RELATED') { 
     if (isset($related_product_ids) && !empty($related_product_ids)) { 
      echo count($related_product_ids); 
     } else { 
      echo "--"; 
     } 
    } 
} 

add_filter("manage_edit-product_sortable_columns", 'related_product_col_sort'); 

function related_product_col_sort($columns) { 
    $custom = array(
     'RELATED' => '_related_ids' 
    ); 
    return wp_parse_args($custom, $columns); 
} 

誰能幫我用related_product_col_sort功能正確的邏輯/代碼。

謝謝。

回答

0

Long答案總之 - 基本上,如果您需要對meta_value進行排序,則無法將其序列化。檢查https://wordpress.stackexchange.com/questions/87265/order-by-meta-value-serialized-array

我認爲最適合您的解決方案是將相關產品的數量存儲在新的meta_key中,並使用它對列進行排序。

如果列包含普通數據而不是序列化數組,則下面是對數據進行排序的步驟。

實際上有兩個步驟,以使自定義列排序

  1. 註冊爲排序
  2. 實現排序邏輯

要註冊列排序您使用manage_{YOUR_SCREEN_ID}_sortable_columns過濾器列並添加您的列

您已使用related_product_col_sort函數註冊您的列,以執行根據數據類型的不同,排序功能有幾種方法。

如果數據是數字或簡單的字母可以使用pre_get_posts行動,並設置meta_keyorderby

add_action('pre_get_posts', 'manage_wp_posts_my_custom_pre_get_posts', 1); 
function manage_wp_posts_my_custom_pre_get_posts($query) { 

    /** 
    * We only want our code to run in the main WP query 
    * AND if an orderby query variable is designated. 
    */ 
    if ($query->is_main_query() && ($orderby = $query->get('orderby'))) { 

     switch($orderby) { 

     // If we're ordering by 'film_rating' 
     case 'RELATED': 

      // set our query's meta_key, which is used for custom fields 
      $query->set('meta_key', 'RELATED'); 

      /** 
      * Tell the query to order by our custom field/meta_key's 
      * This will only work is the meta_value for the RELATED key is either a number or a simple string 
      */ 
      $query->set('orderby', 'meta_value'); 

      break; 

     } 

    } 
} 

如果你的排序值是比較複雜的像一個日期

add_filter('posts_clauses', 'manage_wp_posts_my_custom_posts_clauses', 1, 2); 
function manage_wp_posts_my_custom_posts_clauses($pieces, $query) { 
    global $wpdb; 

    /** 
    * We only want our code to run in the main WP query 
    * AND if an orderby query variable is designated. 
    */ 
    if ($query->is_main_query() && ($orderby = $query->get('orderby'))) { 

     // Get the order query variable - ASC or DESC 
     $order = strtoupper($query->get('order')); 

     // Make sure the order setting qualifies. If not, set default as ASC 
     if (! in_array($order, array('ASC', 'DESC'))) 
     $order = 'ASC'; 

     switch($orderby) { 

     // If we're ordering by release_date 
     case 'RELATED': 

      /** 
      * We have to join the postmeta table to 
      * include our release date in the query. 
      */ 
      $pieces[ 'join' ] .= " LEFT JOIN $wpdb->postmeta wp_rd ON wp_rd.post_id = {$wpdb->posts}.ID AND wp_rd.meta_key = '_related_ids'"; 

      // This will only work if the meta_value is a date. 
      $pieces[ 'orderby' ] = "STR_TO_DATE(wp_rd.meta_value,'%m/%d/%Y') $order, " . $pieces[ 'orderby' ]; 

     break; 

     } 

    } 

    return $pieces; 

} 
+0

我想我明白了我錯在哪裏,會嘗試一下並且會更新你。而且我正在使用相關產品插件以序列化方式存儲值,我想我必須編輯該插件以使排序邏輯更容易。謝謝。 –

相關問題