0

Has anyone written or can suggest a plugin that would replace the filter-by-month in the Post Administration screen with filter-by-week-number instead?

e.g. a drop down option for "Week of 05/31-06/06, 2011"

1 Answer 1

2

here you go , its something i quickly cooked up and it seems to work fine:

enter image description here

<?php
/*
Plugin Name: admin-filter-by-week
Plugin URI: http://en.bainternet.info
Description: answer to Filtering posts on Post Administration Page by Week Number instead of by Month
http://wordpress.stackexchange.com/questions/19186/filtering-posts-on-post-administration-page-by-week-number-instead-of-by-month
Version: 1.0
Author: Bainternet
Author URI: http://en.bainternet.info
*/

add_filter( 'parse_query', 'week_admin_posts_filter' );
add_action( 'restrict_manage_posts', 'ba_admin_posts_filter_restrict_manage_posts' );

function week_admin_posts_filter( $query )
{
    global $pagenow;
    if ( is_admin() && $pagenow=='edit.php' && isset($_GET['weekly_archive-dropdown']) && $_GET['weekly_archive-dropdown'] != '') {
        $link =  $_GET['weekly_archive-dropdown'];  
        //http://en.bainternet.info/?m=2011&w=22
        $pos = strpos($link, '?m=');
        if ($pos !== false) {
            $m = substr($link, $pos + 3);
            $wpos = strpos($m, '&w=');
            $w = substr($m, $wpos + 3);
            $m = substr($m, 0, $wpos + 3);
            $query->query_vars['year'] = $m;
            $query->query_vars['w'] = $w;
            $query->query_vars['post_status'] = array('publish','pending','draft','future','private');
        }
    }
}

function hack_weekly_archives($w){
    $types = "IN('publish','pending','draft','future','private')";
    $w = str_replace("= 'publish'",$types,$w);
    return $w;
}


function ba_admin_posts_filter_restrict_manage_posts()
{
    add_filter('getarchives_where','hack_weekly_archives');
    ?>
    <select name="weekly_archive-dropdown">
    <option value=""><?php echo esc_attr( __( 'Select Week' ) ); ?></option> 
    <?php wp_get_archives( 'type=weekly&format=option&show_post_count=1' ); ?>
    </select>
    <?php
    remove_filter('getarchives_where','hack_weekly_archives');
}
9
  • The one trick with doing it this way is that it only shows the weeks for items that are published--but not for items which are either draft or scheduled. I'm toying with it to try and see how to modify it to show weeks that are scheduled, too. Jun 3, 2011 at 23:52
  • @Justin : i updated the code to show posts of all types
    – Bainternet
    Jun 4, 2011 at 0:38
  • Thanks. It still doesn't seem to be showing the weeks in which there are only scheduled posts (e.g. no published posts). I'll look into it more tomorrow. Jun 4, 2011 at 1:31
  • I think in the ba_admin_posts_filter_restrict_manage_posts, if I understand it right, wp_get_archives is only pulling weeks in which there are regular posts? and then week_admin_posts_filter is showing any posts published in those weeks. Is there any way to have wp_get_archives pull the weeks for which there are scheduled posts as well as published posts? Jun 4, 2011 at 1:34
  • Instead of using wp_get_archives, I'd just make a new query object with WP_Query and build the weeks list out like that. Jun 4, 2011 at 1:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.