• Resolved kelvinthegerbil

    (@kelvinthegerbil)


    Hi,

    please could you add the POST ID to the options and export.
    I’ve dropped it in manually using the following but it would be nice to be an option as part of the supported/updated versions in future.

    Edit the plugin functions.php

    <?php
    /**
     * Created by PhpStorm.
     * User: Atlas_Gondal
     * Date: 4/9/2016
     * Time: 9:01 AM
     */
    
    function get_selected_post_type($post_type, $custom_posts_names){
    
        switch ($post_type){
    
            case "any":
    
                $type = "any";
                break;
    
            case "page":
    
                $type = "page";
                break;
    
            case "post":
    
                $type = "post";
                break;
    
            default:
    
                for( $i = 0; $i < count($custom_posts_names); $i++ ){
    
                    if ($post_type == $custom_posts_names[$i] ){
    
                        $type = $custom_posts_names[$i];
    
                    }
    
                }
    
        }
    
        return $type;
    
    }
    
    function IsChecked($name,$value)
    {
        foreach($name as $data)
        {
            if($data == $value)
            {
                return true;
            }
        }
    
        return false;
    }
    
    /**
     * @param $selected_post_type
     * @param $post_status
     * @param $post_author
     * @param $post_per_page
     * @param $offset
     * @param $export_type
     * @param $additional_data
     * @param $csv_path
     * @param $csv_name
     * @param $posts_from
     * @param $posts_upto
     */
    function generate_output($selected_post_type, $post_status, $post_author, $post_per_page, $offset, $export_type, $additional_data, $csv_path, $csv_name, $posts_from, $posts_upto){
    
        $html = array();
        $counter = 0;
    
        if ($export_type == "here") {
            $line_break = "<br/>";
        }
        else {
            $line_break = "";
        }
    
        if ($post_author == "all"){
            $post_author = "";
        }
    
        if ($post_per_page == "all" && $offset == "all") {
            $post_per_page = -1;
            $offset = "";
        }
    
        switch ($post_status){
            case "all":
                $post_status = array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'trash');
                break;
            case 'publish':
                $post_status = 'publish';
                break;
            case 'pending':
                $post_status = 'pending';
                break;
            case 'draft':
                $post_status = 'draft';
                break;
            case 'future':
                $post_status = 'future';
                break;
            case 'private':
                $post_status = 'private';
                break;
            case 'trash':
                $post_status = 'trash';
                break;
            default:
                $post_status = 'publish';
                break;
        }
    
        $posts_query = new WP_Query( array(
            'post_type'         => $selected_post_type,
            'post_status'       => $post_status,
            'author'            => $post_author,
            'posts_per_page'    => $post_per_page,
            'offset'            => $offset,
            'orderby'           => 'title',
            'order'             => 'ASC',
            'date_query' => array(
                array(
                    'after'     => $posts_from,
                    'before'    => $posts_upto,
                    'inclusive' => true,
                ),
            )
        ) );
    
        if(!$posts_query->have_posts()){
            echo "no result found in that range, please <strong>reselect and try again</strong>!";
            return;
        }
        
         while ( $posts_query->have_posts() ):
    
                $html['postid'][$counter] = (isset($html['postid'][$counter]) ? "" : null);
    
                $posts_query->the_post();
                $html['postid'][$counter] .= get_the_ID().$line_break;
                $counter++;
    
            endwhile;
    
            $counter = 0;
    
        if (IsChecked($additional_data, 'url')) {
    
            while ( $posts_query->have_posts() ):
    
                $html['url'][$counter] = (isset($html['url'][$counter]) ? "" : null);
    
                $posts_query->the_post();
                $html['url'][$counter] .= get_permalink().$line_break;
                $counter++;
    
            endwhile;
    
            $counter = 0;
    
        }
    
        if (IsChecked($additional_data, 'title')) {
    
            while ( $posts_query->have_posts() ):
    
                $html['title'][$counter] = (isset($html['title'][$counter]) ? "" : null);
    
                $posts_query->the_post();
                $html['title'][$counter] .= get_the_title().$line_break;
                $counter++;
    
            endwhile;
    
            $counter = 0;
    
        }
    
        if (IsChecked($additional_data, 'category')) {
    
            while ( $posts_query->have_posts() ):
    
                $html['category'][$counter] = (isset($html['category'][$counter]) ? "" : null);
    
                $categories = '';
                $posts_query->the_post();
                $cats = get_the_category();
                foreach($cats as $index => $cat){
    
                    $categories .= ($index == 0 ? $cat->name : ", ".$cat->name);
    
                }
                
                $html['category'][$counter] .= $categories.$line_break;
                
                $counter++;
    
            endwhile;
    
            $counter = 0;
    
        }
        export_data($html, $export_type, $csv_path, $csv_name);
    
        wp_reset_postdata();
    }
    
    function export_data($urls, $export_type, $csv_path, $csv_name){
    
        $file_path = wp_upload_dir();
    
        $count = 0;
        foreach($urls as $item){
            $count = count($item);
        }
    
        switch ($export_type){
    
            case "text":
    
                $data = '';
                $headers = array();
    
                $file = $csv_path.$csv_name.'.CSV';
                $myfile = @fopen($file, "w") or die("Unable to create a file on your server!");
                fprintf( $myfile, "\xEF\xBB\xBF");
    
                (isset($urls['postid']) ? $headers[] = 'Post ID' : null);
                (isset($urls['title']) ? $headers[] = 'Title' : null);
                (isset($urls['url']) ? $headers[] = 'URLs' : null);
                (isset($urls['category']) ? $headers[] = 'Categories' : null);
    
                fputcsv($myfile, $headers);
    
                for( $i = 0; $i < $count; $i++ ){
                    $data = array(
                        ($urls['postid']) ? $urls['postid'][$i] : "",
                        ($urls['title']) ? $urls['title'][$i] : "",
                        ($urls['url']) ? $urls['url'][$i] : "",
                        ($urls['category']) ? $urls['category'][$i] : ""
                    );
    
                    fputcsv($myfile, $data);
                }
    
                fclose($myfile);
    
                echo "<div class='updated'>Data exported successfully! <a href='".$file_path['baseurl']."/".$csv_name.".CSV' target='_blank'><strong>Click here</strong></a> to Download.</div>";
    
                break;
    
            case "here":
    
                echo "<h1 align='center'><strong>Below is a list of Exported Data:</strong></h1>";
                echo "<table class='form-table'>";
                echo "<tr><th>ID</th>";
    
                echo isset($urls['postid']) ? "<th>Post ID</th>" : null;
                echo isset($urls['title']) ? "<th>Title</th>" : null;
                echo isset($urls['url']) ? "<th>URLs</th>" : null;
                echo isset($urls['category']) ? "<th>Categories</th>" : null;
    
                echo "</tr>";
    
                for( $i = 0; $i < $count; $i++ ){
    
                    $id = $i + 1;
                    echo "<tr><td>".$id."</td>";
                    echo isset($urls['postid']) ? "<td>".$urls['postid'][$i]."</td>" : null;
                    echo isset($urls['title']) ? "<td>".$urls['title'][$i]."</td>" : null;
                    echo isset($urls['url']) ? "<td>".$urls['url'][$i]."</td>" : null;
                    echo isset($urls['category']) ? "<td>".$urls['category'][$i]."</td>" : null;
                    echo "</tr>";
                }
    
                echo "</table>";
    
                break;
    
            default:
    
                echo "Sorry, you missed export type, Please <strong>Select Export Type</strong> and try again! :)";
                break;
    
        }
    
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Atlas Gondal

    (@atlas_gondal)

    Thanks Kelvin, I was working on new release, and just saw your request now, while looking for new feature requests. I will include this feature in next update. Thanks again for sharing code snippet!

    Plugin Author Atlas Gondal

    (@atlas_gondal)

    Thanks again for your effort, now its available in current release!

    • This reply was modified 4 years, 10 months ago by Atlas Gondal.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Please Add Post ID Column’ is closed to new replies.