【问题标题】:WP - Shortcode to query custom post type and display according to custom meta valueWP - 查询自定义帖子类型并根据自定义元值显示的简码
【发布时间】:2014-12-11 13:31:36
【问题描述】:

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

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

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;
    }
}

非常感谢任何帮助!

EDIT2:

这是我当前的代码:

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;
    }
}

【问题讨论】:

    标签: php wordpress custom-post-type


    【解决方案1】:

    您可以使用meta_query 或键/值对。阅读here

    $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'   => '>='
            ),
        ),
    ));
    

    【讨论】:

    • 我认为他/她还需要 'compare'=&gt;'&gt;=' 作为帖子等于或大于请求的帖子
    • 这是一种享受,谢谢:) 你知道是否有任何方法可以用 DD/MM/YYYY 格式输入广播日期吗?
    • 我不知道,我不认为,但您可以使用 strtotimedate 函数轻松转换
    • 谢谢,我会试一试,让你知道我的进展如何。我更习惯于 js 做事的方式,所以我在想可能是通过“/”将它的广播日期拆分并将数组项放入一个新变量中。如果我不能让 strtotime 工作,你认为我的 js 方法可以在 php 中使用吗?
    • strtotime 在这里不好,那是我的坏。但你可以使用这个,$date = '24/12/2014'; $parts = explode('/',$date); $newDate = $parts[2]."-".$parts[1]."-".$parts[0];
    猜你喜欢
    • 2014-07-30
    • 1970-01-01
    • 2014-07-12
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多