Cloud Sky
Cloud Sky

Reputation: 117

Wordpress - Exclude page from Sidebar widget?

I'm trying to hide a page from the 'Most Popular Post' widget in the sidebar. I've got the 'WP Hide Post' plugin, It helps me hide from everywhere but that doesn't hide a page from the the widget.

Upvotes: 0

Views: 155

Answers (1)

Florian
Florian

Reputation: 773

Use the exclude argument in the WP_Query.

<?php 
$excudeID = 30;// the post_id off the excluded page
$popularpost = new WP_Query( array( 'post__not_in' => array($excludeID)  ) );

//use this query in your widget
while ( $popularpost->have_posts() ) : $popularpost->the_post();

the_title();

endwhile;
?>

Another option could be to write your own widget:

class drc_PopulairPostsWithoutThatone extends WP_Widget
{

    public function __construct()
    {
        // Instantiate the parent object
        parent::__construct(false, 'Populair Posts Title');
    }

    public function widget($args, $instance)
    {
        global $wpdb;
        $excludeId = 30;
        $return    = "<ul>";
        $query     = $wpdb->prepare("SELECT * FROM wp_popularpostsdata, wp_posts WHERE wp_popularpostsdata.postid = $wpdb->posts.ID AND $wpdb->posts.post_type = 'page' AND wp_popularpostsdata.postid != %d ORDER BY pageviews DESC limit 10", $excludeId);
        $tops      = $wpdb->get_results($query);
        foreach ($tops as $top) {
            $return .= '<li><a href="' . get_permalink($top->postid) . '">' . get_the_title($top->postid) . '</a> - ' . $top->pageviews . '</li>';
        }
        $return .= "</ul>";
        return $return;
    }

    public function update($new_instance, $old_instance)
    {
        // Save widget options
    }

    public function form($instance)
    {
        // Output admin widget options form
    }
}

function drc_register_widgets()
{
    register_widget('drc_PopulairPostsWithoutThatone');
}

add_action('widgets_init', 'drc_register_widgets');

Query taken from: https://wordpress.org/support/topic/shortcode-get-the-least-popular-posts/ Documentation about widgets: https://codex.wordpress.org/Function_Reference/register_widget

Upvotes: 1

Related Questions