You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Yes, it is possible to sort the results by multiple meta fields using Corcel. Here's an example of how you can modify your code to achieve that:
Post::hasMeta(["is_available" => true])
->status("publish")
->type("post")
->join("postmeta as pm1", function ($join) {
$join->on("pm1.post_id", "=", "posts.ID")
->where("pm1.meta_key", "visits");
})
->join("postmeta as pm2", function ($join) {
$join->on("pm2.post_id", "=", "posts.ID")
->where("pm2.meta_key", "product_name");
})
->orderBy("pm1.meta_value", "desc")
->orderBy("pm2.meta_value", "asc")
->get();
In this code, we are performing two separate joins on the postmeta table, one for the visits meta key and another for the product_name meta key. Then we use the orderBy method to specify the order of sorting for each meta field.
Make sure to adjust the table alias (pm1 and pm2) and the meta key names (visits and product_name) according to your actual database schema.
With this modification, the results will be sorted by visits in descending order and then by product_name in ascending order.
Hello,
is it possible to sort the results by multiple meta fields?
Current code:
But I want to order it by
visits DESC, product_name ASC
.Is there any way?
The text was updated successfully, but these errors were encountered: