在我的自定义帖子类型上有一个名为date Start的自定义字段,我甚至添加了一个datepicker。现在我想根据一些日期搜索帖子。通常我会使用sql函数查找几天之间的日期,但我不知道如何搜索自定义字段中给定的日期。
搜索自定义帖子类型的日期
1 个回复
SO网友:Abdul Awal Uzzal
您需要使用WP_Query 获取与您的查询匹配的帖子。
并且在WP_Query
您需要使用以下各项:
\'meta_query\' => array(
\'meta_key\' => \'date_started\', // change it to your custom field meta_key
\'type\' => \'DATE\', // You can also try changing it to TIME or DATETIME if it doesn\'t work
\'meta_value\' => \'2017-05-23\', // Replace with your preferred value
\'meta_compare\' => \'=\',
),
完整的代码如下所示:<?php
$post_query = new WP_Query(
array(
\'post_type\' => \'post\',
\'posts_per_page\' => 1,
\'meta_query\' => array(
\'meta_key\' => \'date_started\', // change it to your custom field meta_key
\'type\' => \'DATE\', // You can also try changing it to TIME or DATETIME if it doesn\'t work
\'meta_value\' => \'2017-05-23\', // Replace with your preferred value
\'meta_compare\' => \'=\',
),
)
);
if($post_query->have_posts()) : while($post_query->have_posts()) : $post_query->the_post();
the_title(); // Print the title of found post
endwhile;
endif;
wp_reset_postdata();
?>
结束