Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export User Info into CSV - Additional Column

I'm using the 'Export Users to CSV' a plugin for wordpress. By default, The plugin only extracts wp_users and wp_usermeta

I have been wanting to add post_title column from wp_posts. I was able to pull out the 'post_title' into the CSV file BUT it's not placed correctly in its respective row.

Please see my edited code and original code

Orignal version:

global $wpdb;
$data_keys = array(
    'ID', 'user_login', 'user_pass',
    'user_nicename', 'user_email', 'user_url',
    'user_registered', 'user_activation_key', 'user_status',
    'display_name'
);
$meta_keys = $wpdb->get_results( "SELECT distinct(meta_key) FROM $wpdb->usermeta" );
$meta_keys = wp_list_pluck( $meta_keys, 'meta_key' );
$fields = array_merge( $data_keys, $meta_keys);

$headers = array();
foreach ( $fields as $key => $field ) {
    if ( in_array( $field, $exclude_data ) )
        unset( $fields[$key] );
    else
        $headers[] = '"' . strtolower( $field ) . '"';
}
echo implode( ',', $headers ) . "\n";

foreach ( $users as $user ) {
    $data = array();
    foreach ( $fields as $field ) {
        $value = isset( $user->{$field} ) ? $user->{$field} : '';
        $value = is_array( $value ) ? serialize( $value ) : $value;
        $data[] = '"' . str_replace( '"', '""', $value ) . '"';
    }
    echo implode( ',', $data ) . "\n";
}

Edited version

global $wpdb;

$data_keys = array(
    'ID', 'user_login', 'user_pass',
    'user_nicename', 'user_email', 'user_url',
    'user_registered', 'user_activation_key', 'user_status',
    'display_name'
);
$meta_keys = $wpdb->get_results( "SELECT distinct(meta_key) FROM $wpdb->usermeta" );
$meta_keys = wp_list_pluck( $meta_keys, 'meta_key' );
$post_keys = $wpdb->get_results( "SELECT distinct(post_title) FROM $wpdb->posts" );
$post_keys = wp_list_pluck( $post_keys, 'post_title' );
$fields = array_merge( $data_keys, $meta_keys, $post_keys);

$headers = array();
foreach ( $fields as $key => $field ) {
    if ( in_array( $field, $exclude_data ) )
        unset( $fields[$key] );
    else
        $headers[] = '"' . strtolower( $field ) . '"';
}
echo implode( ',', $headers ) . "\n";

foreach ( $users as $user ) {
    $data = array();
    foreach ( $fields as $field ) {
        $value = isset( $user->{$field} ) ? $user->{$field} : '';
        $value = is_array( $value ) ? serialize( $value ) : $value;
        $data[] = '"' . str_replace( '"', '""', $value ) . '"';
    }
    echo implode( ',', $data ) . "\n";
}

I hope my explanation is not confusing. Cheers!

Here's an image to explain it better :

https://www.dropbox.com/s/xijkb4fp16atcx3/question-jpeg.jpg?dl=0

like image 602
Jeremi Liwanag Avatar asked Nov 10 '15 07:11

Jeremi Liwanag


1 Answers

The problem is that you are using the list of titles as columns, since wp_list_pluck doesn't retain the column name post_title.

The approach would be different if you want to use just one title post per user, or you want more, especially since the name for the column will change.
Assuming you want them all, we can create a simple approach that you can improve.

For example, I would say.

First, remove these two lines

$post_keys = $wpdb->get_results( "SELECT distinct(post_title) FROM $wpdb->posts" );
$post_keys = wp_list_pluck( $post_keys, 'post_title' );

and change $fields = array_merge( $data_keys, $meta_keys, $post_keys);by $fields = array_merge( $data_keys, $meta_keys, $post_keys);

Then, before

 echo implode( ',', $data ) . "\n";

you add

$post_keys = $wpdb->get_results( "SELECT distinct(post_title) FROM $wpdb->posts  WHERE (post_type LIKE 'page' OR post_type LIKE 'post') and post_status LIKE 'publish' AND post_author = ". $user->ID );
$post_titles = wp_list_pluck( $post_keys, 'post_title');
$data =array_merge ($data, $post_titles);

With this, the column headers for the post titles wont be set, we can improve it to set it if you need it, but you should have already a column per every post title

like image 184
Carlos Robles Avatar answered Sep 21 '22 10:09

Carlos Robles