Skip to content

Instantly share code, notes, and snippets.

@ericmann
Created February 11, 2014 04:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericmann/8929492 to your computer and use it in GitHub Desktop.
Save ericmann/8929492 to your computer and use it in GitHub Desktop.
Allow Trash Posts
<?php
class My_Post_Work {
public function __construct() {
add_action( 'init', array( $this, 'custom_rewrites' ) );
add_filter( 'posts_results', array( $this, 'make_trash_public' ), 10, 2 );
add_filter( 'the_posts', array( $this, 'make_trash_private' ), 10, 2 );
}
/**
* Add custom rewrite rules to make sure individual URLs direct to the appropriate resource.
*/
public function custom_rewrites() {
add_rewrite_tag( '%my-status%', '(.+)' );
add_rewrite_rule( '^rawstuff/([0-9]+)/?$', 'index.php?my-status=any&p=$matches[1]', 'top' );
}
/**
* Hack the Trash post status to make it queryable.
*
* @param array $posts
* @param WP_Query $query
*
* @global array $wp_post_statuses;
*
* @return array
*/
public function make_trash_public( $posts, $query ) {
$status = get_query_var( 'my-status' );
if ( ! empty( $status ) ) {
global $wp_post_statuses;
// Remove it so we don't bork anything else
set_query_var( 'my-status', '' );
$wp_post_statuses['my_trash'] = $wp_post_statuses['trash'];
$wp_post_statuses['trash']->public = true;
}
return $posts;
}
/**
* Hack the Trash post status to make it non-queryable again.
*
* @param $posts
* @param $query
*
* @global array $wp_post_statuses
*
* @return array
*/
public function make_trash_private( $posts, $query ) {
global $wp_post_statuses;
if ( isset( $wp_post_statuses['my_trash'] ) ) {
$wp_post_statuses['trash'] = $wp_post_statuses['my_trash'];
unset( $wp_post_statuses['my_trash'] );
}
return $posts;
}
}
new My_Post_Work();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment