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 badges
Add a comment  | 

1 Answer 1

Reset to default 0

Don'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