开发者

Wordpress - Sort posts depending on value of Custom Field and alphabetically

My client has a website listing different sailing services. These services have different levels of subscription:

  • Free
  • Premium
  • Full

In the back end, I have set up some Custom Fields using Advanced Custom Fields (which work perfectly as normal WP custom fields). The Custom Field for that service is called "subscription_level" and it's a dropdown list with the options mentioned above.

What I'm trying to achieve is to display the posts in the order of the level of subscription and alphabetically. So first, you'd have all the "Full" services sorted by title, then the "Premium" and then the "Free" ones.

Like:

---- Full ---

Full Service A

Full Service B

Full Service C

---- Premium ---

Premium Service A

Premium Service B

Premium Service C

---- Free ---

Free Service A

Free Service B

Free Service C

Ideally this should also have pagination, but if that's not possible, it's not the end of the world.

I have tried several custom select queries by looking at the Codex instructions, without luck. My problem is I need two ways of "orderby"? One that divides the posts according to the meta_value of a my custom field "subscription_level" and at the same time one that orders each level of subscription by title... and I just can't seem to find a way of achieving this.

Any ideas pointing me in the right direction would be开发者_JAVA技巧 awesome!

Thanks in advance


Hope I understand the problem:

  • You have a post_type "services" (or this actually are your standard posts)
  • each of these services has a subscription level like "free", "premium", or "full" stored in a custom field

So the first thing to do is categorizing the posts into these levels.

Afterwards inside these levels the posts should be ordered according to their title ( or other).

I am not sure if it is easy to achieve this with one custom SQL-query. It suppose it would be something like this:

SELECT * FROM wp_posts,wp_postmeta.meta_value INNER JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id WHERE wp_posts.post_status='publish' AND wp_postmeta.meta_key = "service" ORDER BY wp_postmeta.meta_value, wp_posts.post_title, ASC

But if this gets to complicated you can chose another way to achieve this aim. Collect the IDs of all "Full service" posts, the IDs of all "Premium service" posts and the IDs of all "Free service" posts, f.e.

SELECT wp_posts.post_ID FROM wp_posts INNER JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id WHERE wp_postmeta.meta_key = "service" and wp_postmeta.meta_value="premium"

(do this for each kind of service)

Afterwards you run WP_QUERY with the 'posts_in'-array or another custom SQL-Query where you order the collected posts by title or a new value.

Depending on what kind of paging you want to use, there is a blog post about how you can integrate custom paging in a simple way yourself:

http://www.blackbam.at/blackbams-blog/2011/07/18/implementing-a-simple-and-flexible-paging-algorithm-using-php/

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜