0

If I for example filter my custom post type by date and it only shows 2 records on screen, I only want to export those 2 records, but it is exporting a bunch of records out of my control ie: the wrong records.

How can I modify this to only export what is showing up on screen ie: the 2 records in this case?

add_action( 'init', 'func_export_some_records' );
function func_export_all_posts() {
    if(isset($_GET['export_some_records'])) {
        $arg = array(
                'post_type' => 'shirts',
                'fields' => 'ids'
            );


        global $post;
        $arr_post = get_posts($arg);
        if ($arr_post) {

            header("Content-Description: File Transfer");
            header("Content-Type: application/csv");
            header('Content-Disposition: attachment; filename="wp.csv"');
            header('Pragma: no-cache');
            header('Expires: 0');

            $file = fopen('php://output', 'w');

            fputcsv($file, array('COLUMN ONE', 'COLUMN TWO'));

            foreach ($arr_post as $post) {

                $color = get_post_meta( $post, 'user-color', true );
                $size = get_post_meta( $post, 'user-size', true );

                fputcsv($file, array($color, $size));
            }


            fclose($file);

        }
    }
}

Here is the button that triggers the download:

add_action( 'manage_posts_extra_tablenav', 'admin_post_list_top_export_button', 20, 1 );
function admin_post_list_top_export_button( $which ) {
    global $typenow;

    if ( 'shirts' === $typenow && 'top' === $which ) {
        ?>
        <input type="submit" name="export_some_records" id="export_some_records" class="button button-primary" value="Export Some Records" />
        <?php
    }

}

One way I thought of but not actually sure how to achieve is to get all the post ID's currently on screen and then in my $arg array use 'includes' => 'post id here'

3
  • Read this stackoverflow.com/a/28090348/3886203 – Daniel Gross Mar 7 '20 at 8:42
  • @DanielGross, thanks for that. That doesn't really do what I want. I still isn't exporting the records on the screen. I only want to export what I can see basically. There are some details in the slug but not sure if I need to use those Filter&paged=1&action2=-1 – user10980228 Mar 7 '20 at 8:56
  • What about getting all the post ID's and then using 'include' => 'all id's here' in the $args array? – user10980228 Mar 7 '20 at 9:03
0
<?php
//edit.php?s&post_status=all&post_type=shirts&action=-1&m=0&slug=03-02-2020&filter_action=Filter&paged=1&action2=-1 
//$parse_uri = explode('wp-content', $_SERVER['SCRIPT_FILENAME']);
//require_once($parse_uri[0] . 'wp-load.php');
header("Content-Description: File Transfer");
header("Content-Type: application/csv");
header('Content-Disposition: attachment; filename="wp.csv"');
header('Pragma: no-cache');
header('Expires: 0');

global $post;

//https://developer.wordpress.org/reference/classes/wp_query/
$post_status = isset($_REQUEST['post_status']) && $_REQUEST['post_status']=='all' ? 'any' : 'publish';
$date = explode('-',$_REQUEST['slug']);
$posts_per_page = $_REQUEST['action2'];
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$arg = array(
    'post_type' => $_REQUEST['post_type'],
    'post_status' => $post_status,
    'posts_per_page' => $posts_per_page,
    'paged' => $paged,
    'date_query' => array(
        'column' => 'post_date',
        'year' => $date[2],
        'month' => $date[1],
        'day' => $date[0],
    )
);

$posts = new WP_Query($arg);

if($posts->have_posts()){

    $file = fopen('php://output', 'w');

    fputcsv($file, array('COLUMN ONE', 'COLUMN TWO'));


    while ( $posts->have_posts() ){
        $posts->the_post();

        $color = get_post_meta( $post->ID, 'user-color', true );
        $size = get_post_meta( $post->ID, 'user-size', true );

        fputcsv($file, array($color, $size));
    }

    fclose($file);

}
?>
27
  • Thank you, but it doesn't work. I have updated my question with the button that triggers the download. That should probably help. Apologies for committing it initially. – user10980228 Mar 7 '20 at 9:17
  • There is no need to apologize! I didn't pay attention to the question. If it is to filter by date, I edited the code. But there's something else in developer.wordpress.org/reference/classes/wp_query – Daniel Gross Mar 7 '20 at 9:27
  • Still nothing. But, maybe it is the date format? Here is the actual slug. edit.php?s&post_status=all&post_type=shirts&action=-1&m=0&slug=03-02-2020&filter_action=Filter&paged=1&action2=-1 – user10980228 Mar 7 '20 at 9:27
  • I think I get it. I'm going to write something – Daniel Gross Mar 7 '20 at 9:31
  • Ready. I edited it. – Daniel Gross Mar 7 '20 at 9:40
0

Create a file (csv.php) at the root of your template and to test, change the IDs.
Then run the code by accessing the URL:
//www.your_domain/wp-content/themes/your_template/csv.php

<?php
header("Content-Description: File Transfer");
header("Content-Type: application/csv");
header('Content-Disposition: attachment; filename="wp.csv"');
header('Pragma: no-cache');
header('Expires: 0');

$parse_uri = explode('wp-content', $_SERVER['SCRIPT_FILENAME']);
require_once($parse_uri[0] . 'wp-load.php');

$color = '';
$size = '';

$post_ids = array('68251','68159');

$args = array(
    'post_type' => 'shirts',
    'nopaging' => true,
    'post_status' => 'any',
    'include' => $post_ids,
);

$posts = get_posts($args);

if($posts){

    $file = fopen('php://output', 'w');

    fputcsv($file,array('COLUMN ONE','COLUMN TWO'));

    foreach ($posts as $post){

        $color = get_post_meta( $post->ID, 'user-color', true );
        $size = get_post_meta( $post->ID, 'user-size', true );

        fputcsv($file,array($color,$size));
    }

    fclose($file);

}
?>
8
  • Thank you Daniel. But I think I just figured it out. I get the date filter from the slug ie: $_GET['slug'] and then in my $args array I used 'meta_key' => 'addition_date' and 'meta_value' => $_GET['slug']. I didn't think of this before! – user10980228 Mar 7 '20 at 11:41
  • Good to know that your code worked this way :) – Daniel Gross Mar 7 '20 at 11:44
  • I don't suppose you know how I can make this export each row in a separate file instead of multiple rows in one file? – user10980228 Mar 7 '20 at 11:47
  • One moment. Let me see... – Daniel Gross Mar 7 '20 at 11:51
  • I've been thinking! I don't see how to do it. I can research something. But in this way it may confuse the visitor. I suggest you print on the screen two download links. – Daniel Gross Mar 7 '20 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.