3
votes

I'm looking for a solution to export custom post to CSV by the current filter from wp-admin page.

Example: if I'm in a wp-admin page of my custom post called "leads" like this:

mysite.com/wp-admin/edit.php?post_type=lead

now I'm make some filters like select data and specific term form taxonomy and get something like this:

mysite.com/wp-admin/edit.phps&post_status=all&post_type=lead&action=-1&m=201607&companies=check-point&filter_action=Filter&paged=1&action2=-1

now I'm looking to way to take this results and export them to CSV - by adding a button/link - "export to csv"

there is some "export to csv" wp plugins but they all force you to go to other page and make the filter again that its not a good solution for us in this project.

Thanks!

1

1 Answers

3
votes

My solution is based on this code here:

https://github.com/mithusree/wp-simple-phpexcel-export/blob/master/wp-simple-phpexcel-export.php

That use in this great library: http://phpexcel.codeplex.com

My url looks now like this:

mysite.com/wp-admin/edit.php?post_status=all&post_type=lead&m=0&start_date=2016-07-04&end_date=2016-08-03&no_feat_img&companies=check-point&filter_action=Filter&paged=1

On line 109 on the form i cut the code and move it to function.php wrapping this form on function an call it only in wp-admin leads archive page and push the values from url to the form:

    add_action( 'admin_notices', 'export_btn' );
function export_btn() {
    global $typenow;
    if ($typenow == 'lead') {

        global $_GET;
        $start_date = $_GET[start_date];
        $end_date = $_GET[end_date];
        $company = $_GET[companies];

        ?>

        <div class="wrap alignright">
            <form method='get' action="admin.php?page=spee-dashboard">
                <input type="hidden" name='page' value="spee-dashboard"/>
                <input type="hidden" name='noheader' value="1"/>
                <input type="hidden" name='start_date' value="<?php echo $start_date ?>"/>
                <input type="hidden" name='end_date' value="<?php echo $end_date ?>"/>
                <input type="hidden" name='company' value="<?php echo $company ?>"/>
                <input style="display:none" type="radio" name='format' id="formatCSV" value="csv" checked="checked"/>
                <input type="submit" name='export' id="csvExport" value="Export"/>
            </form>
        </div>

        <?php
    }
}

In lines 45-50 I edited the mysql query according to my url:

$start_date = $_GET['start_date'];
$end_date = $_GET['end_date'];
$company = $_GET['company'];

$query = "
            SELECT * FROM wp_posts p
            LEFT OUTER JOIN wp_term_relationships r ON r.object_id = p.ID
            LEFT OUTER JOIN wp_term_taxonomy x ON x.term_taxonomy_id = r.term_taxonomy_id
            LEFT OUTER JOIN wp_terms t ON t.term_id = x.term_id
            WHERE p.post_type = 'lead'
            AND p.post_status =  'publish'
            AND p.post_date BETWEEN '$start_date' AND '$end_date 23:59:59'
            AND t.slug = '$company'
            ORDER BY p.post_date DESC
            ";