• 0

Excluding post or category from random post widget


Question

Hey there guys.

Ok thing is I really suck at php and I'm trying to exlude a post, or possible a category, either one would work, from a random post widget I have.

If you check out the site you can see the widget on the right side (random paranormal movies) is displaying my sticky welcome message at the top.

I know the category id and post id and have tried a few things to no avail.

Anyway any help would sure be welcome:

<?php

/*

Plugin Name: Simple Random Posts Widget

Plugin URI: http://infobak.nl/si...for-wordpress//

Version: 1.26

Description: Widget which displays random posts

Author: Jan Meeuwesen

Author URI: http://infobak.nl/si...-for-wordpress/

License: GPLv2

Copyright 2012 Jan Meeuwesen

*/

define("DefNoOfPosts", "5"); // default number of random posts to display

class SimpleRandomPostsWidget extends WP_Widget {

function SimpleRandomPostsWidget()

{

parent::WP_Widget( false, 'Simple Random Posts', array('description' => 'Random posts widget') );

}

function widget($args, $instance)

{

global $NewSimpleRandomPosts;

$title = empty( $instance['title'] ) ? 'Simple Random Posts' : $instance['title'];

echo $args['before_widget'];

echo $args['before_title'] . $title . $args['after_title'];

echo $NewSimpleRandomPosts->GetSimpleRandomPosts( empty( $instance['ShowPosts'] ) ? DefNoOfPosts : $instance['ShowPosts'] );

echo $args['after_widget'];

}

function update($new_instance)

{

return $new_instance;

}

function form($instance)

{

?>

<p>

<label for="<?php echo $this->get_field_id('title'); ?>"><?php echo 'Title:'; ?></label>

<input type="text" name="<?php echo $this->get_field_name('title'); ?>" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" value="<?php echo esc_attr($instance['title']); ?>" />

</p>

<p>

<label for="<?php echo $this->get_field_id('ShowPosts'); ?>"><?php echo 'Number of entries:'; ?></label>

<input type="text" name="<?php echo $this->get_field_name('ShowPosts'); ?>" id="<?php echo $this->get_field_id('ShowPosts'); ?>" value="<?php if ( empty( $instance['ShowPosts'] ) ) { echo esc_attr(DefNoOfPosts); } else { echo esc_attr($instance['ShowPosts']); } ?>" size="3" />

</p>

<?php

}

}

class SimpleRandomPosts {

function GetSimpleRandomPosts($noofposts)

{

rewind_posts();

query_posts('orderby=rand&showposts='.$noofposts);

if (have_posts()) :

echo '<ul>';

while (have_posts()) : the_post();

echo '<div id="post-'.get_the_ID().'"><li><a href="'.get_permalink().'">'.get_the_title().'</a></li></div>';

endwhile;

echo '</ul>';

endif;

wp_reset_query();

}

}

$NewSimpleRandomPosts = new SimpleRandomPosts();

function SimpleRandomPosts_widgets_init()

{

register_widget('SimpleRandomPostsWidget');

}

add_action('widgets_init', 'SimpleRandomPosts_widgets_init');

?>

8 answers to this question

Recommended Posts

  • 0

Example

&lt;?php $query = new WP_Query( array( 'post_type' =&gt; 'post', 'post__not_in' =&gt; array( 2, 5, 12, 14, 20 ) ) ); ?&gt;

Those ID's in the array of "post__not_in" are what can be generally excluded.

All of your custom querying options are on this page in the WordPress Codex.

http://codex.wordpress.org/Class_Reference/WP_Query

  • 0

Example

&lt;?php $query = new WP_Query( array( 'post_type' =&gt; 'post', 'post__not_in' =&gt; array( 2, 5, 12, 14, 20 ) ) ); ?&gt;

Those ID's in the array of "post__not_in" are what can be generally excluded.

All of your custom querying options are on this page in the WordPress Codex.

http://codex.wordpre...erence/WP_Query

Thanks again mate for the help.

What is confusing me though is that does the plugin which the original code is from write into the query file? Do I have to go into the query.php file and make the changes in there or can i make them in the original plugin file?

Sorry man you got me a little lost here. Coding has never been my strong suit.

  • 0

Thanks again mate for the help.

What is confusing me though is that does the plugin which the original code is from write into the query file? Do I have to go into the query.php file and make the changes in there or can i make them in the original plugin file?

Sorry man you got me a little lost here. Coding has never been my strong suit.

no the plugin is using wp_query (query.php)

just edit the code directly, edit the plugin.

Either from the files or admincp>plugins>edit

  • 0

no the plugin is using wp_query (query.php)

just edit the code directly, edit the plugin.

Either from the files or admincp>plugins>edit

The code I posted is from the plugin.

I also can't find any obvious place where I could stuff the exlude or that is the -.

You think I might have to include the line he mentioned before inside the code:

<?php $query = new WP_Query( array( 'post_type' => 'post', 'post__not_in' => array( 2, 5, 12, 14, 20 ) ) ); ?>

This topic is now closed to further replies.
  • Posts

    • Win11Debloat 2026.06.14 by Razvan Serea Win11Debloat is a lightweight, easy to use PowerShell script that allows you to quickly declutter and customize your Windows experience. It can remove pre-installed bloatware apps, disable telemetry, remove intrusive interface elements and much more. The script also includes many features that system administrators and power users will enjoy. Such as a powerful command-line interface, support for Windows Audit mode and the option to make changes to other Windows users. All changes made by Win11Debloat can be easily reversed, and most removed apps can be restored via the Microsoft Store. A full guide on how to undo the changes is available here. Win11Debloat features: Below is an overview of the key features and functionality offered by Win11Debloat. Please refer to the wiki for more information about the default settings preset. Remove a wide variety of preinstalled apps. Click here for more info. Disable telemetry, diagnostic data, activity history, app-launch tracking & targeted ads. Disable tips, tricks, suggestions & ads across Windows. Disable Windows location services & app location access. Disable Find My Device location tracking. Disable 'Windows Spotlight' and tips & tricks on the lock screen. Disable 'Windows Spotlight' desktop background option. Disable ads, suggestions and the MSN news feed in Microsoft Edge. Hide Microsoft 365 ads on the Settings 'Home' page, or hide the 'Home' page entirely. Disable & remove Microsoft Copilot. Disable Windows Recall. Disable Click to Do, AI text & image analysis tool. Prevent AI service (WSAIFabricSvc) from starting automatically. Disable AI Features in Edge. Disable AI Features in Paint. Disable AI Features in Notepad. Disable the Drag Tray for sharing & moving files. Restore the old Windows 10 style context menu. Turn off Enhance Pointer Precision, also known as mouse acceleration. Disable the Sticky Keys keyboard shortcut. Disable Storage Sense automatic disk cleanup. Disable fast start-up to ensure a full shutdown. ...and more. Once you’ve downloaded the Win11Debloat file (Get.ps1), just follow these quick steps: Locate the Get.ps1 script file. Right-click the file and select Run with PowerShell from the context menu. If prompted by User Account Control (UAC), select Yes to grant the script the necessary administrative permissions. Win11Debloat 2026.06.14 changes: This is a minor release that hopefully addresses the false positives in Windows Defender and Bitdefender that prevented users from downloading and/or running Win11Debloat. Refactor Get-RegFileOperations.ps1 to address false positives by @Raphire in #626 Add logging around WinGet app retrieval and increase timeout to 20s by @Raphire Download: Win11Debloat 2026.06.14 | Open Source View: Win11Debloat Home Page | Screenshots 1| 2 Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Still using Microsoft Money 2005 in 2026 here!
    • I have a couple to mention, and they still run great on Windows 11 Adobe Lightroom Version 2 Alcohol 120% CLZ Book, Comic, Game, Movie, & Music Collector (PC - No longer sold / Grandfathered in - now mobile apps/online only) DVDDecrypter ISO Buster Pro version 1.9.1 (Still supports HD-DVD too) Nero Burning Rom 8 (Only the burning software, no backup, media converter, etc)   OpenAL (Runtime) - GuildWars 1 Reforged still uses it for 3d headphone audio PowerDVD 12 Ultra SPTD (SCSI Pass through Direct Driver) UltraISO Windows Media Encoder 9 WinImage You can tell I still sport an optical drive    
    • Linux 7.1 arrives with an NTFS overhaul and major hardware performance boosts by Paul Hill The founder of the Linux kernel has just announced the availability of Linux 7.1. This is a stable version of the kernel that will now be tested by various Linux distributions before it is shipped to users through update managers. Some users, like those on Debian, for example, might not get it for a long time, if at all, while Fedora users can expect it in the near future. With Linux 7.1 out on time, the merge window for Linux 7.2 is now open, giving contributors the opportunity to send in major new features that have been waiting for the last two months. Torvalds warned that he is currently travelling and will be in another timezone, so timing for the merge window may be irregular due to timezone differences and limited internet access. Torvalds said that he has already fetched early pull requests to allow him to do some offline work, but the travel could still cause disruption. Right now, he is not planning to extend the release, but did consider it. He said he might later regret not extending, though. In terms of this last week of development for Linux 7.1, Torvalds said there were no major or alarming changes. This week consisted mostly of smaller driver updates to GPU, networking, and sound, networking fixes, trace tooling fixes, and misc minor fixes. The shortlog this week lists fixes for driver bugs, memory leaks, I/O and USB fixes, networking and RDMA fixes, DRM/graphics fixes, and tooling and verification improvements. Specific fixes include USB series heap-overflow and buffer overflow fixes, and multiple use-after-free, memory-leak, and refcount corrections across subsystems such as i2c, zram, gpio, and net. There are fixes for graphics drivers, including amdgpu, i915, and virtio, as well as hypervisor and virtualization tweaks affecting mshv, vmbus, and hyperv. According to Phoronix, anyone running Linux 7.1 should look out for the new NTFS driver, Intel FRED for improved performance on Panther Lake and future CPUs, faster graphics with Intel Arc Battlemage, and improvements for older AMD Radeon GPUs. If you are running Linux on your computer and everything is fine, then you don’t need to worry about updating to Linux 7.1 as a priority; just wait for it to be pushed to you. If you have tried Linux on hardware but it didn’t work properly, trying again with a distro that uses Linux 7.1 could cause Linux to work on your machine, thanks to the new hardware support.
    • you can also do this with this tool: PowerSettingsExplorer made by mbk1969 at 3dguru forum.. I found it by accident researching on modern standby and annoying quirks of it in 2022
  • Recent Achievements

    • Week One Done
      Jeroen Wilms earned a badge
      Week One Done
    • Week One Done
      rolfus earned a badge
      Week One Done
    • One Month Later
      Leroy Jethro Gibbs earned a badge
      One Month Later
    • Conversation Starter
      flexorcist earned a badge
      Conversation Starter
    • One Month Later
      AndreaB earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      507
    2. 2
      +Edouard
      197
    3. 3
      PsYcHoKiLLa
      139
    4. 4
      ATLien_0
      90
    5. 5
      Steven P.
      81
  • Tell a friend

    Love Neowin? Tell a friend!