首页 > 解决方案 > 如何在 CSV 上导出 wordpress 用户 slug?

问题描述


我正在使用此代码(原始代码 [此处][1])在帖子后端创建一个按钮,以便导出和下载包含以下信息的 CSV:“帖子标题”、“URL”、“类别”、“标签”、“作者”、“作者 slug” 但我找不到导出用户 slug 的方法。
我该怎么做?我会很感激你的帮助
function admin_post_list_add_export_button( $which ) {
    global $typenow;
  
    if ( 'post' === $typenow && 'top' === $which ) {
        ?>
        <input type="submit" name="export_all_posts" class="button button-primary" value="<?php _e('Export All Posts'); ?>" />
        <?php
    }
}
 
add_action( 'manage_posts_extra_tablenav', 'admin_post_list_add_export_button', 20, 1 );

function func_export_all_posts() {
    if(isset($_GET['export_all_posts'])) {
        $arg = array(
            'post_type' => 'post',
            'post_status' => 'publish',
            'posts_per_page' => -1,
        );
  
        global $post;
        $arr_post = get_posts($arg);
        if ($arr_post) {
  
            header('Content-type: text/csv');
            header('Content-Disposition: attachment; filename="wp-posts.csv"');
            header('Pragma: no-cache');
            header('Expires: 0');
  
            $file = fopen('php://output', 'w');
  
            fputcsv($file, array('Post Title', 'URL', 'Categories', 'Tags', 'Author', 'Author slug'));
  
            foreach ($arr_post as $post) {
                setup_postdata($post);
                  
                $categories = get_the_category();
                $cats = array();
                if (!empty($categories)) {
                    foreach ( $categories as $category ) {
                        $cats[] = $category->name;
                    }
                }
  
                $post_tags = get_the_tags();
                $tags = array();
                if (!empty($post_tags)) {
                    foreach ($post_tags as $tag) {
                        $tags[] = $tag->name;
                    }
                }
  
                fputcsv($file, array(get_the_title(), get_the_permalink(), implode(",", $cats), implode(",", $tags), get_the_author(), get_user_by('slug','nicename')));
            }
  
            exit();
        }
    }
}
 
add_action( 'init', 'func_export_all_posts' );

标签: phpwordpresscsvslug

解决方案


推荐阅读