WP - 查询自定义帖子类型并根据自定义元值显示的简码


WP - Shortcode to query custom post type and display according to custom meta value

我已经设置了一个插件,用于创建自定义帖子类型("程序"(以及分类法和一些自定义元。我有一个名为"广播日期"的自定义元值,我正在尝试创建一个短代码,该代码将仅显示广播日期等于或大于今天的节目(以显示即将播出的节目时间表(。

目前我只显示帖子,我不确定从哪里开始日期查询。这是我到目前为止得到的:

add_shortcode('programme-schedule', 'programme_schedule_shortcode');
function programme_schedule_shortcode($atts, $content){
  extract(shortcode_atts(array( // a few default values
   'posts_per_page' => '-1',
   'post_type' => 'programmes')
   , $atts));
  global $post;
  $posts = new WP_Query($atts);
  $output = '';
    if ($posts->have_posts())
        while ($posts->have_posts()):
            $posts->the_post();
        $out = '<p>' . get_the_title() . '</p>';
    endwhile;
  else
    return;
  wp_reset_query();
  return html_entity_decode($out);
}

我可以添加什么,以便它只显示即将推出的节目?

谢谢:)

编辑:

事实证明代码也不起作用,我现在正在使用这个(目前仍然只获取帖子,而不是过滤它们(:

add_shortcode( 'hiblio-schedule', 'rmcc_post_listing_shortcode1' );
function rmcc_post_listing_shortcode1( $atts ) {
    ob_start();
    $query = new WP_Query( array(
        'post_type' => 'programmes',
    ) );
    if ( $query->have_posts() ) { ?>
        <ul class="schedule-listing">
            <?php while ( $query->have_posts() ) : $query->the_post(); ?>
            <li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </li>
            <?php endwhile;
            wp_reset_postdata(); ?>
        </ul>
    <?php $myvariable = ob_get_clean();
    return $myvariable;
    }
}

任何帮助都非常感谢!

编辑2:

这是我当前的代码:

add_shortcode( 'hiblio-schedule', 'hiblio_schedule_shortcode' );
function hiblio_schedule_shortcode( $atts ) {
    $todaysDate = date("Y/m/d");
    $date = '24/12/2014';//get_post_meta( $post->ID, 'broadcast-date', true );
    $parts = explode('/',$date);
    $newDate = $parts[2]."-".$parts[1]."-".$parts[0];
    ob_start();
    $query = new WP_Query(array(
    'post_type' => 'programmes',
    'meta_query' => array(
        array(
        'key' => 'broadcast-date',
        'value' => $todaysDate  , //The date what you want...
        'compare'   => '>='
        ),
    ),
    ));
    if ( $query->have_posts() ) {
    ?>
        <ul class="schedule-listing">
            <?php while ( $query->have_posts() ) : $query->the_post(); ?>
            <li id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </li>
            <?php endwhile;
            wp_reset_postdata(); ?>
        </ul>
    <?php $myvariable = ob_get_clean();
    return $myvariable;
    }
}
您可以使用

meta_query或键/值对。在这里阅读。

$query = new WP_Query(array(
    'post_type' => 'programmes',
    'meta_query' => array(
        array(
            'key' => 'broadcast-date',
            'value' => '2014-12-11', //The date what you want...
            'compare'   => '>='
        ),
    ),
));