Make WordPress Core

Opened 14 years ago

Closed 13 years ago

Last modified 13 years ago

#21485 closed enhancement (wontfix)

Exclude current post from Recent Posts widget

Reported by: Asif2BD Owned by:
Priority: normal Milestone:
Component: Widgets Version: 2.8
Severity: normal Keywords: has-patch
Cc: Focuses:

Description

Checking demo site - ​http://twentytwelvedemo.wordpress.com/

If you visit any recent post like this one - ​http://twentytwelvedemo.wordpress.com/2012/03/28/road-trips/

The recent post widget on right sidebar include this post in recent 5 post. We could just exclude present post from the loop using code like this

<?php
				global $post;
				$args=array(
					"numberposts"=>5,
					"orderby"=>"post_date",
					"post__not_in"=>array($post->ID)
				);
				
				$posts = get_posts($args);
	
				foreach($posts as $post) {
					setup_postdata($post);
?>

Most theme has it. If you are already in a post which is among recent 5, no point of showing it in the loop.

Attachments (2)

21485.diff​ (908 bytes ) - added by DrewAPicture 14 years ago.
Excludes current post
21485.2.diff​ (960 bytes ) - added by DrewAPicture 14 years ago.
+ Readability

Download all attachments as: .zip

Change History (7)

@DrewAPicture
14 years ago

Excludes current post

#1 @DrewAPicture
14 years ago

  • Cc xoodrew@… added
  • Keywords has-patch added
  • Summary Twenty Twelve: Exclude Present Post From Recent Post → `

21485.diff​ Adds a post__not_in value to the WP_Query. Not sure if using get_the_ID() in this instance is cheating :)

@DrewAPicture
14 years ago

+ Readability

#2 @SergeyBiryukov
14 years ago

  • Component Bundled Theme → Widgets
  • Summary ` → Exclude current post from Recent Posts widget

#3 follow-up: @nacin
14 years ago

If I had to guess, one reason why not to do this would be it would make caching harder.

#4 in reply to: ↑ 3 @c3mdigital
13 years ago

  • Keywords ux-feedback removed
  • Resolution → wontfix
  • Status new → closed
  • Version → 2.8

Replying to DrewAPicture:

21485.diff​ Adds a post__not_in value to the WP_Query. Not sure if using get_the_ID() in this instance is cheating :)

get_the_ID() wouldn't be reliable on an archive, search or page for posts..

Replying to nacin:

If I had to guess, one reason why not to do this would be it would make caching harder.

Agreed the current post would not be excluded if the cache returned then if the cache was built without the current post then on other pages it would be missing. The only alternative I can think of is to add an additional post to the query args and check in the loop for the existing id then continue if found. Even that seams kind of hackish but would work.

I'm closing as wontfix as we can let theme developers do this on their own using the widget_posts_args filter.

#5 @SergeyBiryukov
13 years ago

  • Milestone Awaiting Review
Note: See TracTickets for help on using tickets.