Make WordPress Core

Opened 11 years ago

Closed 10 years ago

Last modified 10 years ago

#21485 closed enhancement (wontfix)

Exclude current post from Recent Posts widget

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

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 11 years ago.
Excludes current post
21485.2.diff (960 bytes) - added by DrewAPicture 11 years ago.
+ Readability

Download all attachments as: .zip

Change History (7)

@DrewAPicture
11 years ago

Excludes current post

#1 @DrewAPicture
11 years ago

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

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
11 years ago

+ Readability

#2 @SergeyBiryukov
11 years ago

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

#3 follow-up: @nacin
11 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
10 years ago

  • Keywords ux-feedback removed
  • Resolution set to wontfix
  • Status changed from new to closed
  • Version set to 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
10 years ago

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