0

I'm using the Annual Archive plugin which shows the year in the sidebar. I want to display in the dropdown only the years that have posts in category 17. How can i achive this?
My code is below...

    function widget( $args, $instance ) {

    extract($args);
    //$c = $instance['count'] ? '1' : '0';
    //$d = $instance['dropdown'] ? '1' : '0';

    $format = empty($instance['format']) ? 'html' : apply_filters('widget_type', $instance['format']);
    $type = empty($instance['type']) ? 'yearly' : apply_filters('widget_type', $instance['type']);
    $before = empty($instance['before']) ? '' : apply_filters('widget_type', $instance['before']);
    $after = empty($instance['after']) ? '' : apply_filters('widget_type', $instance['after']);
    $limit = apply_filters('widget_limit', $instance['limit']);
    $title = apply_filters('widget_title', empty($instance['title']) ? __('Annual Archive', 'anarch') : $instance['title'], $instance, $this->id_base);

    //$wpdb->query('select * from my_plugin_table where foo = "bar"');

    echo $before_widget;
    if ( $title )
        echo $before_title . $title . $after_title;

    if ($format == 'option') {
        $dtitle = __('Select Year', 'anarch');
        if ($type == 'monthly'){
            $dtitle = __('Select Month', 'anarch');
        }
        else if($type == 'weekly'){
            $dtitle = __('Select Week', 'anarch');
        }
        else if($type == 'daily'){
            $dtitle = __('Select Day', 'anarch');
        }
        else if($type == 'postbypost' || $type == 'alpha'){
            $dtitle = __('Select Post', 'anarch');
        }

    ?>
    <select name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> <option value=""><?php echo esc_attr(__($dtitle, 'anarch')); ?></option>  <?php wp_get_archives(apply_filters('widget_archive_dropdown_args', array('type' => $type, 'format' => 'option', 'show_post_count' => $c, 'before' => $before, 'after' => $after, 'limit' => $limit))); ?> </select>
    <?php
    } else {
    ?>
    <ul>
    <?php wp_get_archives(apply_filters('widget_archive_args', array('type' => $type, 'limit' => $limit, 'format' => $format, 'before' => $before, 'after' => $after, 'show_post_count' => $c))); ?>
    </ul>
    <?php
    }

    echo $after_widget;
}
1
  • What is term_taxonomy_id for the categoryId 17? – Vinod Dalvi Feb 18 '13 at 11:16
0

Try this, you need to use two filters "getarchives_where" and "getarchives_join" to alter the way archive are shown and include your category 17..

function widget( $args, $instance ) {
extract($args);
    //$c = $instance['count'] ? '1' : '0';
    //$d = $instance['dropdown'] ? '1' : '0';

    $format = empty($instance['format']) ? 'html' : apply_filters('widget_type', $instance['format']);
    $type = empty($instance['type']) ? 'yearly' : apply_filters('widget_type', $instance['type']);
    $before = empty($instance['before']) ? '' : apply_filters('widget_type', $instance['before']);
    $after = empty($instance['after']) ? '' : apply_filters('widget_type', $instance['after']);
    $limit = apply_filters('widget_limit', $instance['limit']);
    $title = apply_filters('widget_title', empty($instance['title']) ? __('Annual Archive', 'anarch') : $instance['title'], $instance, $this->id_base);

    //$wpdb->query('select * from my_plugin_table where foo = "bar"');

    echo $before_widget;
    if ( $title )
        echo $before_title . $title . $after_title;

    if ($format == 'option') {
        $dtitle = __('Select Year', 'anarch');
        if ($type == 'monthly'){
            $dtitle = __('Select Month', 'anarch');
        }
        else if($type == 'weekly'){
            $dtitle = __('Select Week', 'anarch');
        }
        else if($type == 'daily'){
            $dtitle = __('Select Day', 'anarch');
        }
        else if($type == 'postbypost' || $type == 'alpha'){
            $dtitle = __('Select Post', 'anarch');
        }

add_filter( 'getarchives_where', 'customarchives_where' );
add_filter( 'getarchives_join', 'customarchives_join' );

function customarchives_join( $x ) {

    global $wpdb;

    return $x . " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)";

}

function customarchives_where( $x ) {

    global $wpdb;

    $include = '17'; // category id to include
    return $x . " AND $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id IN ($include)";

}

    ?>
    <select name="archive-dropdown" onchange='document.location.href=this.options[this.selectedIndex].value;'> <option value=""><?php echo esc_attr(__($dtitle, 'anarch')); ?></option> <?php wp_get_archives(apply_filters('widget_archive_dropdown_args', array('type' => $type, 'format' => 'option', 'show_post_count' => $c, 'before' => $before, 'after' => $after, 'limit' => $limit))); ?> </select>
    <?php
    } else {
    ?>
    <ul>
    <?php wp_get_archives(apply_filters('widget_archive_args', array('type' => $type, 'limit' => $limit, 'format' => $format, 'before' => $before, 'after' => $after, 'show_post_count' => $c))); ?>
    </ul>
    <?php
    }

    echo $after_widget;

remove_filter( 'getarchives_where', 'customarchives_where' );
remove_filter( 'getarchives_join', 'customarchives_join' );

}
1
  • yes i used this and it worked perfectly... – Shwet Feb 20 '13 at 9:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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