picandocodigo / List-Category-Posts

WordPress plugin which allows you to list posts from a category into a post/page using the [catlist] shortcode.
http://wordpress.org/extend/plugins/list-category-posts/
GNU General Public License v2.0
241 stars 112 forks source link

Feature: Add a customfield_date_display parameter #508

Closed braekling closed 10 months ago

braekling commented 10 months ago

First of all thanks for your useful plugin! Because we are using it to display a specific event post type which contains an event date within a custom field, something like a customfield_date_display and customfield_date_dateformat would be very helpful.

Suggested feature

We want to add the event date to the event post list. A customfield_date_display and corresponding parameters would allow us to show such a field (same as the existing customfield_display, customfield_display_name, etc.), but the additional customfield_date_dateformatwill also allow us to format the date to be shown as d.m.Y H:i instead of Y-m-d H:i:s as it is stored in the database.

Current usage

Currently, we can only show the custom field as is, which means all dates are shown as Y-m-d H:i:s, which may cause some confusion in Germany. :wink:

Shortcode examples

This is how it currently looks like: [catlist post_type="event" customfield_orderby="_event_date" order="asc" customfield_display="_event_date" customfield_display_name="no"]

This is how we would like to use it: [catlist post_type="event" customfield_orderby="_event_date" order="asc" customfield_date_display="_event_date" customfield_date_display_name="no" customfield_date_dateformat="d.m.Y H:i"]

(Of course, the parameter naming is just an example.)

klemens-st commented 10 months ago

This is complicated because users might have a plethora of different filters they would like to apply to custom fields, not just for dates. This is why we encouraged to implement such filtering in custom templates. Otherwise we would end up with many new parameters to satisfy all possible scenarios: dates, numbers, strings, etc.

Perhaps we could add a few built in parsing methods for dates,

braekling commented 10 months ago

Thanks for your quick response!

I understand the problem that arises with specific custom field types - the complexity would become unmanageable, because the additional parameters (such as *_display_name) always have to be included, too.

So, as you suggested, I had a look how templates work, and I created my own one form the default template. There I added my "custom date field" by adding this code between custom fields and post thumbnail:

  //Event date:
  $event_date = get_post_meta($post->ID, '_event_date', true);
  if (!empty($event_date)) {
      $event_date_obj = strtotime($event_date);
      $lcp_display_output .= $this->wrapper->wrap(date($this->params['dateformat'], $event_date_obj), 'div', 'lcp-customfield-date');
  }

It also uses the dateformat parameter of the catlist shortcode, so that format changes don't require changes in the PHP file. Maybe this will help others with similar requirements.