I have a custom postmeta
field which stores the post/product_id in a serialized way. Example _related_ids
=> a:4:{i:0;i:2462;i:1;i:2466;i:2;i:2469;i:3;i:2472;}
I'm showing the product_id count of _related_ids
in WooCommerce Product Listing screen (Backed), which is working fine. Now I want to make that column sortable. So I write a function related_product_col_sort
which is hooked into manage_edit-product_sortable_columns
. Column Shorting is not working. (not working means it is not ordering the product_id count properly).
Here is my full code
//_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);
}
Can anyone help me out with the correct logic/code with related_product_col_sort
function.
Thanks.
Long Answer short - Basically, if you need to sort on a meta_value you can't store it serialized. Check https://wordpress.stackexchange.com/questions/87265/order-by-meta-value-serialized-array.
I think the best possible solution for you is to store the count of related products in a new meta_key and use that to sort the columns.
Below are the steps to sort your data if the column consists normal data instead of a serialized array.
There are actually 2 steps to make a custom column sortable
To register a column as sortable you use the
manage_{YOUR_SCREEN_ID}_sortable_columns
filter and add your columnYou have already registered your column using the
related_product_col_sort
function, to implement the sort functionality you have couple ways depending on the type of data.If the data is numeric or simple alphabets you can use the
pre_get_posts
action and set themeta_key
andorderby
valuesIf your sorting value is more complicated like a date