我有一个自定义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产品列表屏幕(支持)中显示了_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 :(得分:0)
长答案 - 基本上,如果您需要对meta_value进行排序,则无法将其存储为序列化。检查https://wordpress.stackexchange.com/questions/87265/order-by-meta-value-serialized-array。
我认为最适合您的解决方案是将相关产品的数量存储在新的meta_key中,并使用它来对列进行排序。
如果列包含普通数据而不是序列化数组,则下面是对数据进行排序的步骤。
实际上有两个步骤可以使自定义列可排序
要将列注册为可排序,请使用manage_{YOUR_SCREEN_ID}_sortable_columns
过滤器并添加列
您已使用related_product_col_sort
函数注册了您的列,以根据数据类型实现排序功能。
如果数据是数字或简单字母,您可以使用pre_get_posts
操作并设置meta_key
和orderby
值
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;
}