Skip Navigation

[Resolved] toolset_get_related_posts() with condition query

This support ticket is created 2 years, 1 month ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 1 reply, has 2 voices.

Last updated by Luo Yang 2 years, 1 month ago.

Assisted by: Luo Yang.

Author
Posts
#2480535
Screenshot 2022-10-21 at 3.04.35 PM.png

Dear Sir/Madam,

Refer to the screenshot, a member purchase several coupons package, how can I get the one coupons with below condition
1. ignore all balance is 0
2. the earliest purchase date

How can I add above query to the tooset_get_related_posts()?

        $child_posts = toolset_get_related_posts( $_REQUEST['m'], 'member-coupon', array( 'query_by_role' => 'parent', 'return' => 'post_object', 'limit'=> 1));
        foreach ($child_posts as $child_post) {
            
            $this_post_id = $child_post->ID;
            
            $this_total_coupons = types_render_field('coupon-purchased-quota', array("id"=> "$child_post->ID"));
            $this_coupon_balance = types_render_field('coupon-quota-balance', array("id"=> "$child_post->ID"));
            $this_used_coupons = $this_total_coupons - $this_coupon_balance;
            
            $this_coupon = new Coupon($_REQUEST['m'], $this_total_coupons, $this_coupon_balance);
            $this_coupon->set_image('/images/stamp.png');
            $has_coupon = true;
        }

#2482307

Hello,

You can follow our document to setup the WP query:
https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/

See the example in section "NEW APPROACH", and you can follow WP document to setup other filter parameters and order by parameters:
https://developer.wordpress.org/reference/classes/wp_query/