Step-1: Add this code with init action in functions.php file.
(this code is to show export button)
function admin_post_list_add_export_button( $which ) {
global $typenow;
if ( 'post' === $typenow && 'top' === $which ) {
?>
<input type="submit" name="jnext_export_posts" class="button button-primary" value="<?php _e('Export Posts'); ?>" />
<?php
}
}
add_action( 'admin_init', 'admin_post_list_add_export_button' );
Screenshot: https://prnt.sc/re4QGfilsCZ4
Step-2: Add this code with init action in functions.php file.
(this code is for the download CSV file)
function jnext_export_posts() {
if(isset($_GET['jnext_export_posts'])) {
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
);
if ( isset($_GET['post']) ) {
$args['post__in'] = $_GET['post'];
} else {
$args['posts_per_page'] = -1;
}
global $post;
$arr_post = get_posts($args);
if ($arr_post) {
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="wp-posts.csv"');
header('Pragma: no-cache');
header('Expires: 0');
$file = fopen('php://output', 'w');
fputcsv($file, array('Post ID', 'Post Title', 'URL', 'Categories', 'Tags'));
foreach ($arr_post as $post) {
setup_postdata($post);
$categories = get_the_category();
$cats = array();
if (!empty($categories)) {
foreach ( $categories as $category ) {
$cats[] = $category->name;
}
}
$post_tags = get_the_tags();
$tags = array();
if (!empty($post_tags)) {
foreach ($post_tags as $tag) {
$tags[] = $tag->name;
}
}
fputcsv($file, array(get_the_ID(), get_the_title(), get_the_permalink(), implode(",", $cats), implode(",", $tags)));
}
exit();
}
}
}
add_action( 'admin_init', 'jnext_export_posts' );
Thank You.
-
This reply was modified 2 years, 5 months ago by Vipul Ghori.