Add Custom fields to Quick Edit and Bulk Edit
The next version (3.4) of Contextual Related Posts will see a brand new feature to allow a user to bulk edit posts, pages or custom post types to add the manual related posts. Plugin users will also be able to bulk select which posts they want to exclude from the related posts lists. Before this, users would need to set these one at a time by editing the individual post.
There are limited up-to-date examples and I did turn to GitHub CoPilot several times for code. With the amount of trial and error I did over the past few days, I felt it would be a good idea to post a tutorial that should hopefully help other WordPress developers and plugin authors.
I will assume you already have an existing plugin or are comfortable adding code to your theme’s functions.php. However, I’d strongly advise creating a functional plugin at a minimum. If you use Advanced Custom Fields, you’ll find value in the code below that allows the user to bulk edit custom fields.
In this tutorial, I have configured two ACF fields called related_posts, a comma-separated field, and exclude_this_post, a True/False field.
Here’s what we will be creating today. The image below links to a video on my YouTube channelOpens in a new window.
Step 1: Add a custom admin column to the post screens
We will start by adding a single column to our post-listing screens. Our code will apply this for all public custom post types. WordPress users can hide the columns they don’t need. While you can add a column per field, we’ll keep it neat by displaying all our data in a single column.
We do this by hooking into the manage_{$post_type}_posts_columns1 filter to add the new column. And then, populate it using the manage_{$post->post_type}_posts_custom_column2 action.
Step 2: Add a custom box for Quick Edit and Bulk Edit screens
We will now add the additional fields to the Quick Edit and Bulk Edit boxes. We can do this using a single function to hook into bulk_edit_custom_box and quick_edit_custom_box.
Add these two lines to the __construct method of our class.
And then add the below code as a new method of our class.
In the code above, you’ll see that we check which filter is called using current_filter() and then display the relevant nonce. We also use it to check and display a checkbox for exclude_this_post in the Quick Edit screen and a dropdown in the Bulk Edit screen.
Step 3: Populate the Quick Edit fields
We’ll need some JavaScript to populate the fields in the Quick Edit box. You can create a new JavaScript file – I called mine bulk-edit.js.
In the above code, we fetch the status of our fields from the classes – one a div and the other an input field created by our PHP code in Step 2. We then use that to set the content of the related posts field and check/uncheck the checkbox for the exclude_this_post.
You will need to enqueue the js file using admin_enqueue_scripts. See the code below:
Step 4: Save the Quick Edit fields
Now that we’ve populated our fields in the Quick Edit mode, we will tell WordPress to save them when the user updates the post. For that, we will hook into the save_post action. Similar to Step 3, add the add_action code into your __construct and the save_post_meta() function as a new method for the Bulk_Edit class.
When the user hits the Update button, the two fields will be updated and saved to the database.
Step 5: Save the Bulk Edit fields
This is more complicated vs the Quick Edit because we need to use Ajax to save the fields. Well, at least complicated for me.
Edit the bulk-edit.js file to add the following code before the closing brackets i.e. the last line of our previously created js file.
I’ve left comments above. In summary, we fetch the post IDs and the values of the related_posts and exclude_this_post fields. We then send this data to the wz_tutorials_save_bulk_edit ajax action that we will create using the code below.
As above, add the add_action code into your __construct and the save_bulk_edit() function as a new method for the Bulk_Edit class.
In the above function, if the related posts field is set to 0, the ACF field is cleared. Otherwise, we parse the field, check if the posts are published and then save them. It’s easier with the Exclude this post field as it is a true/false field. If set to true, we save the ACF field. If false, we delete the field.
Get the code
You can browse the code in GitHub.


Excellent tuto which helped me much. Thanks!
One thing I came across: Step 5 can be achieved much simple without extra scripts or ajax calls.
Here is a sample of code I’m using that simply hooks into save_post and checks for GET data and nonce that is passed by the bulk Update button:
“`
/**
* Save my bulk edit settings.
*
* @param int $post_id Post ID.
*/
function my_bulk_edit_save( $post_id ) {
if ( empty( $_GET[‘bulk_edit’] ) || ! current_user_can( ‘edit_post’, $post_id ) ) {
return;
}
// check bulk edit nonce.
if ( empty( $_GET[‘_wpnonce’] ) || ! wp_verify_nonce( $_GET[‘_wpnonce’], ‘bulk-posts’ ) ) {
return;
}
if ( isset( $_GET[‘my_key’] ) ) {
update_post_meta( $post_id, ‘_my_key’, sanitize_key( wp_unslash( $_GET[‘my_key’] ) ) );
}
}
add_action( ‘save_post’, ‘my_bulk_edit_save’ );
“`
Thanks. This does indeed look simpler. Will try this out. This should hopefully make my plugin code a bit simpler if I can get it to work.