我试图通过选择帖子并执行批量操作来打印帖子,以便将它们动态添加到我已经设置的模板页面。这可能吗?
更新更多信息:我正在使用中的此功能this 实例
更具体的问题是:您能否将url的一部分从管理员传递到前端?
function custom_bulk_action() {
global $typenow;
$post_type = $typenow;
if($post_type == \'slides\') {
// get the action
$wp_list_table = _get_list_table(\'WP_Posts_List_Table\'); // depending on your resource type this could be WP_Users_List_Table, WP_Comments_List_Table, etc
$action = $wp_list_table->current_action();
$allowed_actions = array("export");
if(!in_array($action, $allowed_actions)) return;
// security check
check_admin_referer(\'bulk-posts\');
// make sure ids are submitted. depending on the resource type, this may be \'media\' or \'ids\'
if(isset($_REQUEST[\'post\'])) {
$post_ids = array_map(\'intval\', $_REQUEST[\'post\']);
}
if(empty($post_ids)) return;
// this is based on wp-admin/edit.php
$sendback = remove_query_arg( array(\'exported\', \'untrashed\', \'deleted\', \'ids\'), wp_get_referer() );
if ( ! $sendback )
$sendback = admin_url( "edit.php?post_type=$post_type" );
$pagenum = $wp_list_table->get_pagenum();
$sendback = add_query_arg( \'paged\', $pagenum, $sendback );
switch($action) {
case \'export\':
// if we set up user permissions/capabilities, the code might look like:
//if ( !current_user_can($post_type_object->cap->export_post, $post_id) )
// wp_die( __(\'You are not allowed to export this post.\') );
$exported = 0;
foreach( $post_ids as $post_id ) {
if ( !$this->perform_export($post_id) )
wp_die( __(\'Error exporting post.\') );
$exported++;
}
$sendback = add_query_arg( array(\'exported\' => $exported, \'ids\' => join(\',\', $post_ids) ), $sendback );
break;
default: return;
}
$sendback = remove_query_arg( array(\'action\', \'action2\', \'tags_input\', \'post_author\', \'comment_status\', \'ping_status\', \'_status\', \'post\', \'bulk_edit\', \'post_view\'), $sendback );
wp_redirect($sendback);
exit();
}
}
SO网友:jerime
所以我想我会更新我提出的解决方案,以防将来有人遇到这个问题。
我所做的是在函数中重建url,并将url中的ID传递给模板页面。
function custom_bulk_action() {
global $typenow;
$post_type = $typenow;
if($post_type == \'post_type\') {
// get the action
$wp_list_table = _get_list_table(\'WP_Posts_List_Table\'); // depending on your resource type this could be WP_Users_List_Table, WP_Comments_List_Table, etc
$action = $wp_list_table->current_action();
$allowed_actions = array("export");
if(!in_array($action, $allowed_actions)) return;
// security check
check_admin_referer(\'bulk-posts\');
// make sure ids are submitted. depending on the resource type, this may be \'media\' or \'ids\'
if(isset($_REQUEST[\'post\'])) {
$post_ids = array_map(\'intval\', $_REQUEST[\'post\']);
}
if(empty($post_ids)) return;
// this is based on wp-admin/edit.php
$sendback = get_site_url() . \'/whatever/?\';
switch($action) {
case \'export\':
// if we set up user permissions/capabilities, the code might look like:
//if ( !current_user_can($post_type_object->cap->export_post, $post_id) )
// wp_die( __(\'You are not allowed to export this post.\') );
$exported = 0;
foreach( $post_ids as $post_id ) {
if ( !$this->perform_export($post_id) )
wp_die( __(\'Error printeting post.\') );
$exported++;
}
$sendback = add_query_arg( array(\'ids\' => join(\',\', $post_ids) ), $sendback );
break;
default: return;
}
$sendback = remove_query_arg( array(\'action\', \'action2\', \'tags_input\', \'post_author\', \'comment_status\', \'ping_status\', \'_status\', \'post\', \'bulk_edit\', \'post_view\'), $sendback );
wp_redirect($sendback);
exit();
}
}
在url中获得ID后,我将其放入一个数组中
$postIds = explode(\',\', $_GET[\'ids\']);
然后打电话给他们
WP_Query
args使用
post__in
.
可能还有一些不必要的代码留在那里,但它完成了工作。