admin管理员组文章数量:1122832
I have found several posts like my question but never really found a good answer.
I have ACF fields on Posts. One of them is a radio, with 3 values (For Sale, Pending and Sold).
I have used a Query to order them:
add_action( 'elementor/query/trier_articles', function( $query ) {
$query->set( 'meta_key', 'status' );
$query->set( 'orderby', array('meta_value' => 'ASC', 'date' => 'DESC') );
$query->set( 'order', 'ASC' );
});
It's working, but now I would like to order them like this : Pending -> For Sale -> Sold.
Could I somehow use meta_key ? Or is there a way to process each meta_value to make it work like I want?
Thank you!
I have found several posts like my question but never really found a good answer.
I have ACF fields on Posts. One of them is a radio, with 3 values (For Sale, Pending and Sold).
I have used a Query to order them:
add_action( 'elementor/query/trier_articles', function( $query ) {
$query->set( 'meta_key', 'status' );
$query->set( 'orderby', array('meta_value' => 'ASC', 'date' => 'DESC') );
$query->set( 'order', 'ASC' );
});
It's working, but now I would like to order them like this : Pending -> For Sale -> Sold.
Could I somehow use meta_key ? Or is there a way to process each meta_value to make it work like I want?
Thank you!
Share Improve this question asked Apr 12, 2024 at 10:26 david simondavid simon 133 bronze badges1 Answer
Reset to default 0Don't think the effort worth it to do it with generating appropriate mysql statements as you can just use three queries, or sort the posts after you have the results of the query with both ways resulting in a mostly readable code. Main reason not to choose the above is if performance is critical.
Another option is to use some different meta field and store some integer value representing the 3 states, but if you are using a plugin that generates the property value status this might be hard work to figure all the place in which the new meta field can be added.
And if you feel like a mysql guru you can probably order by the value of FIND_IN_SET. You will need to figure out which filter to use to override the query. One pointer is this snippet - https://gist.github.com/mikeschinkel/6402058#file-find-in-set-php but I didn't inspect that code.
本文标签: Custom WP Query with neither ASC or DESC
版权声明:本文标题:Custom WP Query with neither ASC or DESC 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736310599a1934436.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论