• 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

    • State of Decay 2, Blasphemous 2, and more join Xbox Free Play Days by Pulasthi Ariyasinghe The latest Free Play Days offer has just kicked off, giving Xbox players the chance to try out a new selection of games over the weekend. The offerings this time aren't as massive as last weekend, but there are still major releases for Xbox players to jump into. This includes Undead Labs' post-apocalyptic title State of Decay 2, as well as two Team17-published titles. Two of the games being offered this time are available to all Xbox players without needing any kind of Game Pass subscription. In the fully free-to-play section, you can jump into State of Decay 2. The title is both an action survival title and a community builder, letting players choose a map, set up their base, and try to keep the growing zombie threat at bay. Cooperative play is available too. Don't forget that the studio is preparing a third entry for 2027. Next, Blasphemous 2 drops in for Metroidvania and Soulslike fans, where The Penitent One returns for another adventure. The title has tough combat with multiple weapon options, deadly traps to avoid, and platforming sequences requiring a lot of patience and timing. Keep in mind though that the offer does have a 5-hour time limit attached to it. Lastly, Xbox Game Pass Ultimate, Premium, and Essential members can now try out the WWII-set first-person shooter Hell Let Loose. The multiplayer game offers 50 vs 50 combat in massive maps, with infantry, tank, and artillery options available for players. Here are the announced games and the platforms they are available to play on: Hell Let Loose (Xbox Series X|S, PC) State of Decay 2: Juggernaut Edition (Xbox Series X|S, PC) Blasphemous 2 (Xbox Series X|S, Xbox One) To easily find the titles on Xbox consoles, first head to the Store, and then in the sidebar, find the Home section. In there, open the Subscriptions tab. The Free Play Days collection will show up in this area. This week's Free Play Days promotions will end on Sunday, June 11, at 11:59 pm PT.
    • Can we not have paperless office, like we was promised in the 80's
    • I actually laughed out loud in real life at the heading on this—whatever Microsoft is drinking, I want some of it.
    • Euro-Office must default to ODF to be considered "genuinely European", LibreOffice argues by David Uzondu Euro-Office is a web-based collaborative office suite that positions itself as a "European sovereign alternative" to American tech companies, backed by a coalition of developers including Nextcloud, IONOS, Abilian, BTactic, OpenProject, and, more recently, Tuta. The project officially went live a couple of days ago, but not before drawing heavy fire from LibreOffice developers, who called the marketing claim that Euro-Office represents the "first open-source office suite developed in Europe" a deceptive historical inaccuracy because projects like OpenOffice and LibreOffice existed decades earlier. Now that the project has launched, LibreOffice is back with another complaint, arguing that Euro-Office cannot consider itself "genuinely European" while it pushes proprietary Microsoft defaults on users. Euro-Office had promised to improve the OpenDocument Format (ODF) back in April, but the current release still plagues users with several technical failures. For instance, the suite lacks an admin setting to enforce ODF, and mobile editors completely block ODF saves, forcing files into Microsoft's OOXML formats. Some configurations force files into read-only mode, while editing frequently corrupts document formatting or erases data. LibreOffice thinks that merely supporting a format as an afterthought does not make you a sovereign alternative, as file formats are the battleground where" digital sovereignty is won or lost." The road to the first stable release of Euro-Office has been quite bumpy due to an aggressive public fallout with OnlyOffice, from which the coalition originally forked the project. OnlyOffice struck back by accusing the coalition of violating copyright terms under its AGPLv3 branding requirements by stripping the original branding anyway and forking the code. Getting Euro-Office up and running is a bit wonky (at least for non-technical users), as there is no direct installer to grab off the web. The easiest way we learnt is by using Docker. First, pull the official Euro-Office image from the GitHub Container Registry: docker pull ghcr.io/euro-office/documentserver:latest Then, run the container with active ports and a secure JWT token, enabling the test environment: docker run -i -t -d -p 8080:80 --restart=always -e EXAMPLE_ENABLED=true -e JWT_SECRET=my_secure_jwt_secret ghcr.io/euro-office/documentserver:latest And finally, open a web browser and go to the following address: http://localhost:8080 If you are running this on a remote server, replace localhost with your server's IP address. You will see the Euro-Office test page, where you can create new text documents, spreadsheets, or presentations directly in the browser. Image via Euro-Office Nextcloud promises that proper standalone desktop versions and mobile apps will arrive in a future release.
  • Recent Achievements

    • Week One Done
      FBSPL earned a badge
      Week One Done
    • One Year In
      Jim Dugan earned a badge
      One Year In
    • One Month Later
      Tommi118 earned a badge
      One Month Later
    • One Month Later
      sjbousquet earned a badge
      One Month Later
    • Week One Done
      sjbousquet earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      486
    2. 2
      PsYcHoKiLLa
      197
    3. 3
      +Edouard
      155
    4. 4
      Steven P.
      83
    5. 5
      ATLien_0
      69
  • Tell a friend

    Love Neowin? Tell a friend!