{"id":132,"date":"2016-04-28T18:02:38","date_gmt":"2016-04-28T18:02:38","guid":{"rendered":"http:\/\/www.3pixls.com\/blog\/?p=132"},"modified":"2016-04-28T18:22:47","modified_gmt":"2016-04-28T18:22:47","slug":"exclude-categories-from-recent-posts-widget","status":"publish","type":"post","link":"https:\/\/www.3pixls.com\/blog\/exclude-categories-from-recent-posts-widget\/","title":{"rendered":"Exclude Categories from Recent Posts Widget"},"content":{"rendered":"<p>[php]<br \/>\n\/**<br \/>\n * Recent_Posts widget w\/ category exclude class<br \/>\n * This allows specific Category IDs to be removed from the Sidebar Recent Posts list<br \/>\n *<br \/>\n *\/<br \/>\nclass WP_Widget_Recent_Posts_Exclude extends WP_Widget {<\/p>\n<p>    function __construct() {<br \/>\n        $widget_ops = array(&#8216;classname&#8217; =&gt; &#8216;widget_recent_entries&#8217;, &#8216;description&#8217; =&gt; __( &quot;The most recent posts on your site&quot;) );<br \/>\n        parent::__construct(&#8216;recent-posts&#8217;, __(&#8216;Recent Posts with Exclude&#8217;), $widget_ops);<br \/>\n        $this-&gt;alt_option_name = &#8216;widget_recent_entries&#8217;;<\/p>\n<p>        add_action( &#8216;save_post&#8217;, array(&amp;$this, &#8216;flush_widget_cache&#8217;) );<br \/>\n        add_action( &#8216;deleted_post&#8217;, array(&amp;$this, &#8216;flush_widget_cache&#8217;) );<br \/>\n        add_action( &#8216;switch_theme&#8217;, array(&amp;$this, &#8216;flush_widget_cache&#8217;) );<br \/>\n    }<\/p>\n<p>    function widget($args, $instance) {<br \/>\n        $cache = wp_cache_get(&#8216;widget_recent_posts&#8217;, &#8216;widget&#8217;);<\/p>\n<p>        if ( !is_array($cache) )<br \/>\n            $cache = array();<\/p>\n<p>        if ( ! isset( $args[&#8216;widget_id&#8217;] ) )<br \/>\n            $args[&#8216;widget_id&#8217;] = $this-&gt;id;<\/p>\n<p>        if ( isset( $cache[ $args[&#8216;widget_id&#8217;] ] ) ) {<br \/>\n            echo $cache[ $args[&#8216;widget_id&#8217;] ];<br \/>\n            return;<br \/>\n        }<\/p>\n<p>        ob_start();<br \/>\n        extract($args);<\/p>\n<p>        $title = apply_filters(&#8216;widget_title&#8217;, empty($instance[&#8216;title&#8217;]) ? __(&#8216;Recent Posts&#8217;) : $instance[&#8216;title&#8217;], $instance, $this-&gt;id_base);<br \/>\n        if ( empty( $instance[&#8216;number&#8217;] ) || ! $number = absint( $instance[&#8216;number&#8217;] ) )<br \/>\n            $number = 10;<br \/>\n        $exclude = empty( $instance[&#8216;exclude&#8217;] ) ? &#8221; : $instance[&#8216;exclude&#8217;];<\/p>\n<p>        $r = new WP_Query(array(&#8216;posts_per_page&#8217; =&gt; $number, &#8216;no_found_rows&#8217; =&gt; true, &#8216;post_status&#8217; =&gt; &#8216;publish&#8217;, &#8216;ignore_sticky_posts&#8217; =&gt; true, &#8216;category__not_in&#8217; =&gt; explode(&#8216;,&#8217;, $exclude) ));<br \/>\n        if ($r-&gt;have_posts()) :<br \/>\n?&gt;<br \/>\n        &lt;?php \/\/echo print_r(explode(&#8216;,&#8217;, $exclude)); ?&gt;<br \/>\n        &lt;?php echo $before_widget; ?&gt;<br \/>\n        &lt;?php if ( $title ) echo $before_title . $title . $after_title; ?&gt;<br \/>\n        &lt;ul&gt;<br \/>\n        &lt;?php  while ($r-&gt;have_posts()) : $r-&gt;the_post(); ?&gt;<br \/>\n        &lt;li&gt;&lt;a href=&quot;&lt;?php the_permalink() ?&gt;&quot; title=&quot;&lt;?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?&gt;&quot;&gt;&lt;?php if ( get_the_title() ) the_title(); else the_ID(); ?&gt;&lt;\/a&gt;&lt;\/li&gt;<br \/>\n        &lt;?php endwhile; ?&gt;<br \/>\n        &lt;\/ul&gt;<br \/>\n        &lt;?php echo $after_widget; ?&gt;<br \/>\n&lt;?php<br \/>\n        \/\/ Reset the global $the_post as this query will have stomped on it<br \/>\n        wp_reset_postdata();<\/p>\n<p>        endif;<\/p>\n<p>        $cache[$args[&#8216;widget_id&#8217;]] = ob_get_flush();<br \/>\n        wp_cache_set(&#8216;widget_recent_posts&#8217;, $cache, &#8216;widget&#8217;);<br \/>\n    }<\/p>\n<p>    function update( $new_instance, $old_instance ) {<br \/>\n        $instance = $old_instance;<br \/>\n        $instance[&#8216;title&#8217;] = strip_tags($new_instance[&#8216;title&#8217;]);<br \/>\n        $instance[&#8216;number&#8217;] = (int) $new_instance[&#8216;number&#8217;];<br \/>\n        $instance[&#8216;exclude&#8217;] = strip_tags( $new_instance[&#8216;exclude&#8217;] );<br \/>\n        $this-&gt;flush_widget_cache();<\/p>\n<p>        $alloptions = wp_cache_get( &#8216;alloptions&#8217;, &#8216;options&#8217; );<br \/>\n        if ( isset($alloptions[&#8216;widget_recent_entries&#8217;]) )<br \/>\n            delete_option(&#8216;widget_recent_entries&#8217;);<\/p>\n<p>        return $instance;<br \/>\n    }<\/p>\n<p>    function flush_widget_cache() {<br \/>\n        wp_cache_delete(&#8216;widget_recent_posts&#8217;, &#8216;widget&#8217;);<br \/>\n    }<\/p>\n<p>    function form( $instance ) {<br \/>\n        $title = isset($instance[&#8216;title&#8217;]) ? esc_attr($instance[&#8216;title&#8217;]) : &#8221;;<br \/>\n        $number = isset($instance[&#8216;number&#8217;]) ? absint($instance[&#8216;number&#8217;]) : 5;<br \/>\n        $exclude = esc_attr( $instance[&#8216;exclude&#8217;] );<br \/>\n?&gt;<br \/>\n        &lt;p&gt;&lt;label for=&quot;&lt;?php echo $this-&gt;get_field_id(&#8216;title&#8217;); ?&gt;&quot;&gt;&lt;?php _e(&#8216;Title:&#8217;); ?&gt;&lt;\/label&gt;<br \/>\n        &lt;input class=&quot;widefat&quot; id=&quot;&lt;?php echo $this-&gt;get_field_id(&#8216;title&#8217;); ?&gt;&quot; name=&quot;&lt;?php echo $this-&gt;get_field_name(&#8216;title&#8217;); ?&gt;&quot; type=&quot;text&quot; value=&quot;&lt;?php echo $title; ?&gt;&quot; \/&gt;&lt;\/p&gt;<\/p>\n<p>        &lt;p&gt;&lt;label for=&quot;&lt;?php echo $this-&gt;get_field_id(&#8216;number&#8217;); ?&gt;&quot;&gt;&lt;?php _e(&#8216;Number of posts to show:&#8217;); ?&gt;&lt;\/label&gt;<br \/>\n        &lt;input id=&quot;&lt;?php echo $this-&gt;get_field_id(&#8216;number&#8217;); ?&gt;&quot; name=&quot;&lt;?php echo $this-&gt;get_field_name(&#8216;number&#8217;); ?&gt;&quot; type=&quot;text&quot; value=&quot;&lt;?php echo $number; ?&gt;&quot; size=&quot;3&quot; \/&gt;&lt;\/p&gt;<\/p>\n<p>        &lt;p&gt;<br \/>\n            &lt;label for=&quot;&lt;?php echo $this-&gt;get_field_id(&#8216;exclude&#8217;); ?&gt;&quot;&gt;&lt;?php _e( &#8216;Exclude Category(s):&#8217; ); ?&gt;&lt;\/label&gt; &lt;input type=&quot;text&quot; value=&quot;&lt;?php echo $exclude; ?&gt;&quot; name=&quot;&lt;?php echo $this-&gt;get_field_name(&#8216;exclude&#8217;); ?&gt;&quot; id=&quot;&lt;?php echo $this-&gt;get_field_id(&#8216;exclude&#8217;); ?&gt;&quot; class=&quot;widefat&quot; \/&gt;<br \/>\n            &lt;br \/&gt;<br \/>\n            &lt;small&gt;&lt;?php _e( &#8216;Category IDs, separated by commas.&#8217; ); ?&gt;&lt;\/small&gt;<br \/>\n        &lt;\/p&gt;<br \/>\n&lt;?php<br \/>\n    }<br \/>\n}<\/p>\n<p>function WP_Widget_Recent_Posts_Exclude_init() {<br \/>\n    unregister_widget(&#8216;WP_Widget_Recent_Posts&#8217;);<br \/>\n    register_widget(&#8216;WP_Widget_Recent_Posts_Exclude&#8217;);<br \/>\n}<\/p>\n<p>add_action(&#8216;widgets_init&#8217;, &#8216;WP_Widget_Recent_Posts_Exclude_init&#8217;);<\/p>\n<p>?&gt;<br \/>\n[\/php]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[php] \/** * Recent_Posts widget w\/ category exclude class * This allows specific Category IDs to be removed from the Sidebar Recent Posts list * *\/ class WP_Widget_Recent_Posts_Exclude extends WP_Widget { function __construct() { $widget_ops = array(&#8216;classname&#8217; =&gt; &#8216;widget_recent_entries&#8217;, &#8216;description&#8217; =&gt; __( &quot;The most recent posts on your site&quot;) ); parent::__construct(&#8216;recent-posts&#8217;, __(&#8216;Recent Posts with Exclude&#8217;), [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":133,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/www.3pixls.com\/blog\/wp-json\/wp\/v2\/posts\/132"}],"collection":[{"href":"https:\/\/www.3pixls.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.3pixls.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.3pixls.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.3pixls.com\/blog\/wp-json\/wp\/v2\/comments?post=132"}],"version-history":[{"count":7,"href":"https:\/\/www.3pixls.com\/blog\/wp-json\/wp\/v2\/posts\/132\/revisions"}],"predecessor-version":[{"id":140,"href":"https:\/\/www.3pixls.com\/blog\/wp-json\/wp\/v2\/posts\/132\/revisions\/140"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.3pixls.com\/blog\/wp-json\/wp\/v2\/media\/133"}],"wp:attachment":[{"href":"https:\/\/www.3pixls.com\/blog\/wp-json\/wp\/v2\/media?parent=132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.3pixls.com\/blog\/wp-json\/wp\/v2\/categories?post=132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.3pixls.com\/blog\/wp-json\/wp\/v2\/tags?post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}