Wordpress 从循环中删除具有父级的帖子

Wordpress 从循环中删除具有父级的帖子,wordpress,posts,Wordpress,Posts,我正在尝试从我的网站的模板页面中删除具有post_父级的帖子。我可以通过以下方式防止它们在循环中显示: if (have_posts()) : while (have_posts()) : the_post(); if (($post->post_parent) ) { continue; } else { //code to display post } 问题是分页仍然显示在页面上,因为查询中的帖子仍然多于每页我的帖子。这会导致

我正在尝试从我的网站的模板页面中删除具有post_父级的帖子。我可以通过以下方式防止它们在循环中显示:

if (have_posts()) : while (have_posts()) : the_post(); 
    if (($post->post_parent) ) {
        continue;
    } else {
        //code to display post
    }
问题是分页仍然显示在页面上,因为查询中的帖子仍然多于每页我的帖子。这会导致分页中出现许多空白页

我试过以下几种:

function exclude_children( $query ) {
          $query->set('post_parent', 0 );
    }
    add_action( 'pre_get_posts', 'exclude_children' );
以及:

在我的循环模板上使用query\u posts()命令之前,请尝试:


因此,我使用的模板页面已经在使用一个查询(从特定类别中获取所有自定义帖子“产品”)。我尝试使用数组合并,但它似乎没有任何作用:
array\u merge($wp\u query->query,array('post\u parent'=>0))
@OrthoClassic Chris,你不应该合并数组,这对你不好(从你现在做的方式来看)。您应该在现有查询参数中插入
'post\u parent=0'
$where .= ' AND post_parent = 0';
  $query = new WP_Query( 'post_parent=0' ); //Display only top-level pages, exclude all child pages: