posts.ID, $wpdb->posts.post_content, $wpd-6ren">
gpt4 book ai didi

php - WPDB 查询 - 根据 _meta_key 返回 meta_value

转载 作者:行者123 更新时间:2023-11-29 19:29:59 24 4
gpt4 key购买 nike

请有人帮助扩展此代码的功能以合并事件的结束日期和开始日期?

<?php
global $wpdb;
$result = $wpdb->get_results ( "SELECT $wpdb->posts.ID, $wpdb->posts.post_content, $wpdb->postmeta.meta_id, $wpdb->postmeta.post_id, $wpdb->postmeta.meta_key, $wpdb->postmeta.meta_value, $wpdb->posts.post_title FROM $wpdb->posts INNER JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id WHERE $wpdb->postmeta.meta_key = '_EventStartDate' ORDER BY $wpdb->postmeta.meta_value " );


foreach ( $result as $page ) {
$date = new DateTime($page->meta_value);

if (strtotime($page->meta_value) >= strtotime('monday this week') && strtotime($page->meta_value) < strtotime('monday next week')) {
echo '<h2><div class="date-title">';
echo $page->post_title;
echo '</div><div class="date-date">';
echo $date->format('d-m-Y').'<br/>';
echo '</div></h2>';
}

}
?>

我尝试改变

WHERE $wpdb->postmeta.meta_key = '_EventStartDate'

WHERE $wpdb->postmeta.meta_key = '_EventStartDate' OR $wpdb->postmeta.meta_key = '_EventEndDate'

然后返回开始日期和结束日期元键,但我无法单独回显这些值。

基本上,我想输出这个

Event Name starts on (start date here) and finishes on (end date here)

对于每个事件。

希望这个问题更清楚吗?

谢谢

[编辑]

开始和结束日期的元键是:

_EventStartDate_EventEndDate 均位于 wp_postmeta

谢谢

最佳答案

如果您想获取自定义 post_type 帖子,则可以使用 WP_Query,要获取元数据,您必须使用 get_post_meta()
做一些这样的事情:

$args = array(
'post_type' => array('event'), //<-- Replace it with your custom post_type
'post_status' => array('publish'),
'order' => 'DESC',
'orderby' => 'date'
);

// The Query
$query = new WP_Query($args);
if (!empty($query->posts))
{
foreach ($query->posts as $post)
{
$_EventStartDate = get_post_meta($post->ID, '_EventStartDate', TRUE);
$_EventEndDate = get_post_meta($post->ID, '_EventEndDate', TRUE);
echo $post->post_title . ' starts on ' . $_EventStartDate . ' and finishes on ' . $_EventEndDate;
//...
}
}

希望这有帮助!

关于php - WPDB 查询 - 根据 _meta_key 返回 meta_value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41851299/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com