• 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

    • A few years ago walmart had the 512 models on clearance for $35. I bought 3 of them. I should have purchased more.
    • I'm fine with a little reasonable promotion of Edge, but the degree which they do it right now I consider extremely unreasonable. 
    • Microsoft AI boss no longer believes that AI will replace human workers by David Uzondu Mustafa Suleyman, the head of Microsoft AI, recently took back his statements concerning white-collar jobs that he gave to the Financial Times in an interview made back in February, where he claimed that AI would replace office workers within 12 to 18 months. On Monday's episode of The Verge's Decoder, Suleyman recast the technology as more like a helpmate than a tool designed to take over your job. He explained that smaller office duties will "increasingly become digitized, automated" as people generate more digital materials. During the discussion, Suleyman emphasized a "very important distinction" between "tasks" and "jobs" to clarify his previous claims. He argued that his earlier comments only referred to individual actions that people perform at their desks. Suleyman used to work for DeepMind, the research lab he co-founded in 2010 alongside Demis Hassabis and Shane Legg, before he left in 2022 to establish Inflection AI and build an empathetic digital assistant. Microsoft hired him in March 2024 to lead its newly formed "Microsoft AI" division, placing him in charge of consumer products like Copilot, Bing, and Edge. His February comments also detailed plans for Microsoft to achieve self-sufficiency with a $140 billion infrastructure budget to train frontier models, predicting that creating a customized AI will soon feel like creating a podcast or a new blog: The 41-year-old is not the only AI executive who's softened his "AI will replace you" stance. OpenAI's CEO, Sam Altman, last month used X to push back against employment panic by arguing that his startup builds tools to assist humans rather than build replacements. He had previously garnered backlash by suggesting that many modern office roles that AI might replace did not qualify as "real work" in the first place, at least when you compare desk jobs to physical, historical labor like farming.
    • Adobe Acrobat Reader DC 2026.001.21662 by Razvan Serea Adobe Acrobat Reader DC software is the free, trusted standard for viewing, printing, signing, and annotating PDFs. Its the only PDF viewer that can open and interact with all types of PDF content – including forms and multimedia. It’s connected to Adobe Document Cloud – so you can work with PDFs on computers and mobile devices. Adobe Document Cloud is a revolutionary, modern and efficient way to get work done with documents in the office, at home or on-the-go. At the heart of Document Cloud is the all-new Adobe Acrobat DC, which will take e-signatures mainstream by delivering free e-signing with every individual subscription. Document Cloud includes a set of integrated services that use a consistent online profile and personal document hub. With Adobe Document Cloud, people will be able to create, review, approve, sign and track documents whether on a desktop or mobile device. Businesses will be able to take advantage of Document Cloud for enterprise which provides enterprise-class document services that integrate into systems of record such as CRM, HCM, CLM, and CMS, adding speed, efficiency and transparency to getting business done with documents. Adobe Acrobat Reader DC new feature highlights: Work with PDFs from anywhere with the new, free Acrobat DC mobile app for Android or iOS. Select functionality is also available on Windows Phone. Use the new Fill & Sign tool in your desktop software to complete PDF forms fast with smart autofill. Download the free Adobe Fill & Sign mobile app to add the same option to your iPad or Android tablet device. Save money on ink and toner when printing from your Windows PC. Store and access files in Adobe Document Cloud with 5GB of free storage. Get instant access to recent files across desktop, web, and mobile devices with Mobile Link. Sync your Fill & Sign autofill collection across desktop, web, and iPad devices. Adobe PDF Pack premium features includes: Convert documents and images to PDF files. Use your mobile device camera to take a picture of a paper document or form and convert it to PDF. Turn PDFs into editable Microsoft Word, Excel, PowerPoint, or RTF files. Combine multiple files into a single PDF (web only). Get signatures from others with a complete e-signature service. Send, track, and confirm delivery of documents electronically instead of using fax or overnight services (tracking not available on mobile). Store and access files online with 20GB of storage. Download: Adobe Acrobat Reader DC 64-bit | 719.0 MB (Freeware) Link: Adobe Acrobat Reader DC Home Page | Release Notes | Screenshot Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Meta will now use data from outside businesses to personalize AI responses by David Uzondu In an update that's rolling out globally (except in a handful of countries), Meta will use your data from outside businesses to personalize your AI responses and your primary feeds. Meta already utilizes your shopping activity to target ads, but the company now plans to expand this tracking to personalize other "parts of your experience" like feed algorithms and AI assistant chats. The company is replacing the two settings ("Your activity off Meta technologies" and "Activity from other businesses") that currently let you disconnect off-platform activity with a single, renamed setting called Activity from other businesses. If you don't want Meta to manipulate your feed and AI responses using your outside history, you can just turn the Activity from other businesses setting off in your account settings. This toggle resides within your Accounts Center, applying your choice to every connected profile. Turning this off will not stop companies from sending your data to Meta. The company will still collect your web interactions, but it only uses them to train products, while still accessing external accounts you connect. When The Verge spoke to Meta spokesperson Emil Vazquez, the representative said that this update will exclude several locations at launch, including the European region, the UK, Brazil, Thailand, South Africa, Turkey, South Korea, Ecuador, Nigeria, and Kenya. The new update comes at a time when the social media giant is recovering from a major PR disaster involving generative AI. Last week, there was a huge security issue on Instagram where attackers figured out a way to trick Meta AI into handing over account ownership (even if the victim had 2FA enabled). Some of the affected accounts include the dormant Obama White House profile, cosmetics brand Sephora, the Chief Master Sergeant of the Space Force, and security researcher Jane Manchun Wong. Internally, the company also had to scale back plans on its Model Capability Initiative (MCI), an employee-monitoring program designed to train corporate AI models by recording worker keystrokes and screen activity, after employees raised privacy concerns and complained about severe battery life drain.
  • Recent Achievements

    • One Year In
      Primer1st earned a badge
      One Year In
    • Experienced
      JayZJay went up a rank
      Experienced
    • Reacting Well
      Sir_Timbit earned a badge
      Reacting Well
    • Week One Done
      rubentuben8 earned a badge
      Week One Done
    • Week One Done
      ARaclen earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      512
    2. 2
      PsYcHoKiLLa
      229
    3. 3
      Edouard
      134
    4. 4
      ATLien_0
      87
    5. 5
      Steven P.
      80
  • Tell a friend

    Love Neowin? Tell a friend!