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'
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'
<?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);
}
?>
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);
}
?>
Filter&paged=1&action2=-1
– user10980228 Commented Mar 7, 2020 at 8:56'include' => 'all id's here'
in the $args array? – user10980228 Commented Mar 7, 2020 at 9:03