Jump to content

Featured listing in order it's featured in


JoshLewis

Recommended Posts

Changing a listing's featured status changes it's featured column from 0 to 1. There doesn't seem to be a traditional way to show listings in the order they were featured in. For example listing A published May 2020, listing B on May 2015, and listing C on May 2010. If I feature in the following order: "listing B, listing A, listing C" it would not display in featured listings in this order given that it would sort by either latest publication date, recently modified, alphabetically, ect. I'm aware of featured ordering as well as secondary ordering for non featured items within the same criteria. As a result if I feature an older post it will not show up on the featured page.

For sites featuring only the latest things this isn't a problem at all. However I can be certain there will be listings from 5 or 10 years ago that will eventually become feature worthy. I've been an administrator of a site where this happened often. Fortunately there is a way to address this utilizing the date field; hence jr_featureddate. JReviews graciously allows us to sort by date field making it an easy option to remedy this. The final crux is having an event handler/filter that fires when featuring a listing. The following is close but is listing_save_pre instead of a featured_event filter:

function featured_time($data, $params)
{
	$today = new DateTime();
	$data['Field']['jr_featureddate'] = $today;

  	return $data;
}
 
Clickfwd\Hook\Filter::add('listing_save_pre', 'featured_time', 10);

I've read through docs and forums for anything related to featured listing. WP hooks doesn't contain featured. I've looked into developer events but unfortunately lack understanding how to properly use them. Probably ideal to have filter_functions.php to have near the top:

namespace JReviews\Listeners;

Further down filter_functions.php using:

use JReviews\Events\ListingWasFeatured;

function featuredDate($data) {
	ob_start();
		$today = new DateTime();
        $data['Field']['jr_featureddate'] = $today;
	ob_end_clean();
	return $data;
}

Perhaps better to pass what's inside the function to a class? Given that ListingWasFeatured is a core provider it would seem redundant to create a new one.

class featuredDate extends JReviews\Events\ListingWasFeatured
{
	protected $listen = [
		'ListingWasFeatured' => [
			'featuredDate()'
		],
	];
}

 

Link to comment
×
×
  • Create New...