Last active
April 27, 2016 15:52
-
-
Save camaech/19b45d2c433f714c3416737d942a9b6c to your computer and use it in GitHub Desktop.
Copy all PDFs attached to a specific template type in WordPress.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
include "wp-load.php"; | |
//Usage: | |
// replace the meta_query value with your template filename | |
// change the $newfile to reflect the directory you want to copy your files to | |
$posts = new WP_Query( | |
array( | |
'post_type' => 'page', | |
'posts_per_page' => -1, | |
'post_status' => 'publish', | |
'meta_query' => | |
array( | |
array( | |
'key' => '_wp_page_template', | |
'value' => 'page-board-portal.php', | |
) | |
) | |
) | |
); | |
$posts = $posts->posts; | |
header('Content-type:text/plain'); | |
foreach($posts as $post) { | |
switch ($post->post_type) { | |
case 'revision': | |
case 'nav_menu_item': | |
break; | |
case 'page': | |
$permalink = get_page_link($post->ID); | |
break; | |
case 'post': | |
$permalink = get_permalink($post->ID); | |
break; | |
case 'attachment': | |
$permalink = get_attachment_link($post->ID); | |
break; | |
default: | |
$permalink = get_post_permalink($post->ID); | |
break; | |
} | |
echo "\n{$post->post_type}\t{$permalink}\t{$post->ID}"; | |
$attachments = get_posts( array( | |
'post_type' => 'attachment', | |
'posts_per_page' => -1, | |
'post_parent' => $post->ID, | |
'exclude' => get_post_thumbnail_id() | |
) ); | |
if ( $attachments ) { | |
foreach ( $attachments as $attachment ) { | |
//'application/pdf' | |
//'application/zip' | |
//'application/vnd.openxmlformats-officedocument.wordprocessingml.document' | |
//'application/vnd.openxmlformats-officedocument.presentationml.presentation' | |
if($attachment->post_mime_type == 'application/vnd.openxmlformats-officedocument.presentationml.presentation' ) { | |
$parsed = parse_url( wp_get_attachment_url( $attachment->ID ) ); | |
$url = '.' . dirname( $parsed [ 'path' ] ) . '/' . urlencode( basename( $parsed[ 'path' ] ) ); | |
$filename = basename($url).PHP_EOL; | |
$newfile = './wp-content/uploads/old-attachments/' . $filename . '.pptx'; | |
//$newfile = $total_attachments . '.pdf'; | |
//echo "\n$url=$newfile"; | |
//system("cp $url $newfile"); | |
//echo unlink($url); | |
} | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment