Home › Forums › General Issues › Query options in post_object field – only choose same value once in all shop_ord
Hey guys hope you can help me in the right direction with my problem.
I have a webshop. In my shop_orders i have made af ACF post_object select field in which I can choose multiple shop_orders from.
But I only want to be able to choose a shop_order from the field one time, so if I choose the shop_orders with IDs #1 and #2 then I dont want to be able to choose IDs #1 and #2 again anywhere (or at least not in the same field in the other shop_orders).
I have played around a bit with this filter:
https://www.advancedcustomfields.com/resources/acf-fields-post_object-query/
And I understand how I can manually exclude certain shop_orders from the post_object select field with $args[‘exclude’].
But i want to automaticaly loop through all the shop_orders and check which values has be chosen in the different shop_orders and then put them all in the $args[‘exclude’] so those values cant be chosen again in any other shop_order.
But I dont know how I can loop through all the shop_orders inside the post_object query filter.
Here is an explanation of how the code could look like but it is just for give an idea of what I want to achieve, it is not a working code in any way:
I dont expect that someone wants to write the code for me. All I need is just to be pointed in the right direction and to get some links to things I can read or something. Thank you.
function my_post_object_query( $args, $field, $post_id ) {
// exclude all shop_orders with certain IDs
$args['exclude'] = ''; // '1,2,3,4' IDs of shop_orders to exclude
// get all shop_orders
$shop_orders = get_posts(array('post_type'=>'shop_order'));
// loop through all shop_orders
foreach( $shop_orders as $shop_order ):
// get the Post Object field inside each shop_order
$shop_order_field = get_field( 'my_field', $shop_order->ID );
$shop_order_field_values = ??????;// get selected values from the field somehow
// for each value from the field
foreach( $shop_order_field_values as $val ):
$args['exclude'] .= $val . ','; // put the selected values into the exclude argument
endforeach;
endforeach;
// now i return the arguments which will filter away all the shop_orders that has already been selected in the my_field in any of the shop_orders
return $args;
};
add_filter('acf/fields/post_object/query/name=my_field', 'my_post_object_query', 10, 3);
I think I should use wc_get_orders() instead of get_posts() in my code:
https://github.com/woocommerce/woocommerce/wiki/wc_get_orders-and-WC_Order_Query
I will try it tomorrow.
But any help is still much appreciated.
The topic ‘Query options in post_object field – only choose same value once in all shop_ord’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.