• 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

    • Just for anyone reading, AdGuard (the free, standalone MV3 extension) is quite good now, a direct competitor to uBlock Origin Lite and much more built-out than it.
    • Microsoft Edge 149.0.4022.62 by Razvan Serea Microsoft Edge is a super fast and secure web browser from Microsoft. It works on almost any device, including PCs, iPhones and Androids. It keeps you safe online, protects your privacy, and lets you browse the web quickly. You can even use it on all your devices and keep your browsing history and favorites synced up. Built on the same technology as Chrome, Microsoft Edge has additional built-in features like Startup boost and Sleeping tabs, which boost your browsing experience with world class performance and speed that are optimized to work best with Windows. Microsoft Edge security and privacy features such as Microsoft Defender SmartScreen, Password Monitor, InPrivate search, and Kids Mode help keep you and your loved ones protected and secure online. Microsoft Edge has features to keep both you and your family protected. Enable content filters and access activity reports with your Microsoft Family Safety account and experience a kid-friendly web with Kids Mode. The new Microsoft Edge is now compatible with your favorite extensions, so it’s easy to personalize your browsing experience. Download: Microsoft Edge (64-bit) | 193.0 MB (Freeware) Download: Microsoft Edge (32-bit) | 170.0 MB Download: Microsoft Edge (ARM64) | 188.0 MB View: Microsoft Edge Website | Release History Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Yeah, when I saw that, I wanted to find the nearest nose. You can't find a good nose these days when you need one.
    • Anthropic launches Claude Fable 5, a state-of-the-art AI model that beats OpenAI's GPT-5.5 by Pradeep Viswanathan Back in April, Anthropic announced Claude Mythos Preview, a frontier model with state-of-the-art coding capabilities. Due to the cybersecurity implications that would occur due to the availability of such a powerful model, Anthropic made it available to only a select set of companies around the world. The company's plan was to prepare appropriate guardrails before releasing such a powerful model to everyone. Now, after nearly two months, Anthropic announced Claude Fable 5, its most capable AI model yet for general users. The company also announced Claude Mythos 5, the same underlying model as Fable 5, but with safeguards lifted, making it more suitable for selected cybersecurity and biology use cases. Claude Fable 5 sits a tier above its Opus models and it beats most other generally available models across areas including software engineering, knowledge work, vision, scientific research, and long-running autonomous tasks. To prevent model misuse, when Claude Fable 5 detects certain requests related to cybersecurity, biology, chemistry, or model distillation, the request will be routed to the Claude Opus 4.8 model. Anthropic claims that these safeguards trigger in less than 5% of sessions on average. However, for large organizations working on critical software, Claude Mythos 5 can be availed through Project Glasswing. Later, Anthropic has plans to expand access through a broader trusted access program. As you can notice in the benchmarks above, Fable 5 and Mythos 5 are state-of-the-art on most key AI benchmarks and they are well ahead of OpenAI's frontier model, GPT-5.5. For example, Fable 5 is the new state-of-the-art model for vision tasks. Also, Mythos 5 has the strongest cybersecurity capabilities of any model in the world. Claude Fable 5 and Claude Mythos 5 are priced at $10 per million input tokens and $50 per million output tokens, which is less than half the price of Claude Mythos Preview. Another big change is that Anthropic is making a change to the way they handle business customer data for both Fable 5 and Mythos 5 models. The company will now require 30-day retention for all traffic on both first- and third-party surfaces. Anthropic promises that it won't use the data to train Claude models, instead it will use it against complex and novel attacks. Claude Fable 5 is available today on the Claude API and consumption-based Enterprise plans. It is also included at no extra cost for Pro, Max, Team, and seat-based Enterprise customers from today through June 22. After that, users on those plans will need usage credits to continue using Fable 5, unless Anthropic extends the included access window based on capacity. Developers can access Fable 5 through the Claude API using the claude-fable-5 model name.
  • Recent Achievements

    • Week One Done
      rubentuben8 earned a badge
      Week One Done
    • Week One Done
      ARaclen earned a badge
      Week One Done
    • One Year In
      jojodbn earned a badge
      One Year In
    • One Month Later
      jojodbn earned a badge
      One Month Later
    • Week One Done
      jojodbn earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      525
    2. 2
      PsYcHoKiLLa
      232
    3. 3
      +Edouard
      124
    4. 4
      ATLien_0
      88
    5. 5
      Steven P.
      83
  • Tell a friend

    Love Neowin? Tell a friend!