Wordpress 排除父帖子并在存档中仅显示子帖子
我已经完成了这个查询并且正在工作。我有很多子帖子,我计划在列出我的自定义帖子类型城市指南的存档页面时只显示子帖子Wordpress 排除父帖子并在存档中仅显示子帖子,wordpress,parent-child,archive,Wordpress,Parent Child,Archive,我已经完成了这个查询并且正在工作。我有很多子帖子,我计划在列出我的自定义帖子类型城市指南的存档页面时只显示子帖子 $args = array( 'orderby' => 'date', 'order' => 'DESC', 'post_type' => 'city-guide', 'posts_per_page' => 36, 'paged' => $paged ); $query = new WP_Query( $args ); ?> <
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'city-guide',
'posts_per_page' => 36,
'paged' => $paged
);
$query = new WP_Query( $args );
?>
<?php $i=1; while( $query->have_posts() ): $query->the_post(); ?>
{
.....
}
$args=array(
'orderby'=>'date',
“订单”=>“描述”,
“post_type”=>“城市指南”,
“每页帖子数”=>36,
“paged”=>paged美元
);
$query=新的WP\u查询($args);
?>
{
.....
}
我试过了
$all = get_posts(array('post_type'=> 'city-guide', 'posts_per_page' => -1));
$parents = array();
foreach ($all as $single)
{
$kids = get_children($single->ID);
if(isset($kids) && !empty($kids) && count($kids) >= 1)
{
$parents[] = $single->ID;
}
}
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'city-guide',
'post__not_in' => $parents,
'posts_per_page' => 36,
'paged' => $paged
);
$query = new WP_Query( $args );
?>
<?php $i=1; while( $query->have_posts() ): $query->the_post(); ?>
{
....
}
$all=get_posts(数组('post_type'=>'城市指南','posts_per_page'=>-1));
$parents=array();
foreach($全部为$single)
{
$kids=get_children($single->ID);
如果(设置($kids)&!空($kids)&&count($kids)>=1)
{
$parents[]=$single->ID;
}
}
$args=数组(
'orderby'=>'date',
“订单”=>“描述”,
“post_type”=>“城市指南”,
'post__not_in'=>$parents,
“每页帖子数”=>36,
“paged”=>paged美元
);
$query=新的WP\u查询($args);
?>
{
....
}
这不起作用。请帮助我找出哪里出了问题。我看到您正在尝试将ID推入数组,但为什么不在循环时使用ID,同时将子对象放入循环中?下面的例子是我将如何解决这个问题
<?php
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'city-guide',
'posts_per_page' => 36,
'paged' => $paged
);
$query = new WP_Query( $args );
$i=1; while( $query->have_posts() ): $query->the_post();
$parentID = get_the_ID();
$childrenArgs = array(
'post_type' => 'page',
'post_parent' => $parentID ,
);
$children = get_children($childrenArgs);
foreach ($children as $child){
echo '<h1>' . $child -> post_title . '</h1>';
$content = $child -> post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
}
endwhile;
?>
我知道这是一个老问题,但希望我能帮助那些在这里寻找与我相同东西的人 通过使用“post\u parent\u not\u in”参数排除post\u parent=0的所有帖子,可以仅显示子帖子:
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'city-guide',
'posts_per_page' => 36,
'paged' => $paged,
'post_parent__not_in' => array(0)
);
这避免了需要循环通过每个家长帖子来获取每个孩子。我认为您需要查看操作。在你的functions.php中类似这样的东西就可以做到这一点
function namespace_custom_query_vars( $query ) {
if ( !is_admin() && $query->is_main_query()) {
if ( $query->query["post_type"] == 'custom_post_type' ) {
$query->set( 'post_parent__not_in', 0 );
}
}
return $query;
}
add_action( 'pre_get_posts', 'namespace_custom_query_vars' );
有一篇关于这件事的不错的帖子。但是请注意,此页面上的代码不会针对小的语法错误进行编译。使用关系如何?一个简单的析取结合应该很有魅力
$args = array(
'post_type' => POST_TYPE,
'posts_per_page' => 36,
'orderby' => 'date',
'order' => 'DESC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => POST_TAXONOMY,
'field' => 'slug',
'terms' => $tax_slug,
'include_children' => true
),
array(
'taxonomy' => POST_TAXONOMY,
'field' => 'slug',
'terms' => $tax_slug,
'include_children' => false,
'operator' => 'NOT IN'
)
)
);
为什么不考虑这个问题?那么简单有效。谢谢