WordPress.org

Ready to get started?Download WordPress

Forums

Order posts by percentage discount value between two meta key values (6 posts)

  1. gomymusic
    Member
    Posted 1 year ago #

    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?

  2. esmi
    Forum Moderator
    Posted 1 year ago #

  3. gomymusic
    Member
    Posted 1 year ago #

    There is no examples or documentation for doing that.

  4. Rachel Carden
    Member
    Posted 1 year ago #

    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.

  5. gomymusic
    Member
    Posted 1 year ago #

    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

  6. gomymusic
    Member
    Posted 1 year ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic