【问题标题】:Get posts from post author - WordPress从帖子作者那里获取帖子 - WordPress
【发布时间】:2020-02-07 16:05:15
【问题描述】:

我正在尝试创建一个部分来显示由创建当前帖子的用户创建的帖子。我需要显示他们显然不包括当前帖子并根据某种类型的帖子(事件)。

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

<?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 ?>

【问题讨论】:

    标签: php html wordpress


    【解决方案1】:

    你有几件事。你的$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'
            )
        )
    );
    

    【讨论】:

    • 这对我有用!但是我使用了keyvalue 而不是meta_keymeta_value 谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-01-22
    • 2011-03-12
    • 2010-12-04
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多