I haven’t tested the code, but this should help.
option
elements do not have a name
attribute:
<option name="top-views" value="top-views">Top Views</option>
<option name="top-likes" value="top-likes">Top Likes</option>
And therefore $_POST['top-views']
for example, is not going to be set, or that the value is not the top-views
in the above option
.
Use the proper option
markup
Based on the following markup:
<option value="comment_count-DESC">Comments ↓</option>
Your custom option
should be:
<option value="{key}-{order}">{label}</option>
So (to sort in DESCending order):
<option value="top_views-DESC">Top Views</option>
<option value="top_likes-DESC">Top Likes</option>
Note that the {key}
should not have a dash (-
).
Parsing the sorting {key}
and {order}
In the misha_insights_filter_function()
function, the sorting {key}
(i.e. the orderby
parameter) and {order}
are parsed like so:
$order = explode( '-', $_POST['misha_order_by'] );
So for example, if “Top Views” was selected, and that the option
value was top_views-DESC
, then $order[0]
would be top_views
and $order[1]
would be DESC
.
And you can then customize the $param
like so:
if ( 'top_views' === $order[0] ) {
$params['meta_key'] = 'post_views_count';
$params['orderby'] = 'meta_value_num';
}
Similarly, if “Top Likes” was selected, and that the option
value was top_likes-DESC
, you can just copy-paste the above if
block and replace the relevant values:
if ( 'top_likes' === $order[0] ) {
$params['meta_key'] = '_post_like_count';
$params['orderby'] = 'meta_value_num';
}