Last active
March 17, 2016 19:54
-
-
Save micc83/6413451 to your computer and use it in GitHub Desktop.
Export WordPress Posts by category or custom taxonomy with attachments
This file contains 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 | |
/** | |
* Export WordPress Posts by category or custom taxonomy with attachments | |
* | |
* Add the needed select boxes to the ui | |
*/ | |
add_action( 'export_filters', 'export_custom_taxonomy_fields' ); | |
function export_custom_taxonomy_fields() { | |
?> | |
<!-- Styles --> | |
<style> | |
#custom_taxonomy_post_filter ul { | |
display: inline-block; | |
padding:20px;border:1px solid #eee;margin: 5px 0; | |
background: #fafafa; | |
-webkit-border-radius: 3px; | |
-moz-border-radius: 3px; | |
-o-border-radius: 3px; | |
-ms-border-radius: 3px; | |
border-radius: 3px; | |
} | |
#custom_taxonomy_post_filter ul label { | |
width: 120px;display: inline-block; | |
} | |
#custom_taxonomy_post_filter ul select { | |
min-width: 150px; | |
} | |
#cat { | |
display: none; | |
} | |
</style> | |
<!-- Taxonomy box --> | |
<li id="custom_taxonomy_post_filter"> | |
<ul> | |
<li><h3><strong>Filter by taxonomies:</strong></h3></li> | |
<?php | |
// Get all post taxonomies | |
$taxonomies = get_object_taxonomies( 'post' ); | |
// For each taxonomy get a select input with al terms | |
foreach ( $taxonomies as $taxonomy ) { | |
$tax = get_taxonomy( $taxonomy ); | |
$name = $tax->labels->name; | |
$terms = get_terms( $taxonomy ); | |
?> | |
<li class="custom_taxonomy_post_filter"> | |
<label for=""><?php echo $name; ?></label> | |
<select name="<?php echo $taxonomy; ?>_custom_taxonomy_post_filter"> | |
<option value="0"><?php _e( 'All' ); ?></option> | |
<?php foreach ( $terms as $term ): ?> | |
<option value="<?php echo $term->term_id; ?>"><?php echo $term->name; ?></option> | |
<?php endforeach; ?> | |
</select> | |
</li> | |
<?php | |
} | |
?> | |
<li class="checkbox"> | |
<label for="export_attachments_ctpt">Export attachments</label> | |
<input type="checkbox" id="export_attachments_ctpt" name="export_attachments_ctpt" value="1" checked/> | |
</li> | |
</ul> | |
</li> | |
<!-- Add our new Taxonomy box --> | |
<script type="text/javascript"> | |
//<![CDATA[ | |
jQuery(document).ready(function($){ | |
$('#post-filters').append($('#custom_taxonomy_post_filter')); | |
}); | |
//]]> | |
</script> | |
<?php | |
} | |
/** | |
* Add a filter trough the only action present in includes/export.php | |
*/ | |
add_action( 'export_wp', 'edit_default_export_query' ); | |
function edit_default_export_query() { | |
add_filter( 'query', 'edit_default_export_query_filter' ); | |
add_filter( 'query', 'edit_default_author_export_query_filter' ); | |
} | |
/** | |
* Edit default export query behavior | |
* | |
* @var String $query Wpdb query | |
* | |
* @return String Wpdb query | |
*/ | |
function edit_default_export_query_filter( $query ) { | |
// Applied only to first query | |
remove_filter( 'query', 'edit_default_export_query_filter') ; | |
global $wpdb, $exported_posts_ids; | |
// Only for posts export | |
if ( 'posts' == $_GET['content'] ){ | |
// Get all post taxonomies | |
$taxonomies = get_object_taxonomies( 'post' ); | |
// Build where clause | |
$where = ''; | |
foreach ( $taxonomies as $taxonomy ) { | |
$taxonomy_field = $taxonomy . '_custom_taxonomy_post_filter'; | |
if ( (int) $_GET[ $taxonomy_field ] ) | |
$where .= $wpdb->prepare( " AND {$wpdb->term_relationships}.term_taxonomy_id = %d ", (int) $_GET[ $taxonomy_field ] ); | |
} | |
// For testing purpose only | |
// $where = $wpdb->prepare ( " AND {$wpdb->posts}.ID = %d ", 6332 ); | |
// Build join clause | |
if ( !empty( $where ) && false === strpos( $query, 'INNER JOIN' ) ){ | |
$join = "INNER JOIN {$wpdb->term_relationships} ON ({$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id)"; | |
$join_position = strpos( $query, $wpdb->posts ) + strlen( $wpdb->posts ) + 1; | |
$query = substr_replace( $query, $join, $join_position, 0 ); | |
} | |
// Rebuild query | |
$query = $query . $where; | |
// Export attachments | |
if ( isset( $_GET['export_attachments_ctpt'] ) ){ | |
// Get selected post IDs | |
$exported_posts_ids = $wpdb->get_col( $query ); | |
// Build an array of attachement IDs | |
$attachment_ids = array(); | |
foreach ( $exported_posts_ids as $post_id ) { | |
$attachments = get_children( array( | |
'post_parent' => $post_id, | |
'post_type' => 'attachment' | |
) ); | |
foreach ( $attachments as $key => $attachment ) | |
$attachment_ids[] = $attachment->ID; | |
} | |
// Build the new query | |
if ( !empty( $attachment_ids ) ){ | |
$attachment_ids = implode( ', ', $attachment_ids ); | |
$attachment_query = "(SELECT ID FROM {$wpdb->posts} WHERE {$wpdb->posts}.ID IN ( {$attachment_ids} ))"; | |
$query = "({$query}) UNION ({$attachment_query})"; | |
} | |
} | |
} | |
return $query; | |
} | |
/** | |
* Limit author export only to selected posts | |
* | |
* @var String $query Wpdb query | |
* | |
* @return String Wpdb query | |
*/ | |
function edit_default_author_export_query_filter( $query ) { | |
global $wpdb, $exported_posts_ids; | |
// Check if its the author query | |
if ( false !== strpos( $query, 'SELECT DISTINCT post_author' ) && !empty( $exported_posts_ids ) ) { | |
// Applied only to first query | |
remove_filter( 'query', 'edit_default_author_export_query_filter') ; | |
$exported_posts_ids = implode( ', ', $exported_posts_ids ); | |
$query .= " AND {$wpdb->posts}.ID IN ( {$exported_posts_ids} )"; | |
} | |
return $query; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How would you go about allowing this to work with a custom post type and its custom taxonomy?