我有自定义日期字段koncerty
的自定义帖子类型datum
,我目前正在使用此查询检索帖子(效果很好):
$date = date("Ymd");
$sql = <<<SQL
SELECT p.ID, p.post_content, p.post_title, datum.meta_value as datum, featured.meta_value as featured
FROM {$wpdb->posts} p
LEFT JOIN {$wpdb->postmeta} featured
ON featured.post_id = p.ID
AND featured.meta_key = 'featured'
LEFT JOIN {$wpdb->postmeta} datum
ON datum.post_id = p.ID
AND datum.meta_key = 'datum'
WHERE
p.post_type = 'koncert'
AND p.post_status = 'publish'
AND datum.meta_value >= '$date'
ORDER BY featured DESC, datum ASC, RAND()
LIMIT 3
SQL;
$posts = $wpdb->get_results( $sql );
我想使用ACF转发器字段插件使datum
字段可重复。我应该如何修改查询,多次获得帖子?也就是说,我将有一个有三个日期的节目,需要像
这里有一些教程:http://www.advancedcustomfields.com/resources/tutorials/querying-the-database-for-repeater-sub-field-values/,但由于我有限的sql知识,我无法把它放在一起。
由于
答案 0 :(得分:0)
我似乎得到了它,这是查询:
SELECT p.ID, p.post_content, p.post_title, datum.meta_value as datum, datum.meta_key , featured.meta_value as featured
FROM wp_posts p
LEFT JOIN wp_postmeta featured
ON featured.post_id = p.ID
AND featured.meta_key = 'featured'
LEFT JOIN wp_postmeta datum
ON datum.post_id = p.ID
AND datum.meta_key LIKE 'datum2_%_datum_koncertu'
WHERE
p.post_type = 'koncert'
AND p.post_status = 'publish'
AND datum.meta_value >= '$date'
ORDER BY featured DESC, datum ASC, RAND()
LIMIT 3
我使用'datum2_%_datum_koncertu'
,因为ACF用于将数据保存到数据库的命名约定为$ParentName_$RowNumber_$ChildName
(请参阅此处http://www.advancedcustomfields.com/resources/tutorials/querying-the-database-for-repeater-sub-field-values/)。
这样查询多次返回每个帖子,并显示日期,我只使用<?php date("d.m.", strtotime($post->datum))?>
不确定,如果可以使用wp_query。