最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to sort WP_Query by a custom text field (written as a date ddmmyyyy)

programmeradmin2浏览0评论

I have an ACF text field for a CPT that is holding a date in DD/MM/YYYY format (it's a text field and not a datepicker field as the data was imported from a CSV file).

My Wordpress loop then sorts by this field in DESC order but of course as it stands this means it displays the entries for the 31st of the month first as the day is the first part of the string. Is there any way to convert this field on-the-fly so it reads YYYY/MM/DD before being parsed by the loop?

I know how to output the date in the format I want in the front-end:

$date = the_field('date'); // eg. 15/05/2020
$orgD = substr($date, 0, 2);
$orgM = substr($date, 3, 2);
$orgY = substr($date, 6, 4);
$newFormat = $orgY . $orgM . $orgD;
$newDate = date("Y/m/d", strtotime($newFormat)); // eg. 2020/05/15

But I'm sure that doesn't help in any way for this purpose.

Is there a function I can add that will convert it or create a new custom field in the alternative format?

发布评论

评论列表(0)

  1. 暂无评论