How to return/export only data showing on screen in custom post type view all screen
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'
Leave an answer