Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8514337
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:50:18+00:00 2026-06-11T04:50:18+00:00

I have written a WordPress widget that displays a custom amount of posts from

  • 0

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');

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:50:20+00:00Added an answer on June 11, 2026 at 4:50 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a function that extract the domain from hostname. e.g. www.domain.com ->
I have written a simple jQuery script that changes the hash tag of a
I have written a loop in wordpress that loops through the post titles and
I have written 2 wordpress plugins and both use jquery 1.6. I load the
I have written an AIR Application that downloads videos and documents from a server.
I have written a function for a multilevel wordpress menu, but I'd like it
I have written this code inside a servlet to delete certain records from three
I have written a function that sorts a big scale of data. To test
I have written a very simple CTE expression that retrieves a list of all
I've written a PHP script to access the latest item from the wordpress database,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.