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