Wordpress - ACF - 使用转发器日期字段多次发布

时间:2013-04-16 22:26:22

标签: sql wordpress

我有自定义日期字段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字段可重复。我应该如何修改查询,多次获得帖子?也就是说,我将有一个有三个日期的节目,需要像

一样显示
  • 1.1.2013 - Show 1 - info
  • 2.1.2013 - Show 1 - info
  • 3.1.2013 - Show 1 - info

这里有一些教程:http://www.advancedcustomfields.com/resources/tutorials/querying-the-database-for-repeater-sub-field-values/,但由于我有限的sql知识,我无法把它放在一起。

由于

1 个答案:

答案 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。