Skip to content

Instantly share code, notes, and snippets.

@joshmoto
Created February 4, 2014 13:11
Show Gist options
  • Save joshmoto/8803305 to your computer and use it in GitHub Desktop.
Save joshmoto/8803305 to your computer and use it in GitHub Desktop.
Media Filter function (page, download, reports, (all post types))
<?php
/* MEDIA LIBRARY FILTER */
add_filter('parse_query', 'node_admin_posts_filter');
add_action('restrict_manage_posts', 'node_admin_posts_filter_restrict_manage_posts');
function node_admin_posts_filter($wp_query)
{
if (is_admin() && isset($_GET['page_id']) && $_GET['page_id'] != '')
{
$original_query = $wp_query;
$wp_query->set('post_parent', $_GET['page_id']);
$wp_query = $original_query;
wp_reset_postdata();
}
else if (is_admin() && isset($_GET['download_id']) && $_GET['download_id'] != '')
{
$original_query = $wp_query;
$wp_query->set('post_parent', $_GET['download_id']);
$wp_query = $original_query;
wp_reset_postdata();
}
else if (is_admin() && isset($_GET['report_id']) && $_GET['report_id'] != '')
{
$original_query = $wp_query;
$wp_query->set('post_parent', $_GET['report_id']);
$wp_query = $original_query;
wp_reset_postdata();
}
else if (is_admin() && isset($_GET['post_type']) && $_GET['post_type'] != '')
{
$original_query = $wp_query;
$wp_query->set('post_type', $_GET['post_type']);
$wp_query = $original_query;
wp_reset_postdata();
}
}
function node_admin_posts_filter_restrict_manage_posts()
{
global $wpdb;
$screen = get_current_screen();
if ($screen->id == "upload")
{
$get_pages = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE $wpdb->posts.post_type IN ('page') AND $wpdb->posts.post_status = 'publish' ORDER BY $wpdb->posts.post_title ASC");
echo '<select name="page_id">';
echo '<option value="">Filter by page...</option>';
$current_page = isset($_GET['page_id']) ? $_GET['page_id'] : '';
foreach ($get_pages as $get_page)
{
$select = null;
if ($current_page == $get_page->ID)
{
$select = ' selected="selected"';
}
echo '<option value="' . $get_page->ID . '" ' . $select . '>' . $get_page->post_title . '</option>';
}
echo '</select>';
$get_downloads = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE $wpdb->posts.post_type IN ('download') AND $wpdb->posts.post_status = 'publish' ORDER BY $wpdb->posts.post_date DESC");
echo '<select name="download_id">';
echo '<option value="">Filter by downloads...</option>';
$current_download = isset($_GET['download_id']) ? $_GET['download_id'] : '';
foreach ($get_downloads as $get_download)
{
$select = null;
if ($current_download == $get_download->ID)
{
$select = ' selected="selected"';
}
echo '<option value="' . $get_download->ID . '" ' . $select . '>' . $get_download->post_title . '</option>';
}
echo '</select>';
$get_reports = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE $wpdb->posts.post_type IN ('reports') AND $wpdb->posts.post_status = 'publish' ORDER BY $wpdb->posts.post_date DESC");
echo '<select name="report_id">';
echo '<option value="">Filter by report...</option>';
$current_report = isset($_GET['report_id']) ? $_GET['report_id'] : '';
foreach ($get_reports as $get_report)
{
$select = null;
if ($current_report == $get_report->ID)
{
$select = ' selected="selected"';
}
echo '<option value="' . $get_report->ID . '" ' . $select . '>' . $get_report->post_title . '</option>';
}
echo '</select>';
$get_types = get_post_types();
echo '<select name="post_type">';
echo '<option value="">Filter by type...</option>';
$current_type = isset($_GET['post_type']) ? $_GET['post_type'] : '';
foreach ($get_types as $get_type)
{
$select = null;
if ($current_type == $get_type)
{
$select = ' selected="selected"';
}
echo '<option value="' . $get_type . '" ' . $select . '>' . $get_type . '</option>';
}
echo '</select>';
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment