从帖子作者那里获取帖子 - WordPress



我正在尝试制作一个部分,显示创建当前帖子的用户创建的帖子。我需要向他们展示明显排除当前帖子并根据某种类型的帖子(事件(显示它们。

我有这个代码,但它不起作用。有人能想到如何帮助我吗?

<?php 
$args = array(
'author'        =>  $post->post_author;
'type'          => $post_meta['_listing_type'][0] == 'event',
'order'         =>  'ASC',
'posts_per_page' => -1
);
$eventos = get_posts( $args );
foreach ( $eventos as $evento ) {
$output .= '<div>'.$evento.'</div>';
endforeach;
if(!empty($eventos)) : ?> 
<div id="listing-eventosartista" class="listing-section">
<h3 class="listing-desc-headline margin-top-60 margin-bottom-30"><?php esc_html_e('Eventos del artista','listeo_core'); ?></h3>
<div class="single-item">
<?php echo $output; ?>
</div>
</div>
<?php endif ?>

你有几件事。你的$args数组中有一个非法的分号,type应该post_type,我也不完全确定你需要那个奇怪的比较运算符吗?

试试这个:

$args = array(
'author'         => $post->post_author,
'post_type'      => 'event',
'order'          => 'ASC',
'posts_per_page' => -1
);

还要确保设置了$post,根据你调用它的位置,可能没有实例化/全局$post对象。

这也是假设您使用的是名为event的实际post_type,否则,如果您正在检查元字段'_listing_type'的常规帖子,则需要删除post_type参数,并添加一个meta_query参数 - 请注意,这在大型数据库上本质上很慢,因为默认情况下不索引postmeta表。另请注意,meta_query是一个数组数组

该查询最终将如下所示:

$args = array(
'author'         => $post->post_author,
'order'          => 'ASC',
'posts_per_page' => -1,
'meta_query'     => array(
array(
'meta_key'   => '_listing_type',
'meta_value' => 'event'
)
)
);

最新更新