简体   繁体   English

获取帖子作者的帖子 - WordPress

[英]Get posts from post author - WordPress

I'm trying to make a section that shows the posts created by the user that created the current post.我正在尝试制作一个部分,显示创建当前帖子的用户创建的帖子。 I need to show them obviously excluding the current post and according to a certain type of post (event).我需要明显地显示它们,不包括当前帖子并根据某种类型的帖子(事件)。

I have this code, but it is not working.我有这个代码,但它不起作用。 Can someone think of how to help me?有人能想到如何帮助我吗?

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

You've got a couple of things.你有几件事。 You've got an illegal semi-colon in your $args array, type should be post_type , I'm also not entirely sure you need that weird comparison operator in there?你的$args数组中有一个非法的分号, type应该是post_type ,我也不完全确定你需要那个奇怪的比较运算符吗?

Try this:尝试这个:

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

Also make sure that $post is set, depending on where you're calling this, there may not be an instantiated/global $post object.另外,还要确保$post设置,这取决于你调用这个地方,可能没有一个实例化/全球$post的对象。

This is also assuming you're using an actual post_type called event , otherwise if you're checking regular posts for the meta field '_listing_type' , you'll need to drop the post_type argument, and add a meta_query argument instead - note that this is inherently slow on large databases because the postmeta table isn't indexed by default.这也假设您正在使用一个名为event的实际post_type ,否则如果您正在检查元字段'_listing_type'常规帖子,您将需要删除post_type参数,并添加一个meta_query参数 - 请注意这在大型数据库上本质上很慢,因为postmeta表默认情况下没有索引。 Also note that meta_query is an array of arrays .另请注意, meta_query是一个数组数组

That query would end up looking something like this:该查询最终看起来像这样:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM