查询自定义帖子类型并从每个自定义帖子类型返回一个帖子
问题描述:
我设置了一个查询来搜索我设置的一些自定义帖子类型。我可以让它显示所有帖子,所以我知道它正在搜索我想要的自定义帖子类型。但我无法弄清楚如何让它只从我搜索的每个自定义帖子类型中返回一个帖子。查询自定义帖子类型并从每个自定义帖子类型返回一个帖子
示例:我有4个自定义帖子类型。 CPT1,CPT2,CPT3,CPT4。我想从CPT1中获得一篇文章,从CPT2中获得一篇文章,等等,这样在循环中就可以得到:CPT1的标题,CPT2的标题等等。
然后我想我可以做一个查询每个自定义的帖子类型,并让它们各自拥有自己的循环,但这看起来好像很多代码。感谢您的高级帮助。
这是代码我现在有:
$mystate = get_state();
// args
$args = array(
'numberposts' => -1,
'post_type' => array('CPT1','CPT2','CPT3','CPT4'),
'meta_key' => 'state_420',
'meta_value' => $mystate,
'order' => 'ASC',
);
/query
$the_query = new WP_Query($args);
?>
<?php
if ($the_query->have_posts()) : ?>
<?php
/* Start the Loop */
while ($the_query->have_posts()) : $the_query->the_post();
/**
* Run the loop for the search to output the results.
* If you want to overload this in a child theme then include a file
* called content-search.php and that will be used instead.
*/
?>
<li style="display: inline-block;"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>, </li>
<?php
endwhile;
the_posts_pagination(array('mid_size' => 2));
else :
get_template_part('template-parts/content', 'none');
endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
答
你应该通过这个参数posts_per_page => 1
我已经试过了,但仅返回一个职位的所有自定义文章类型 – bootstrap714