How To Export Custom Posts and Metabox To CSV

function admin_post_list_add_export_button( $which ) {
    global $typenow;
    if ( 'shaform_db' === $typenow && 'top' === $which ) { ?>
        <input type="submit" name="export_all_posts" class="button button-primary" value="<?php _e('Export All Submissions'); ?>" />
        <?php
    }
}
add_action( 'manage_posts_extra_tablenav', 'admin_post_list_add_export_button', 20, 1 );
  function func_export_all_posts() {
    if(isset($_GET['export_all_posts'])) {
        $arg = array(
            'post_type' => 'shaform_db',
            'post_status' => 'publish',
            'posts_per_page' => -1,
        );  
    global $current_user, $product, $wp_session, $post;

    $arr_post = get_posts($arg);
    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('Full Name', 'Email', 'Phone', 'City', 'Postal Code', 'Appointment Date', 'Date', 'How did you hear about us?','Preferred Showroom', 'Street Address', 'Interested in', 'Number of Rooms', 'Comments', 'Image URL', 'Item Name', 'Item URL'));

        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;
                }
            }*/

        global $current_user, $product, $wp_session, $post;

        $p_data = get_post_meta( get_the_ID(),  "_quote_items", true );

        $ptitle = array();
        $purl = array();
        $pimg = array();

        foreach ($p_data as $quote) {

            $product_id = $quote['pid'];
            $quantity   = $quote['quantity'];
            $product   = wc_get_product( $product_id );
            $item_data = array();
            $var_data  = array();
            $str       = '?';

            if (!empty($quote['vari_id'])) {

                $variation   = wc_get_product($quote['vari_id']);
                $pro_title   = $variation->get_title();
                $p_title     = explode('(', $variation->get_formatted_name());
                $pro_title   = $p_title[0];
                $product_url = $variation->get_permalink();
                $pro_image   = $variation->get_image();
                $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $product_id ), array(100,100) );

            } else {
                $pro_title   = $product->get_title();
                $product_url = $product->get_permalink();
                $pro_image   = $product->get_image();
                $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $product_id ), array(100,100) );
            }

            $ptitle[] = $pro_title;
            $purl[] = $product_url;
            $pimg[] = $thumbnail[0];

        }   // end foreach


            $c1 = get_post_meta( get_the_ID(),  "_your_custom_1", true );
            $c2 = get_post_meta( get_the_ID(),  "_your_custom_2", true );
            $c3 = get_post_meta( get_the_ID(),  "_your_custom_3", true );
            $c4 = get_post_meta( get_the_ID(),  "_your_custom_4", true );
            $c5 = get_post_meta( get_the_ID(),  "_your_custom_5", true );
            $c6 = get_post_meta( get_the_ID(),  "_your_custom_6", true );
            $c7 = get_post_meta( get_the_ID(),  "_your_custom_7", true );
            $c8 = get_post_meta( get_the_ID(),  "_your_custom_8", true );
            $c9 = get_post_meta( get_the_ID(),  "_your_custom_9", true );
            $c10 = get_post_meta( get_the_ID(),  "_your_custom_10", true );
            $c11 = get_post_meta( get_the_ID(),  "_your_custom_11", true );
            $c12 = get_post_meta( get_the_ID(),  "_your_custom_12", true );
            $c13 = get_post_meta( get_the_ID(),  "_your_custom_13", true );

            //fputcsv($file, array(get_the_title(), get_the_permalink(), implode(",", $cats), implode(",", $tags)));

            fputcsv($file, array($c1, $c2, $c3, $c4, $c5, $c6, $c7, $c8, $c9, $c10, $c11, $c12, $c13, implode(",", $pimg), implode(",", $ptitle), implode(",", $purl)));
        }

        exit();
    }
}
}
add_action( 'init', 'func_export_all_posts' );
global $post, $product, $woocommerce, $wp_session;
									
// Insert the post into the database
$post_id = wp_insert_post(array (
'post_type' => 'shaform_db',
'post_title' => 'SHA Form ('.$lval.') - ' . date( 'Y-m-d H:i:s' ),
'post_content' => $comments,
'post_status' => 'publish',
'comment_status' => 'closed',
'ping_status' => 'closed',
'meta_input' => array(
'_your_custom_1' => $fullname, 
'_your_custom_2' => $email,
'_your_custom_3' => $phone,
'_your_custom_4' => $city,
'_your_custom_5' => $zipcode,
'_your_custom_6' => $date,
'_your_custom_7' => $send_date,
'_your_custom_8' => $hearabout,
'_your_custom_9' => $lval,
'_your_custom_10' => $address,
'_your_custom_11' => $totalFlooringType,
'_your_custom_12' => $NumberOfRooms,
'_your_custom_13' => $comments,
),));
									
if ( ! empty( $_POST['meta_input'] ) ) {
foreach ( $_POST['meta_input'] as $field => $value ) {
update_post_meta( $post_id, $field, $value );
}
}
									
$s_data = array();
$s_data = WC()->session->get( 'quotes' );
//echo $post_id;
update_post_meta( $post_id,  "_quote_items", $s_data );

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>