Jump to content

Selector not working?


ndolph
 Share

Recommended Posts

This is my code...

function events_carousel() {
	$events = wire()->pages->get("/events/")->children('start=0, event_date>=' . date("Ymd") . ', limit=5, sort=-created');
	foreach ($events as $event) {
		echo "<li><strong>" . $event->title."</strong> - ".$event->event_date . "</li>";
	}
}

And this is the output..

  • Back to the future! - 20141217
  • MORE MULTI EVENT DAYS - 20150121
  • Testing multi day events - 20150121
  • Whisper does things - 20150126
  • Crazy dog stuff - 20150121

But, it shouldn't display "Back to the future!", but it doesn't seem to care about the event_date.

Where am I going wrong?

Link to comment
Share on other sites

I would suggest you to use a standartized dateformat, just to be sure the date is parsed correctly.

From the comments here: http://processwire.com/api/selectors/#examples

I was using a standardized date format before and it didn't work for some reason.

Changed the field to use  l, F j, Y g:i a and changed the code to the following..

$events = wire()->pages->get("/events/")->children('start=0, event_date>=' . date("l F j Y g:i a") . ', limit=5, sort=-created');

It now works. *thumbs up*

Link to comment
Share on other sites

I've reset the settings of the field and have settled with this, I like displaying the date in a different format for certain places that this is embedded...

function events_list($location) {

	$dateFormatOut = 'D M j Y';
	$dateSelectorFormat = 'l F j Y g:i a';
	$dateQueryComparison = '';
	$outString = '<li><strong>%s</strong> - %s</li>'; // If you need to change this for a specific case, do so in the switch below. %s is printf() string representation

	// Switch to alter options depending on location
	switch($location) {
		case 'carousel':
			$dateFormatOut = 'l F j Y g:i a';
			$dateQueryComparison = '>=';
			break;
		case 'past':
			$dateQueryComparison = '<=';
			break;
		case 'upcoming':
			$dateQueryComparison = '>=';
			break;
	}

	$events = wire()->pages->get('/events/')->children(sprintf('start=0, event_date%s%s, limit=5, sort=-created', $dateQueryComparison, date($dateSelectorFormat)));
	foreach ($events as $event) {
		printf($outString, $event->title, date($dateFormatOut, $event->event_date));
	}
}
Link to comment
Share on other sites

As you're not using specific dates for your selector, you could also use "today" or "time()" to compare to your datefield. No need to use a string date format, if you use it only in the selector. And you save all the lines about the $selectorFormat. Also I would suggest to change the line in the foreach to this: 

printf($outString, $event->title, date($dateFormatOut, $event->getUnformatted("event_date")));

This way you'll always get the timestamp of the field, no matter how it's displayed in the backend by the settings.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...