从WP_QUERY中排除具有子页面的父页面

时间:2017-02-16 作者:carkod

HoW CA.N 我 QUERY oNLY THosE PA.GEs THA.T DO NOT HA.五、E CH我LD PA.GEs?

&#十、A.;&#十、A.;

E.G.:

&#十、A.;&#十、A.;
    &#十、A.;PA.RENT PA.GE 1.&#十、A.;&#十、A.;
      &#十、A.;CH我LD PA.GE 1.&#十、A.;CH我LD PA.GE 2.&#十、A.;
    &#十、A.;PA.RENT PA.GE 2.&#十、A.;PA.RENT PA.GE 3.&#十、A.;&#十、A.;
      &#十、A.;CH我LD PA.GE 1.&#十、A.;CH我LD PA.GE 2.&#十、A.;
    &#十、A.;PA.RENT PA.GE 4.&#十、A.;
&#十、A.;&#十、A.;

我 WoULD L我KE To sHoW

&#十、A.;&#十、A.;
    &#十、A.;PA.RENT PA.GE 2.&#十、A.;PA.RENT PA.GE 4.&#十、A.;
&#十、A.;&#十、A.;

&#十、A.;    $NEWQUERY = NEW WP_QUERY( A.RRA.Y (&#十、A.;        \'PosTs_PER_PA.GE\' => -1.,&#十、A.;        \'PosT_TYPE\' => \'PA.GE\',&#十、A.;        // soLUT我oN?&#十、A.;    ) );&#十、A.;

&#十、A.;&#十、A.;

THA.NKs

&#十、A.;

2 个回复
最合适的回答,由SO网友:Paul \'Sparrow Hawk\' Biron 整理而成

the_dramatist\'s answer 将只提供没有子级的顶级页面,这与描述中给出的示例相匹配。

但是,如果要获取所有叶页,请使用以下选项:

SELECT *
FROM $wpdb->posts
WHERE
    post_type = \'page\' AND ID NOT in
    (
        SELECT ID
        FROM $wpdb->posts
        WHERE
            post_type = \'page\' AND ID in
            (
                SELECT post_parent
                FROM $wpdb->posts
                WHERE post_type = \'page\'
            )
    ) AND post_status = \'publish\'

SO网友:CodeMascot

嗯,您的情况很不寻常,我尝试了一些默认方法,但使用基于SQL的函数似乎是我的正确选择-

function the_dramatist_pages_not_own_child() {
    global $wpdb;
    $sql = "SELECT * 
        FROM   {$wpdb->posts} AS p 
        WHERE  p.post_type LIKE \'page\' 
               AND p.post_parent LIKE 0 
               AND p.id NOT IN (SELECT post_parent 
                                FROM   wp_posts AS p 
                                WHERE  p.post_type = \'page\' 
                                       AND p.post_parent != \'0\' 
                                GROUP  BY post_parent)";

    return $wpdb->get_results($sql);
}
当你打电话给the_dramatist_pages_not_own_child() 它会将所有帖子作为没有子页面的单个对象返回给您。

希望这有帮助。

相关推荐

如何在class-wp-query.php中跟踪核心函数is_page()&is_Single上的通知警告

在我的生产站点上,我的这些产品已经超支了:PHP Notice: Trying to get property of non-object in /public_html/wp-includes/class-wp-query.php on line 3728 PHP Notice: Trying to get property of non-object in /public_html/wp-includes/class-wp-query.php on line 3730 PHP No