无法在WordPress帖子类型查询中更改每页帖子

时间:2016-11-15 作者:MikeL5799

我有一个名为“事件”的自定义帖子类型。帖子显示良好,只是WP查询循环仅显示一篇帖子,而不是所有帖子。这是我函数中的完整代码。事件的php文件:

    function min_get_events ( $echo = true ) {
     $events = new WP_query(array(
    \'post_type\' => \'events\',
    \'posts_per_page\' => -1,
    \'order\' => \'ASC\',
    \'orderby\' => \'meta_value\',
    \'meta_key\' => \'min_event-start\',
    \'meta_query\' => array(
        \'key\'     => \'min_event-start\',
        \'value\'   => date(\'Y-m-d\'),
        \'compare\' => \'>\',
    )
));

$i = 0;
$n = 1;

if ( $events->have_posts() ) :
    while ( $events->have_posts() ):
        global $post;
        $events->the_post();

        $open_date = get_post_meta($post->ID, \'min_event-start\', true);
        $close_date = get_post_meta($post->ID, \'min_event-end\', true);
        $start = strtotime($open_date);
        $end = strtotime($close_date);
        if ($start==$end || $start > $end) {
            $date_string = date(\'F j, Y\', $start);
        }
        else {
            $date_string = date(\'F j, Y\', $start) .\' – \'. date(\'F j, Y\', $end);
        }
        //$description = get_post_meta($post->ID, \'event-description\', true);
        $location = get_post_meta($post->ID, \'min_event-location\', true);
        $url = get_post_meta($post->ID, \'min_event-url\', true);
        //$registration_url = get_post_meta($post->ID, \'event-registration-url\', true);
        $cta_text = get_post_meta($post->ID, \'min_event-cta-text\', true);
        $cta = !empty($cta_text) ? $cta_text : \'Register Now\';

        $i++;
        if ( $i == 1 ) {
            ?>
            <div class="row">
            <?php
        }
        ?>
        <div class="event col-sm-4">
            <a href="<?= $url ?>">
                <?php the_post_thumbnail(\'thumb-events\'); ?>
            </a>
            <h1><?= htmlentities($post->post_title) ?></h1>
            <div >
                <div><?= $date_string; ?></div>
                <div><?= htmlentities($location) ?></div>
                <div><a href="<?=$url;?>" class="register-now"><?= $cta; ?></a></div>
            </div>
            <!-- <p><?= $description ?></p> -->
            <!-- <p><a href="<?=$url;?>" class="register-now"><?= $cta; ?></a></p> -->
        </div> <!-- .event -->
        <?php


        if ( $i == 3  || ($events->current_post +1) == $events->post_count) {
            ?>
            </div> <!-- <?php echo $i; ?> .row <?php echo $n; ?> -->
            <?php
            $i = 0;
            $n++;
        }

    endwhile;
endif;

ob_start();
?>

<?php
$return = ob_get_contents();
ob_end_clean();
wp_reset_postdata();
if ( $echo ) {
    echo $return;
} else {
    return $return;
}
 }
我错过什么了吗?

1 个回复
SO网友:Benoti

对于单个自定义字段处理,必须在数组的数组中更改meta\\u查询参数:

 \'meta_query\' => array(
     array(
         \'key\'     => \'min_event-start\',
         \'value\'   => date(\'Y-m-d\'),
         \'compare\' => \'>\',
    )
 )
您将获得这些信息WP_Query 在单个自定义字段处理部件中

希望它能与此配合使用。