WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am looking for a widget that a user can choose a Custom POst Type Term from a list. As an example I have a Custom Post Type called events and it has multiple categories. I want the user to be able to choose to show only posts from a certain category.

Does anyone know of one or know how I can make this please?

share|improve this question
up vote 2 down vote accepted

I have build, some time before a widget, Also you can set the number of posts, the category and the order!

/****** Widget ******/
class Artdev_Category_Posts extends WP_Widget {

    function Artdev_Category_Posts() {
        $widget_ops = array('description' => 'Add Selected Category Posts to the Sidebar' );
        parent::WP_Widget(false, __('&rarr; Category List', 'artdev'),$widget_ops);      

    function widget($args, $instance) {  
        extract( $args );
        $title = esc_attr($instance['title']);
        $cat_name = esc_attr($instance['cat_name']);
        $posts = esc_attr($instance['posts']);
        $order = esc_attr($instance['order']);

        echo $before_widget; ?>

            <?php if ( $title ) echo $before_title . $title . $after_title; ?>

            <?php // The Loop
            $wq = new WP_Query();
            $wq->query( array( 'post_type' => 'events', 'category_name' => $cat_name, 'posts_per_page' => $posts, 'orderby' => $order )); 
            if( $wq->have_posts() ) :
            <ul class="widgetlist">
                <?php while($wq->have_posts()) : $wq->the_post(); ?>
                        <h3><a href="<?php the_permalink(); ?>" rel="bookmark"><?php echo get_the_title(); ?></a></h3>
                <?php endwhile; ?>
            <?php endif; wp_reset_query(); ?>

            <?//php } ?>

       <?php echo $after_widget;

   function update($new_instance, $old_instance) {                
       return $new_instance;

   function form($instance) {        
        $title = esc_attr($instance['title']);
        $cat_name = esc_attr($instance['cat_name']);
        $posts = esc_attr($instance['posts']);
        $order = esc_attr($instance['order']);

        <p><!-- Widget Title -->
            <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:','artdev'); ?></label>
            <input type="text" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" />

        <p><!-- Category Name -->
            <label for="<?php echo $this->get_field_id('cat_name'); ?>"><?php _e('Category Name:','artdev'); ?></label>
            <input type="text" name="<?php echo $this->get_field_name('cat_name'); ?>" value="<?php echo $cat_name; ?>" id="<?php echo $this->get_field_id('cat_name'); ?>" class="widefat" />

        <p><!-- Number Of Posts -->
            <label for="<?php echo $this->get_field_id('posts'); ?>"><?php _e('Number of posts to show:','artdev'); ?></label>
            <input type="text" name="<?php echo $this->get_field_name('posts'); ?>" id="<?php echo $this->get_field_id('posts'); ?>" value="<?php echo $posts; ?>" size="2" />

        <p><!-- Order Posts -->
            <label for="<?php echo $this->get_field_id('order'); ?>"><?php _e('Order of posts:','artdev'); ?></label>
            <select name="<?php echo $this->get_field_name('order'); ?>">
                <option value="title" <?php if ($order == 'title') { echo 'selected="selected"'; } ?>>Order Posts by Title</option>
                <option value="ID" <?php if ($order == 'ID') { echo 'selected="selected"'; } ?>>Order Posts by ID</option>
                <option value="date" <?php if ($order == 'date') { echo 'selected="selected"'; } ?>>Order Posts by Date</option>
                <option value="rand" <?php if ($order == 'rand') { echo 'selected="selected"'; } ?>>Randomize Posts</option>

share|improve this answer
Many thanks for the code is there any way to allow the user to choose the category from a dropdown? – Lee Dec 14 '11 at 13:08
Yes @Lee, it's possible, when i have some time i will update the answer, but if you know how to do this, use the get_terms(). Take a look here also: wordpress.org/support/topic/… – Philip Dec 14 '11 at 13:53
thanks I will try it out myself. – Lee Dec 14 '11 at 14:07

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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