gpt4 book ai didi

wordpress - 如何查询多个家长的帖子

转载 作者:行者123 更新时间:2023-12-03 05:02:02 25 4
gpt4 key购买 nike

我想查询当前页面的 sibling 和 child 。

单独使用 'post_parent' => $post->post_parent'post_parent' => $post->ID 我可以让它工作。

但是我想同时查询它们

我尝试使用变量“合并”它们,但似乎不起作用。我得到的输出是网站上的所有页面(与 post_parent 为 0 相同)。

完整代码:

<?php
$mysibling = $post->post_parent;
$mychild = $post->ID;
$mychildmysibling = array( $mychild, $mysibling );

$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent' => $mychildmysibling,
'sort_order' => 'asc'
);

$parent = new WP_Query( $args );

if ( $parent->have_posts() ) : ?>

<ul>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>
<?php endwhile ?>
</ul>

<?php endif; wp_reset_query(); ?>

最佳答案

您的查询应如下所示:

$args = array(
'post_type' => 'page',
'posts_per_page' => -1,
'post_parent__in' => $mychildmysibling,
'sort_order' => 'asc'
);

正如本页所写:https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters

post_parent (int) - use page id to return only child pages. Set to 0 to return only top-level entries.
post_parent__in (array) - use post ids. Specify posts whose parent is in an array. (available since Version 3.6)

将其视为一个 MySQL 问题,其中使用 "IN" clause使用多个值时。

关于wordpress - 如何查询多个家长的帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37907201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com