【发布时间】:2019-05-28 04:13:48
【问题描述】:
我正在尝试直接从 Wordpress 的仪表板 (edit.php) 将自定义类型的帖子列表导出为 CSV。到目前为止,这是我的代码:
add_filter('pre_get_posts', 'yri_export_csv');
function yri_export_csv($query) {
if(is_admin() && $query->get('post_type') == 'kohde' && isset($_GET['export_csv'])) {
$kunta = $query->get('kunta');
$osasto = $query->get('osasto');
$time = time();
$args = $query->query;
$posts = get_posts($args);
header('Content-type: application/csv');
header("Content-Disposition: attachment; filename=kohteet_{$kunta}_{$osasto}_{$time}.csv");
foreach ($posts as $post) {
// Do CSV stuff here
}
die();
}
}
问题是当我使用get_posts 或new WP_Query 查询帖子时,Wordpress 出于某种原因将我重定向到前端,到 404 页面。为什么这样做?
【问题讨论】: