{"id":46598,"date":"2026-09-28T08:02:55","date_gmt":"2026-09-28T08:02:55","guid":{"rendered":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/?p=46598"},"modified":"2026-10-10T21:01:22","modified_gmt":"2026-10-10T21:01:22","slug":"export-custom-post-type-to-csv-without-plugin","status":"publish","type":"post","link":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/export-custom-post-type-to-csv-without-plugin\/","title":{"rendered":"WordPress Export Custom Post Type to CSV Without Plugin"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Clients often want their custom post type data in a spreadsheet: every book, event or location, with its categories and custom fields. The built-in <strong>Tools \u2192 Export<\/strong> only gives you WXR XML. Export plugins are usually larger than the job needs, and many build the file in memory, so they struggle on big sites.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This snippet adds an <strong>Export CSV<\/strong> button to the list screen of every post type (posts, pages and custom post types). One click downloads a UTF-8 CSV with the core fields, one column per taxonomy and any meta keys you choose. The download is protected by a nonce and a capability check. Cells that a spreadsheet would run as formulas are neutralised.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The snippet<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Save it as <code>wp-content\/mu-plugins\/dpc-export-csv.php<\/code>, or paste it (without the header) into a small plugin or your theme\u2019s <code>functions.php<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">&lt;?php\n\/**\n * Plugin Name: DPC Export Post Type to CSV\n * Description: Adds an \"Export CSV\" button to every post type list screen.\n *\/\n\ndefined( 'ABSPATH' ) || exit;\n\nadd_action( 'manage_posts_extra_tablenav', 'dpc_csv_export_button' );\nadd_action( 'admin_post_dpc_export_csv', 'dpc_csv_export_download' );\n\n\/**\n * Print the button above the list table.\n *\/\nfunction dpc_csv_export_button( string $which ): void {\n\t$screen = get_current_screen();\n\tif ( 'top' !== $which || ! $screen || ! post_type_exists( $screen-&gt;post_type ) ) {\n\t\treturn;\n\t}\n\n\t$object = get_post_type_object( $screen-&gt;post_type );\n\tif ( ! current_user_can( $object-&gt;cap-&gt;edit_others_posts ) ) {\n\t\treturn;\n\t}\n\n\t$url = wp_nonce_url(\n\t\tadd_query_arg(\n\t\t\tarray(\n\t\t\t\t'action'    =&gt; 'dpc_export_csv',\n\t\t\t\t'post_type' =&gt; $screen-&gt;post_type,\n\t\t\t),\n\t\t\tadmin_url( 'admin-post.php' )\n\t\t),\n\t\t'dpc_export_csv_' . $screen-&gt;post_type\n\t);\n\n\tprintf(\n\t\t'&lt;div class=\"alignleft actions\"&gt;&lt;a href=\"%s\" class=\"button\"&gt;%s&lt;\/a&gt;&lt;\/div&gt;',\n\t\tesc_url( $url ),\n\t\tesc_html__( 'Export CSV', 'dpc' )\n\t);\n}\n\n\/**\n * Handle the download request.\n *\/\nfunction dpc_csv_export_download(): void {\n\t$post_type = isset( $_GET&#91;'post_type'&#93; ) ? sanitize_key( wp_unslash( $_GET&#91;'post_type'&#93; ) ) : '';\n\t$object    = get_post_type_object( $post_type );\n\n\tif ( ! $object || ! $object-&gt;show_ui ) {\n\t\twp_die( esc_html__( 'Unknown post type.', 'dpc' ), '', array( 'response' =&gt; 400 ) );\n\t}\n\n\tcheck_admin_referer( 'dpc_export_csv_' . $post_type );\n\n\tif ( ! current_user_can( $object-&gt;cap-&gt;edit_others_posts ) ) {\n\t\twp_die( esc_html__( 'You are not allowed to export this content.', 'dpc' ), '', array( 'response' =&gt; 403 ) );\n\t}\n\n\t$filename = sanitize_file_name( $post_type . '-' . gmdate( 'Y-m-d' ) . '.csv' );\n\n\tnocache_headers();\n\theader( 'Content-Type: text\/csv; charset=utf-8' );\n\theader( 'Content-Disposition: attachment; filename=\"' . $filename . '\"' );\n\n\t$out = fopen( 'php:\/\/output', 'w' );\n\tfwrite( $out, \"\\xEF\\xBB\\xBF\" ); \/\/ BOM so Excel opens the file as UTF-8.\n\tdpc_csv_export_write( $post_type, $out );\n\tfclose( $out );\n\texit;\n}\n\n\/**\n * Taxonomies that get their own column.\n *\/\nfunction dpc_csv_export_taxonomies( string $post_type ): array {\n\t$taxonomies = get_object_taxonomies( $post_type, 'objects' );\n\t$taxonomies = array_filter( $taxonomies, static fn( WP_Taxonomy $tax ): bool =&gt; $tax-&gt;show_ui );\n\n\treturn array_keys( $taxonomies );\n}\n\n\/**\n * Meta keys that get their own column. Empty unless you add some.\n *\/\nfunction dpc_csv_export_meta_keys( string $post_type ): array {\n\treturn array_values( (array) apply_filters( 'dpc_csv_export_meta_keys', array(), $post_type ) );\n}\n\n\/**\n * Turn a value into a safe CSV cell.\n *\/\nfunction dpc_csv_export_cell( mixed $value ): string {\n\t$value = is_scalar( $value ) || null === $value ? (string) $value : (string) wp_json_encode( $value );\n\n\t\/\/ Stop spreadsheet apps from running the cell as a formula.\n\tif ( '' !== $value &amp;&amp; in_array( $value&#91;0&#93;, array( '=', '+', '-', '@', \"\\t\", \"\\r\" ), true ) ) {\n\t\t$value = \"'\" . $value;\n\t}\n\n\treturn $value;\n}\n\n\/**\n * Write all posts of a type to an open stream. Returns the number of rows.\n *\n * @param resource $handle\n *\/\nfunction dpc_csv_export_write( string $post_type, $handle ): int {\n\t$taxonomies = dpc_csv_export_taxonomies( $post_type );\n\t$meta_keys  = dpc_csv_export_meta_keys( $post_type );\n\t$batch      = 200;\n\t$page       = 1;\n\t$count      = 0;\n\n\t$header = array_merge(\n\t\tarray( 'ID', 'Title', 'Slug', 'Status', 'Date', 'Modified', 'Author', 'URL', 'Excerpt' ),\n\t\t$taxonomies,\n\t\t$meta_keys\n\t);\n\tfputcsv( $handle, $header, ',', '\"', '' );\n\n\tdo {\n\t\t$query = new WP_Query(\n\t\t\tarray(\n\t\t\t\t'post_type'      =&gt; $post_type,\n\t\t\t\t'post_status'    =&gt; array( 'publish', 'future', 'draft', 'pending', 'private' ),\n\t\t\t\t'posts_per_page' =&gt; $batch,\n\t\t\t\t'paged'          =&gt; $page,\n\t\t\t\t'orderby'        =&gt; 'ID',\n\t\t\t\t'order'          =&gt; 'ASC',\n\t\t\t\t'no_found_rows'  =&gt; true,\n\t\t\t)\n\t\t);\n\n\t\tforeach ( $query-&gt;posts as $post ) {\n\t\t\t$row = array(\n\t\t\t\t$post-&gt;ID,\n\t\t\t\t$post-&gt;post_title,\n\t\t\t\t$post-&gt;post_name,\n\t\t\t\t$post-&gt;post_status,\n\t\t\t\t$post-&gt;post_date,\n\t\t\t\t$post-&gt;post_modified,\n\t\t\t\tget_the_author_meta( 'display_name', (int) $post-&gt;post_author ),\n\t\t\t\tget_permalink( $post ),\n\t\t\t\t$post-&gt;post_excerpt,\n\t\t\t);\n\n\t\t\tforeach ( $taxonomies as $taxonomy ) {\n\t\t\t\t$terms = get_the_terms( $post, $taxonomy );\n\t\t\t\t$row&#91;&#93; = is_array( $terms ) ? implode( '|', wp_list_pluck( $terms, 'name' ) ) : '';\n\t\t\t}\n\n\t\t\tforeach ( $meta_keys as $key ) {\n\t\t\t\t$row&#91;&#93; = get_post_meta( $post-&gt;ID, $key, true );\n\t\t\t}\n\n\t\t\tfputcsv( $handle, array_map( 'dpc_csv_export_cell', $row ), ',', '\"', '' );\n\t\t\t++$count;\n\t\t}\n\n\t\t++$page;\n\t} while ( count( $query-&gt;posts ) === $batch );\n\n\treturn $count;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Add it with Scripts Organizer<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/dplugins.com\/downloads\/scripts-organizer\/\">Scripts Organizer<\/a> go to <strong>Code Blocks \u2192 Add New<\/strong> and give the block a name. Set <strong>Trigger location<\/strong> to <strong>Admin only<\/strong>, tick <strong>PHP<\/strong> under <strong>Script location<\/strong>, paste the code into the PHP editor and publish. Leave <strong>Action\/Hook<\/strong> empty. The list screens and <code>admin-post.php<\/code>, which serves the download, both run in wp-admin, so the code never loads on the front end.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add the meta-key filter from <strong>Customise it<\/strong> to the same code block, below the snippet. The plugin header comment is harmless there.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How it works<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>manage_posts_extra_tablenav<\/code> fires above the list table on every post type screen, including pages, even though the hook name says posts. The button only appears for users who can <code>edit_others_posts<\/code> for that type, because the export includes other people\u2019s drafts and private posts.<\/li>\n\n\n<li>The link points to <code>admin-post.php<\/code> with a nonce tied to the post type. <code>dpc_csv_export_download()<\/code> sanitises the post type, then verifies the nonce with <code>check_admin_referer()<\/code> and checks the capability again, because the button\u2019s visibility alone is not access control.<\/li>\n\n\n<li><code>dpc_csv_export_write()<\/code> streams straight to <code>php:\/\/output<\/code>, 200 posts per query, so you never hold the whole file in memory. Posts in the trash and auto-drafts are skipped.<\/li>\n\n\n<li>Each taxonomy that has an admin UI gets one column, with term names joined by <code>|<\/code>.<\/li>\n\n\n<li><code>dpc_csv_export_cell()<\/code> puts a <code>'<\/code> in front of any value that starts with <code>=<\/code>, <code>+<\/code>, <code>-<\/code>, <code>@<\/code>, a tab or a carriage return. Without it, a post titled <code>=HYPERLINK(...)<\/code> would run as a formula when the client opens the file in Excel (CSV injection).<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Customise it<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Custom fields aren\u2019t exported by default, because post meta is full of internal keys. List the ones you want for each post type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"php\" class=\"language-php\">add_filter( 'dpc_csv_export_meta_keys', function ( array $keys, string $post_type ): array {\n\tif ( 'book' === $post_type ) {\n\t\t$keys = array_merge( $keys, array( 'isbn', 'price' ) );\n\t}\n\treturn $keys;\n}, 10, 2 );<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Need the content too?<\/strong> Add <code>'Content'<\/code> to the header array and <code>$post-&gt;post_content<\/code> to <code>$row<\/code>. Expect large cells full of block markup.<\/li>\n\n\n<li><strong>European Excel<\/strong> often expects semicolons. Change the <code>','<\/code> delimiter in both <code>fputcsv()<\/code> calls to <code>';'<\/code>.<\/li>\n\n\n<li><strong>Only some post types:<\/strong> return early in <code>dpc_csv_export_button()<\/code> unless <code>$screen-&gt;post_type<\/code> is in your own allow-list, and make the same check in the download handler.<\/li>\n\n\n<li><strong>ACF or Meta Box fields<\/strong> that store arrays are written as JSON. Format them yourself in the loop if you need readable values.<\/li>\n\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Gotchas<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The export ignores the list screen\u2019s current filters and search, and always exports every non-trashed post of that type.<\/li>\n\n\n<li>Values that legitimately start with <code>-<\/code> or <code>+<\/code>, such as negative numbers or phone numbers, also get the <code>'<\/code> prefix. Excel hides it, but other tools may not.<\/li>\n\n\n<li>On very large sites the request can still hit <code>max_execution_time<\/code>. For tens of thousands of rows, run <code>dpc_csv_export_write()<\/code> from WP-CLI or a cron job and write to a file instead of <code>php:\/\/output<\/code>.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Still setting up the post type? See <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/attach-category-to-custom-post-type\/\">Attach Category to Custom Post type<\/a>. To show the same taxonomy data on the list screen itself, use <a href=\"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/add-taxonomy-to-admin-columns\/\">Add Taxonomy to Admin columns<\/a>.<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Add an Export CSV button to any WordPress post type list screen, with nonce and capability checks, terms, meta and formula-injection protection.<\/p>\n","protected":false},"author":1,"featured_media":46890,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"WordPress Export Custom Post Type to CSV Without Plugin","description":"Add an Export CSV button to any WordPress post type list screen, with nonce and capability checks, terms, meta and formula-injection protection."},"_slim_seo_primary_term_category":0,"_slim_seo_primary_term_post_tag":0,"footnotes":""},"categories":[3],"tags":[97,142,141,143,144],"dpc_library":[204],"class_list":["post-46598","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","tag-admin","tag-csv","tag-custom-post-types","tag-export","tag-security"],"_links":{"self":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/comments?post=46598"}],"version-history":[{"count":2,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46598\/revisions"}],"predecessor-version":[{"id":46889,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/posts\/46598\/revisions\/46889"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media\/46890"}],"wp:attachment":[{"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/media?parent=46598"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/categories?post=46598"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/tags?post=46598"},{"taxonomy":"dpc_library","embeddable":true,"href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/code.dplugins.com\/wp-json\/wp\/v2\/dpc_library?post=46598"}],"curies":[{"name":"wp","href":"https:\/\/summer-heart-0930.chufeiyun1688.workers.dev:443\/https\/api.w.org\/{rel}","templated":true}]}}