Gary Woods
Gary Woods

Reputation: 1011

Exclude current post from the output

I have written a Wordpress widget that displays a custom amount of posts from a custom tag (feel free to use it). Here is the code:

    <?php 
    $category = get_the_category();
    $current_category = $category[0]->term_id;
    ?>

    <?php query_posts("showposts=".$posts_number."&cat=".$current_category."&tag=featured"); // the tag
    if (have_posts()) : while (have_posts()) : the_post(); ?>
        <div class="block-post clearfix">
            <?php
                $thumb = '';
                $width = 287;
                $height = 162;
                $classtext = 'post-image';
                $titletext = get_the_title();
                $thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Recent');
                $thumb = $thumbnail["thumb"];
            ?>

            <?php if($thumb <> '' && get_option('aggregate_thumbnails_index') == 'on') { ?>
                <div class="thumb">
                    <a href="<?php the_permalink(); ?>">
                        <?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
                        <span class="overlaybig"></span>
                    </a>
                </div>  <!-- end .post-thumbnail -->
            <?php } ?>

            <div class="recenttitle"><h3 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3></div>
        </div> <!-- end .block-post -->
    <?php endwhile; endif; wp_reset_query(); ?>

My question is, how can I exclude the current post from the output? The problem is that it does not check if the user is currently viewing any of the posts that it outputs. How can I adjust the code so that it skips the current post that the user is on?

I am pretty sure that this is an easy fix, but I am lost at the moment.

UPDATE: Here is the code for the entire widget, including the fix provided by maiorano84:

<?php class ArtificePinned extends WP_Widget
{
    function ArtificePinned(){
        $widget_ops = array('description' => 'Displays posts filtered by current category and the tag pinned');
        $control_ops = array('width' => 400, 'height' => 300);
        parent::WP_Widget(false,$name='Artifice Pinned',$widget_ops,$control_ops);
    }

  /* Displays the Widget in the front-end */
    function widget($args, $instance){
        extract($args);
        $title = apply_filters('widget_title', empty($instance['title']) ? ' ' : $instance['title']);
        $posts_number = empty($instance['posts_number']) ? '' : (int) $instance['posts_number'];

        echo $before_widget;

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

<?php 
$category = get_the_category();
$current_category = $category[0]->term_id;
$qarr = array(
    'posts_per_page' => $posts_number,
    'cat' => $current_category,
    'tag' => 'featured',
    'post__not_in' => get_the_ID()
);
$q = new WP_Query($qarr);
if($q->have_posts()) : while ($q->have_posts()) : $q->the_post();
?>
    <div class="block-post clearfix">
        <?php
            $thumb = '';
            $width = 287;
            $height = 162;
            $classtext = 'post-image';
            $titletext = get_the_title();
            $thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Recent');
            $thumb = $thumbnail["thumb"];
        ?>

        <?php if($thumb <> '' && get_option('aggregate_thumbnails_index') == 'on') { ?>
            <div class="thumb">
                <a href="<?php the_permalink(); ?>">
                    <?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
                    <span class="overlaybig"></span>
                </a>
            </div>  <!-- end .post-thumbnail -->
        <?php } ?>

        <div class="recenttitle"><h3 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3></div>
    </div> <!-- end .block-post -->
<?php endwhile; endif;?>

<?php
        echo $after_widget;
    }

  /*Saves the settings. */
    function update($new_instance, $old_instance){
        $instance = $old_instance;
        $instance['title'] = stripslashes($new_instance['title']);
        $instance['posts_number'] = (int) $new_instance['posts_number'];

        return $instance;
    }

  /*Creates the form for the widget in the back-end. */
    function form($instance){
        //Defaults
        $instance = wp_parse_args( (array) $instance, array('title'=>' ', 'posts_number'=>'7') );

        $title = esc_attr($instance['title']);
        $posts_number = (int) $instance['posts_number'];

        # Title
        echo '<p><label for="' . $this->get_field_id('title') . '">' . 'Title:' . '</label><input class="widefat" id="' . $this->get_field_id('title') . '" name="' . $this->get_field_name('title') . '" type="text" value="' . $title . '" /></p>';
        # Number Of Posts
        echo '<p><label for="' . $this->get_field_id('posts_number') . '">' . 'Number of Posts:' . '</label><input class="widefat" id="' . $this->get_field_id('posts_number') . '" name="' . $this->get_field_name('posts_number') . '" type="text" value="' . $posts_number . '" /></p>';
        # Category ?>

        <?php
    }

}// end ArtificePinned class

function ArtificePinnedInit() {
  register_widget('ArtificePinned');
}

add_action('widgets_init', 'ArtificePinnedInit');

?>

Upvotes: 0

Views: 810

Answers (1)

maiorano84
maiorano84

Reputation: 11951

Don't use query_posts, as its intention is to modify the default Wordpress Loop. Use WP Query instead.

To answer your question, the solution is pretty simple. I've taken the liberty of giving you this example using WP_Query:

<?php
$qarr = array(
    'posts_per_page' => $posts_number,
    'cat' => $current_category,
    'tag' => 'featured',
    'post__not_in' => get_the_ID()
);
$q = new WP_Query($qarr);
if($q->have_posts()) : while ($q->have_posts()) : $q->the_post();
?>

Bear in mind, I've also changed 'showposts' to 'posts_per_page', as showposts was deprecated in version 2.1

UPDATE: Your code should now look like this:

<?php 
$category = get_the_category();
$current_category = $category[0]->term_id;
$qarr = array(
    'posts_per_page' => $posts_number,
    'cat' => $current_category,
    'tag' => 'featured',
    'post__not_in' => get_the_ID()
);
$q = new WP_Query($qarr);
if($q->have_posts()) : while ($q->have_posts()) : $q->the_post();
?>
    <div class="block-post clearfix">
        <?php
            $thumb = '';
            $width = 287;
            $height = 162;
            $classtext = 'post-image';
            $titletext = get_the_title();
            $thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Recent');
            $thumb = $thumbnail["thumb"];
        ?>

        <?php if($thumb <> '' && get_option('aggregate_thumbnails_index') == 'on') { ?>
            <div class="thumb">
                <a href="<?php the_permalink(); ?>">
                    <?php print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext); ?>
                    <span class="overlaybig"></span>
                </a>
            </div>  <!-- end .post-thumbnail -->
        <?php } ?>

        <div class="recenttitle"><h3 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3></div>
    </div> <!-- end .block-post -->
<?php endwhile; endif;?>

FIX:

The problem is stemming from this:

'post__not_in' => get_the_ID()

The problem is 'post__not_in' expects an array. Not an integer. My apologies, that was my mistake. Please change that line of code to:

'post__not_in' => array(get_the_ID())

and you should be good.

Upvotes: 1

Related Questions