获取帖子作者的帖子-WordPress

时间:2020-02-07 16:05:15

标签: php html 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 ?>

1 个答案:

答案 0 :(得分:0)

您有两件事。您的$args数组中有一个非法分号,type应该是post_type,我也不完全确定您是否需要在其中使用奇怪的比较运算符?

尝试一下:

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

还要确保设置了$post,具体取决于您在何处调用 ,可能没有实例化/全局$post对象。

这还假设您正在使用称为post_type的实际event,否则,如果您正在检查元字段'_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'
        )
    )
);