Home › Forums › General Issues › Querying custom field with bidirectional relationship
Hello, I have a custom field called ‘linked_requests’ that’s stored in wp_postmeta. The value stored in this field is a serialized array (format -> a:1:{i:0;s:3:”123″;i:1;s:3:”124″;}). The existing values were made using the function “bidirectional_acf_update_value” described in:
https://www.advancedcustomfields.com/resources/bidirectional-relationships/
I want to make an automatic matching system, instead of the one above that requires field values to be manually selected when editing a post.
So now I’m trying to loop through wp_postmeta and see if a post id in this table and the current user’s post id contain 4 fields with the same value. If they have matching values, then a serialized array should be made in field ‘linked_requests’ containing the current user’s post id. The serialized array should be able to contain multiple post id’s (like the example used above).
But, if the ‘linked_requests’ field already contains a serialized array but does not contain the current user’s post id, then I would like to add the current’s user’s post id to the existing serialized array. How would I go about reading and updating this array?
I’m really not sure exactly what you are doing. I’m not even sure what you mean by
loop through wp_postmeta and see if a post id in this table and the current user’s post id contain 4 fields with the same value.
Can you explain further or supply any code that you are attempting to use?
Yeah sorry, I’m new to the plugin. I’ll try to explain it better.
So, the developer before me implemented Gravity Forms and ACF in the website that I’m currently working on. The data of these gravity forms are stored in wp_postmeta. 2 of those fields are text data fields, 2 other fields contain a date (a begin date and an end date).
Now, there is a custom field that was made using ACF (called linked_requests). This field’s value can currently only be set via editing a post manually (using the bidirectional relationship function of ACF). The custom field contains other post id’s that match based on a couple criteria. The criteria is based on the 2 text data fields having to containing the exact same value, and the begin date and end date have to be within the same timeframe as well when iterating through the posts.
My current approach is iterating through the posts, then use get_post_meta to get the meta_value (based on the meta_key) that’s linked to the post_id:
add_action( 'gform_after_submission_1', 'link_requests_action_after_apc', 10, 2 );
function link_requests_action_after_apc( $entry, $form ) {
$requests = new WP_Query([
'post_type' => ['request'],
'orderby' => 'ID',
'order' => 'ASC',
'post_author' => 122,
]);
if ($requests->have_posts()) :
while ($requests->have_posts()) : $requests->the_post();
$preferred_driver_locations = get_post_meta('preferred_driver_locations');
$driver_type = get_post_meta('driver_type');
$authority = '';
$from_date = get_post_meta('from_date');
$until_date = get_post_meta('until_date');
endwhile;
endif;
}
Then I have to iterate through the custom field (linked_requests), to see if this is empty, contains a serialized array or if the custom field does not exist yet (for that particular post_id). If the field contains a serialized array, then I have to read it and check if the array contains the current user’s post_id. If not, then I have to add the post_id to the serialized array (so unserialize and serialize when updating the field).
How would I got about doing this? And how can I maintain that bidirectional relationship when it comes to that custom field?
You must be logged in to reply to this topic.
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.