Reputation: 87
I am working on a wordpress project and I want to add the bulk action on my custom post.
I have used Custom Post Type
UI plugin for custom post and Advanced Custom Fields
plugin for custom fields.
Please suggest me any code or plugin to add bulk action for my custom posts.
Thanks, Aniket.
Upvotes: 4
Views: 4769
Reputation: 17205
In addition with Themesfa's answer, to bulk edit custom post types you have two main option, using the built-in WordPress features or opting for a plugin like Bulk Task Editor.
Here’s a breakdown of both methods:
Core WordPress Method
First, hook into the bulk_actions-{screen_id} filter to add your custom action to the bulk actions dropdown.
function register_custom_bulk_action($bulk_actions) {
$bulk_actions['your_custom_action'] = 'Custom Action';
return $bulk_actions;
}
add_filter('bulk_actions-edit-{post_type}', 'register_custom_bulk_action'); // Replace 'edit-post' with the screen ID you need.
Next, use the handle_bulk_actions-{screen_id} filter to handle the bulk action when the user submits it.
function handle_custom_bulk_action($redirect_to, $doaction, $post_ids) {
if ($doaction == 'your_custom_action') {
// Your logic here
// Redirect back to the posts list after processing.
$redirect_to = add_query_arg('bulk_action_done', count($post_ids), $redirect_to);
}
return $redirect_to;
}
add_filter('handle_bulk_actions-edit-{post_type}', 'handle_custom_bulk_action', 10, 3); // Replace 'edit-post' with the correct screen ID.
Using Bulk Task Editor
For advanced bulk editing of thousands of posts, the Bulk Task Editor for WordPress is a great solution. Here is how it works:
First register a task:
add_action( 'rewbe_post_type_actions', function($actions,$post_type){
if( $post_type == 'your-custom-post-type' ){
$actions[] = array(
'label' => 'Custom Action',
'id' => 'your_custom_action',
);
}
return $actions;
},10,2);
Add the callback function of your task:
add_action('rewbe_do_post_{your_custom_action}',function($post,$args){
// your logic here
return $post;
},10,2);
Execute the Task:
Navigate to the “Tasks” menu in the WordPress admin sidebar.
Click “Add New” to create a new post task, giving it a descriptive name for easy identification.
If needed, use the filtering options to narrow down the list of users. This is particularly useful if only a subset of users requires role updates.
Select the “Custom Action” task from the dropdown menu.
Note: Adjust the number of users processed at a time based on your server’s capacity. Start with a lower number to avoid overloading the server.
Finally, click “Publish” or “Update” to start the process. The Bulk Task Editor will automatically process the users, updating their roles as specified.
References
Upvotes: 0
Reputation: 164
Since WordPress 4.7 (released December 2016) it is possible to add custom bulk actions without using JavaScript.
//Hooks
add_action( 'current_screen', 'my_bulk_hooks' );
function my_bulk_hooks() {
if( current_user_can( 'administrator' ) ) {
add_filter( 'bulk_actions-edit-post', 'register_my_bulk_actions' );
add_filter( 'handle_bulk_actions-edit-post', 'my_bulk_action_handler', 10, 3 );
add_action( 'admin_notices', 'my_bulk_action_admin_notice' );
}
}
//Register
function register_my_bulk_actions($bulk_actions) {
$bulk_actions['email_to_eric'] = __( 'Email to Eric', 'text_domain');
return $bulk_actions;
}
//Handle
function my_bulk_action_handler( $redirect_to, $doaction, $post_ids ) {
if ( $doaction !== 'email_to_eric' ) {
return $redirect_to;
}
foreach ( $post_ids as $post_id ) {
// Perform action for each post.
}
$redirect_to = add_query_arg( 'bulk_emailed_posts', count( $post_ids ), $redirect_to );
return $redirect_to;
}
//Notices
function my_bulk_action_admin_notice() {
if ( ! empty( $_REQUEST['bulk_emailed_posts'] ) ) {
$emailed_count = intval( $_REQUEST['bulk_emailed_posts'] );
printf( '<div id="message" class="updated fade">' .
_n( 'Emailed %s post to Eric.',
'Emailed %s posts to Eric.',
$emailed_count,
'text_domain'
) . '</div>', $emailed_count );
}
}
Note.1: You must use
bulk_actions
filters whenWP_Screen
object is defined.That's why I usedcurrent_screen
action in line 2.Note.2: if you want to add bulk action to custom page like woocommerce products page just change screen id in line 5 & 6. Ex:
add_filter( 'bulk_actions-edit-product', 'register_my_bulk_actions' );
add_filter( 'handle_bulk_actions-edit-product', 'my_bulk_action_handler', 10, 3 );
More information :
Using Custom Bulk Actions
https://make.wordpress.org/core/2016/10/04/custom-bulk-actions/
Upvotes: 5
Reputation: 1
use "register_post_type" of WordPress function, It easier than the additional plugins Reference: https://codex.wordpress.org/Function_Reference/register_post_type
Upvotes: -3