我正在构建嵌套循环查询。我有一个元框,可以捕获邮件id(分类器和转发器)。到目前为止,我的代码
while ( have_posts() ) : the_post();
?>
<div id="<?php echo $post->post_name; ?>">
<h1><?php the_title(); ?></h1>
<?php
the_content();
$urls = get_post_meta( get_the_ID(), \'mymeta_url_group\', false );
foreach ( $urls as $url ) {
//echo $url["mymeta_url"];
foreach ( $url[\'mymeta_slider\'] as $u ) {
//echo "$u<br> "; shows post id like 67 68 56 etc
$my_query = new WP_Query( array( \'post_type\' => \'slider\', \'p\' => "\'.$u .\'" ) );
if ( $my_query->have_posts() ) {
while ( $my_query->have_posts() ) {
$my_query->the_post();
the_title();
}
}
wp_reset_postdata();
}
但我的输出结果重复了。也就是说,帖子类型滑块有3篇帖子查询显示6篇帖子重复了两次。
最合适的回答,由SO网友:Hendrik Luehrsen 整理而成
我是否正确地看到,您正在对要检索的每个帖子进行WP\\U查询?这是非常低效的,您可以使用WP\\u查询提供的post\\u-in参数。(http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters)
foreach ( $urls as $url ) {
//echo $url["mymeta_url"];
foreach ( $url[\'mymeta_slider\'] as $u ) {
//echo "$u<br> "; shows post id like 67 68 56 etc
$posts_to_recive[] = $u;
}
$my_query = new WP_Query( array( \'post_type\' => \'slider\', \'post__in\' => $posts_to_recive ) );
if ( $my_query->have_posts() ) {
while ( $my_query->have_posts() ) {
$my_query->the_post();
the_title();
}
}
wp_reset_postdata();
}
然而,这是非常低效的。保持WP\\U查询量尽可能低。