Php Wordpress-仅显示自定义日期和时间元等于或大于当前值的帖子
我正在查询一个自定义帖子类型“schedule”,并通过一个自定义元字段“u broadcast\u date”对帖子进行排序,以尝试创建一个类似于电视指南的东西 以下是我的插件中注册帖子类型和自定义元数据的代码:Php Wordpress-仅显示自定义日期和时间元等于或大于当前值的帖子,php,wordpress,custom-post-type,Php,Wordpress,Custom Post Type,我正在查询一个自定义帖子类型“schedule”,并通过一个自定义元字段“u broadcast\u date”对帖子进行排序,以尝试创建一个类似于电视指南的东西 以下是我的插件中注册帖子类型和自定义元数据的代码: /** Register Schedule Post Type */ add_action( 'init', 'hiblio_pt_schedule_init' ); function hiblio_pt_schedule_init() { $labels = array(
/** Register Schedule Post Type */
add_action( 'init', 'hiblio_pt_schedule_init' );
function hiblio_pt_schedule_init() {
$labels = array(
'name' => _x( 'Scheduled Programmes', 'post type general name', 'hbl-plugin' ),
'singular_name' => _x( 'Scheduled Programme', 'post type singular name', 'hbl-plugin' ),
'menu_name' => _x( 'Scheduled Programmes', 'admin menu', 'hbl-plugin' ),
'name_admin_bar' => _x( 'Scheduled Programme', 'add new on admin bar', 'hbl-plugin' ),
'add_new' => _x( 'Add New', 'scheduled-programme', 'hbl-plugin' ),
'add_new_item' => __( 'Add a Programme to the Schedule', 'hbl-plugin' ),
'new_item' => __( 'New Scheduled Programme', 'hbl-plugin' ),
'edit_item' => __( 'Edit Scheduled Programme', 'hbl-plugin' ),
'view_item' => __( 'View Scheduled Programme', 'hbl-plugin' ),
'all_items' => __( 'Scheduled Programmes', 'hbl-plugin' ),
'search_items' => __( 'Search Scheduled Programmes', 'hbl-plugin' ),
'parent_item_colon' => __( 'Parent Scheduled Programmes:', 'hbl-plugin' ),
'not_found' => __( 'No Scheduled Programmes found.', 'hbl-plugin' ),
'not_found_in_trash' => __( 'No Scheduled Programmes found in Trash.', 'hbl-plugin' )
);
$args = array(
'labels' => $labels,
'description' => __( 'Description.', 'hbl-plugin' ),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => 'hbl-admin',
'query_var' => true,
'rewrite' => array( 'slug' => 'schedule' ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt' ),
'taxonomies' => array( 'hbl-categories' ),
'register_meta_box_cb' => 'add_schedule_metaboxes'
);
register_post_type( 'schedule', $args );
}
/** Custom Meta for Schedule Post Type */
function add_schedule_metaboxes(){
add_meta_box('hbl_schedule_meta', 'Programme Details', 'hbl_schedule_meta', 'schedule', 'normal', 'high');
}
/*** Schedule Meta Box */
function hbl_schedule_meta() {
global $post;
echo '<input type="hidden" name="schedulemeta_noncename" id="schedulemeta_noncename" value="' .
wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
$broadcastDate = get_post_meta($post->ID, '_broadcast_date', true);
echo '<label for="_broadcast_date">Broadcast Date</label><input type="date" name="_broadcast_date" value="' . $broadcastDate . '" class="widefat" /><br /><br />';
$broadcastTime = get_post_meta($post->ID, '_broadcast_time', true);
echo '<label for="_broadcast_time">Broadcast Time</label><input type="time" name="_broadcast_time" value="' . $broadcastTime . '" class="widefat" />';
}
/** Save schedule meta */
function hbl_save_schedule_meta($post_id, $post) {
if ( !wp_verify_nonce( $_POST['schedulemeta_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
if ( !current_user_can( 'edit_post', $post->ID )) {
return $post->ID;
}
$schedule_meta['_broadcast_date'] = $_POST['_broadcast_date'];
$schedule_meta['_broadcast_time'] = $_POST['_broadcast_time'];
foreach ($schedule_meta as $key => $value) {
if( $post->post_type == 'revision' ) return;
$value = implode(',', (array)$value);
if(get_post_meta($post->ID, $key, FALSE)) {
update_post_meta($post->ID, $key, $value);
} else {
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key);
}
}
add_action('save_post', 'hbl_save_schedule_meta', 1, 2);
/**注册计划职位类型*/
添加行动('init','hiblio_pt_schedule_init');
函数hiblio_pt_schedule_init(){
$labels=数组(
'name'=>x('Scheduled Programmes','post type general name','hbl plugin'),
“单数名称”=>\ux('Scheduled Programme'、'post type单数名称'、'hbl plugin'),
“菜单名称”=>\ux('Scheduled Programmes'、'admin menu'、'hbl plugin'),
'name_admin_bar'=>\u x('Scheduled program','addnew on admin bar','hbl plugin'),
'add_new'=>\u x('add new','scheduled program','hbl plugin'),
'add_new_item'=>\('add a program to Schedule','hbl plugin'),
“新建项目”=>“(“新建计划”、“hbl插件”),
“编辑项目”=>“(“编辑计划节目”、“hbl插件”),
“查看项目”=>“(“查看计划节目”、“hbl插件”),
“所有项目”=>(“计划节目”、“hbl插件”),
“搜索项目”=>(“搜索计划节目”、“hbl插件”),
“父项目号”=>“(“父计划节目:”,“hbl插件”),
“未找到”=>“(“未找到计划的节目”。,“hbl插件”),
“未在垃圾箱中找到”=>(垃圾箱中未找到计划的节目),“hbl插件”)
);
$args=数组(
“标签”=>$labels,
“description'=>\(“description.”,“hbl插件”),
“public”=>正确,
“公开可查询”=>正确,
'show_ui'=>true,
“在菜单中显示”=>“hbl管理员”,
'query_var'=>true,
“重写”=>array('slug'=>schedule'),
“能力类型”=>“职位”,
“has_archive”=>true,
“分层”=>false,
“菜单位置”=>null,
'支持'=>数组('标题','编辑器','缩略图','摘录'),
“分类法”=>数组(“hbl类别”),
“注册元盒”=>“添加元盒”
);
注册后类型('schedule',$args);
}
/**计划职位类型的自定义元数据*/
函数add_schedule_metaboxes(){
添加“时间表”框(“hbl\U时间表”、“计划详情”、“hbl\U时间表”、“时间表”、“正常”、“高”);
}
/***明细表元框*/
功能hbl_时间表_元(){
全球$员额;
回声';
$broadcastDate=get_post_meta($post->ID,'u broadcast_date',true);
回音“广播日期
”;
$broadcastTime=get_post_meta($post->ID,'u broadcast_time',true);
回声“广播时间”;
}
/**保存时间表元*/
功能hbl\u保存\u时间表\u元($post\u id,$post){
如果(!wp_verify_nonce($_POST['schedulemeta_noncename'],plugin_basename(uu文件_u))){
返回$post->ID;
}
如果(!当前用户\u可以('edit\u post',$post->ID)){
返回$post->ID;
}
$schedule_meta[''u broadcast_date']=$u POST['u broadcast_date'];
$schedule_meta[''u broadcast_time']=$u POST['u broadcast_time'];
foreach($schedule\u meta作为$key=>$value){
if($post->post_type=='revision')返回;
$value=内爆(“,”,(数组)$value);
if(get_post_meta($post->ID,$key,FALSE)){
更新发布元数据($post->ID,$key,$value);
}否则{
添加帖子元($post->ID,$key,$value);
}
如果(!$value)删除帖子元($post->ID,$key);
}
}
添加动作(“保存帖子”、“hbl保存时间表元”,1,2);
以下是我的页面模板中的代码:
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'template-parts/content', 'page' ); ?>
<?php
global $post;
$args=array(
'post_type' => 'schedule',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1,
'orderby' => '_broadcast_date',
'order' => 'ASC'
);
$schedule_query = null;
$schedule_query = new WP_Query($args);
if( $schedule_query->have_posts() ) {
while ($schedule_query->have_posts()) : $schedule_query->the_post(); ?>
<div class="scheduleItem">
<div class="scheduleItemImage" style="background-image: url(<?php if ( has_post_thumbnail() ) {
echo wp_get_attachment_url( get_post_thumbnail_id($post->ID) );
} else {
echo get_template_directory_uri().'/images/rewindBlank.jpg';
}
?>)"></div>
<div class="scheduleItemInfoWrapper">
<h2><?php the_title(); ?></h2>
<div class="scheduleItemContent">
<?php echo the_content(); ?>
</div>
<div class="scheduleItemDetails">
<span class="scheduleItemDate"><?php echo get_post_meta( $post->ID, '_broadcast_date', true).'</span> @ <span class="scheduleItemTime">'.get_post_meta( $post->ID, '_broadcast_time', true) ?></span>
</div>
</div>
</div>
<?php endwhile;
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>
<?php endwhile; // End of the loop. ?>
您可以使用类似的:(未测试,可能需要修改)
编辑:长寿,经常投票:D您能告诉我们广播日期和时间是如何保存在数据库中的吗?这可能会改变您的查询方式。此外,在WP_查询的上下文中可能不可能执行此操作-您可能必须直接查询数据库才能完成此操作。@MikelBitson感谢您的回复,我已经从插件中添加了代码。您可以打开数据库,打开usermeta表,并搜索像“%broadcast_date%”这样的键吗?这将向您显示保存在数据库中的meta_值,通过这种方式,我们可以查看如何查询,因为我们将知道这些值保存为的格式。_broadcast_date存储为YYYY-MM-DD,_broadcast_time为HH:MMIt适用于日期,但不适用于时间:(我在时间查询中尝试将小h改为大写h,因为时间是以24小时格式存储的,但这取消了日期查询,只显示时间晚于当前时间的帖子。它似乎接近我要查找的内容,但它运行的是query1或query2
而不是query1和query2
Hey@user28419我为AND关系和24小时时间编辑了我的帖子-如果这仍然不起作用,我们将需要进行一些调试。忽略我的第一条评论,将H改为大写似乎实际上并没有改变查询,我认为时间查询根本不起作用,在日期和ti上将元查询改为AND操作我是一个问题。例如,如果是14:00,明天03:00的节目就不会出现。这可能会有所帮助
$args=array(
'post_type' => 'schedule',
'post_status' => 'publish',
'posts_per_page' => -1,
'caller_get_posts'=> 1,
'orderby' => '_broadcast_date',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_broadcast_date',
'value' => date('Y-m-d'),
'compare' => '>=',
'type' => 'DATE'
),
array(
'key' => '_broadcast_time',
'value' => date('H:i'),
'compare' => '>=',
'type' => 'TIME'
),
)
);
$schedule_query = new WP_Query($args);