WordPress.org

Support

Support » How-To and Troubleshooting » Order posts by percentage discount value between two meta key values

Order posts by percentage discount value between two meta key values

  • Hey,

    I want to list posts (build in posts) sorted by by percentage discount value between two meta key values.
    meta_key = price
    meta_key = lowprice
    discount = (price – lowprice) / price * 100)
    I want to order posts by this result.

    Any help?

Viewing 5 replies - 1 through 5 (of 5 total)
  • esmi

    @esmi

    Forum Moderator

    There is no examples or documentation for doing that.

    Your need is pretty custom so you’re probably going to have to hook into the query, via filter, to set this up. I would recommend the ‘posts_clauses’ filter because it gives you access to all the query clauses in one filter.

    The ‘posts_clauses’ passes two parameters: the clauses (separated into an array) and the actual query. So your filter would start out like this:

    add_filter( 'posts_clauses', 'my_website_posts_clauses_filter', 1, 2 );
    function my_website_posts_clauses_filter( $clauses, &$this ) {
       return $clauses;
    }

    The different clauses are: ‘where’, ‘groupby’, ‘join’, ‘orderby’, ‘distinct’, ‘fields’ and ‘limits’.

    You’re going to have to filter the ‘fields’ to make sure your two custom fields are being queried and then you can filter the ‘where’ to add in your discount logic.

    But make you sure remember this is a filter so you don’t want to overwrite what already exists, you’re just adding on to it.

    Hope this points you in the right direction.

    Thanks for advice, but I think is too hard for me.
    I hope someone who can do it to help me 🙂
    I still wait responses.

    Stop bumping

    I think “posts_orderby” may help me, but I dont know how to use it.
    Someone can help me?

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Order posts by percentage discount value between two meta key values’ is closed to new replies.