Meta_Query不能正常工作

时间:2016-04-12 作者:Laura Sage

我正在尝试运行一个查询,该查询仅在高级自定义字段选择框中显示满足条件的项目,但没有得到任何结果。这是我的疑问。如有任何帮助,将不胜感激:

<?php $args = array(
    \'post_type\' => \'home_plans\',
    \'orderby\'=> \'date\',
    \'order\' => \'rand\',
    \'numberposts\' => \'12\',
    \'meta_query\' => array(
        \'relation\' => \'AND\',
            array(
                \'key\' => \'display_where\',
                \'value\' => \'here\',
                \'compare\' => \'LIKE\'
            )
    )
); ?>

<div id="ms-container" class="row archive">
    <ul id="posts_list">
        <?php $recent_posts = wp_get_recent_posts( $args );
        $selected = get_field(\'display_where\');

        foreach( $recent_posts as $recent ){
            get_template_part( \'template-parts/plan-archive-loop\', get_post_format() );
        }

        //wp_reset_postdata();
        ?>
    </ul>
</div>
{编辑}代码有点变化。以下是新代码:

<?php $archive_args = array(
    \'post_type\' => \'speight_home_plans\',
    \'orderby\'=> \'title\',
    \'order\' => \'ASC\',
    \'posts_per_page\' => 12,
    \'paged\' => $paged,
    \'page\' => $paged,
    \'meta_query\' => array(
        \'key\' => \'display_where\',
        \'value\' => \'speight\',
        \'compare\' => \'LIKE\'
    )
);

$archive_query = new WP_Query( $archive_args );
if ( $archive_query->have_posts() ) :

$paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1;
$total_posts = $archive_query->found_posts;
$start_post = ($paged - 1) * $posts_per_page + 1;
$end_post = min($start_post + $posts_per_page - 1, $total_posts);

echo "<p class=results-count>Showing $start_post - $end_post of $total_posts home plans.</p>";

while ( $archive_query->have_posts() ) : $archive_query->the_post();
    get_template_part( \'template-parts/plan-archive-loop\', get_post_format() );
endwhile;
wp_reset_postdata();
endif;
这在archive-speight\\u home\\u计划中。我的主题的php文件。

1 个回复
SO网友:Kuuak

基于codex, 这个meta_query 参数包含一个或多个relation 参数not set 如果是单个内部meta_query 大堆

同时拆下page 参数,因为它仅用于静态首页。

args数组应如下所示:

$archive_args = array(
    \'post_type\' => \'speight_home_plans\',
    \'orderby\'=> \'title\',
    \'order\' => \'ASC\',
    \'posts_per_page\' => 12,
    \'paged\' => $paged,
    \'meta_query\' => array(
        array(
            \'key\' => \'display_where\',
            \'value\' => \'speight\',
            \'compare\' => \'LIKE\'
        ), 
    ), 
);

相关推荐

WordPress Custom Post Loop

我正在尝试循环浏览自定义WordPress帖子,遇到了一个问题,比如我添加了自定义字段并想在中显示它<li> 使用循环。我成功地完成了操作,但数据/链接/类别正在重复,如果类别与以下内容相同,我希望只显示一次:如果我有2篇带有data1类别的帖子,那么链接将只显示data1once 但我有2个不同类别的帖子,然后它会分别显示每个帖子。Sample Code:<ul class="filter filter-top"> <li cla