Support

Account

Home Forums General Issues Bulk Action for Updating Bidirectional Relationships

Unread

Bulk Action for Updating Bidirectional Relationships

    • Andreas

    • September 29, 2020 at 3:43 am

    Hello,
    I have a relationship field with a group and several post types, all of which share this same field. By using the below example, I was able to add bidirectional relations to saving the post.

    https://www.advancedcustomfields.com/resources/bidirectional-relationships/

    However, a large portion of the posts will be imported, and thought the import plugin creates the relation between Post A and Post B, to create the association between Post B and Post A, I would have to go to either and “save” the post.

    To avoid this task, I would like to create a custom bulk action that will use the same ACF example but in a bulk manner.
    Bellow is the skeleton code. Can you please advise if this is possible? All my attempts in doing so have failed. The update_field always returns true, but no update occurs.

    add_filter( 'bulk_actions-edit-people', 'custom_rel_update_bulk_action' );
    
    function custom_rel_update_bulk_action( $bulk_array ) {
    
    	$bulk_array['bulk_update_rels'] = 'Update Relationships';
    	return $bulk_array;
    
    }
    
    add_filter( 'handle_bulk_actions-edit-people', 'custom_rel_update_bulk_action_handler', 10, 3 );
    
    function custom_rel_update_bulk_action_handler( $redirect, $doaction, $object_ids ) {
    
    	// let's remove query args first
    	$redirect = remove_query_arg( array( 'bulk_update_rels_done' ), $redirect );
    
    	// do something for "Make Draft" bulk action
    	if ( $doaction == 'bulk_update_rels' ) {
    
    		foreach ( $object_ids as $post_id ) {
    
                //https://www.advancedcustomfields.com/resources/bidirectional-relationships/
    
    		}
    
    		// do not forget to add query args to URL because we will show notices later
    		$redirect = add_query_arg(
    			'bulk_update_rels_done', // just a parameter for URL (we will use $_GET['bulk_update_rels_done'] )
    			count( $object_ids ), // parameter value - how much posts have been affected
    		$redirect );
    
    	}
    
    	return $redirect;
    
    }
    add_action( 'admin_notices', 'bulk_update_rels_notices' );
    
    function bulk_update_rels_notices() {
    
    	// first of all we have to make a message,
    	// of course it could be just "Posts updated." like this:
    	if ( ! empty( $_REQUEST['bulk_update_rels_done'] ) ) {
    		echo '<div id="message" class="updated notice is-dismissible">
    			<p>Posts updated.</p>
    		</div>';
    	}
    
    }

    Advanced Custom Fields PRO 5.9.1

    Thank you very much

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.