Wordpress - 仅显示自定义日期和时间元素等于或大于现在的帖子

时间:2015-11-05 15:04:05

标签: php wordpress custom-post-type

我正在查询自定义帖子类型,“日程安排”,并通过自定义元字段“_broadcast_date”对帖子进行排序,以尝试创建电视指南。

以下是我的插件中的代码,用于注册帖子类型和自定义元:

/** 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);

以下是我的页面模板中的代码:

<?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. ?>  

我想要做的是以某种方式改变查询,以便它将仅返回_broadcast_date和_broadcast_time meta将来的帖子,即仅显示即将播出的广播。有什么想法吗?

1 个答案:

答案 0 :(得分:2)

你可以这样使用meta_query :(未经测试,可能需要修订)

$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);

编辑:长期居住,经常投票:D