php - 从 Wordpress 中的关系字段显示和排序帖子(Pods 和事件日历)
问题描述
我已经使用 Pod 在 Wordpress 中创建了自定义帖子类型,然后我还使用了“事件日历”。我正在尝试使用关系字段将事件日历中的事件链接到名为“项目”的自定义帖子类型。
所以我有我的“项目”(由 PODS 创建的 CPT)和与事件的关系(由事件日历创建的 CPT)。创建项目时,您可以将事件添加到项目中。下面的代码基于本教程 ( https://docs.pods.io/tutorials/get-values-from-a-relationship-field/ ) 检索属于该特定项目的事件和开始时间该事件,并将它们发布在列表中。
“项目”是指称为项目的自定义帖子类型。“ project_event ”是关系字段。“ _EventStartDate ”是包含事件开始日期的字段。
//get Pods object for current post
$pod = pods( 'projects', get_the_id() );
//get the value for the relationship field
$related = $pod->field( 'project_event' );
//loop through related field, creating links to their own pages
//only if there is anything to loop through
if ( ! empty( $related ) ) { { ;
echo '<ul>';
foreach ( $related as $rel ) {
//get id for related post and put in ID
//for advanced content types use $id = $rel[ 'id' ];
$id = $rel[ 'ID' ];
//show the related post name as link
echo '<li><a href="'.esc_url( get_permalink( $id ) ).' " >'.get_the_title( $id ).'</a> ';
//get the value for some_field in related post and echo it
$someField = get_post_meta( $id, '_EventStartDate', true );
if( $someField != '' )
echo date( "j.n.Y </li>", strtotime( $someField ) ) ;
;
}
} //end of foreach
} //endif ! empty ( $related )
echo '</ul>';
此代码有效,但它以随机顺序检索事件。我想对它们进行排序,以便它根据事件的日期检索事件。我的 PHP 技能有限,所以我不知道如何实现这一点。
“全天”事件也未正确显示。现在它将在全天活动中发布 00:00 的开始时间……我不太确定如何解决这个问题。
在 PODS 支持的官方论坛中,他们推荐了tribe_get_start_date 函数(https://docs.theeventscalendar.com/reference/functions/tribe_get_start_date/)
任何提示我可以或应该如何解决这个问题?
解决方案
我实际上发现顺序不是随机的。这些事件按 ID 排序,因此最新创建的事件总是最后一个......但问题仍然存在。如何按日期对它们进行排序?
我在 GitHub ( https://gist.github.com/jo-snips/5112025 ) 上找到了这个,其中包含有关如何从事件日历中对事件进行排序的信息。但是可以在我提供的代码中包含一个数组吗?
推荐阅读
- apache-pulsar - 连接代理时出错:PulsarClientException Connection Refused
- python - 如何让python启动多个进程或程序
- r - 替代 mapply 以覆盖零长度输入的错误
- screen - 一些子窗口不在笔记本电脑屏幕上打开,但在显示器上打开
- spring-boot - Cron 每天从 00h05 开始
- c++ - 构建库时出现 cmake 链接器问题。链接库存在但无法链接
- django-channels - Django Channels 指定发送函数
- python - 使用opencv从图像连接中获取图形长度
- c# - System.IO.Ports.SerialPort.GetPortNames 返回非活动端口 - .NET Core 5
- vb.net - 带有phpmyadmin服务器的VB.NET,如何在每天结束后删除表中的记录?